Bootstrap.php 2.81 KB
<?php

/**
 * 启动运行
 * 
 * @name Bootstrap
 * @author fei.hong
 * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php
 * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
use Yaf\Bootstrap_Abstract;
use Yaf\Dispatcher;
use Yaf\Application;
use Yaf\Registry;
use Yaf\Loader;
use Yaf\Config;

use Plugin\TemplateLayout;

class Bootstrap extends Bootstrap_Abstract
{
    private $_config;

    /**
     * 初始化配置
     * @param Yaf_Dispatcher $dispatcher
     */
    public function _initConfig(Dispatcher $dispatcher)
    {
        define('REQUEST_METHOD', strtoupper($dispatcher->getRequest()->getMethod()));
        $this->_config = Application::app()->getConfig();
        Registry::set('appConfig', $this->_config);
    }

    /**
     * 初始化应用
     * @param Yaf_Dispatcher $dispatcher
     */
    public function _initApplication(Dispatcher $dispatcher)
    {
        defined('APPLICATION_SYSTEM_CONFIG') || define('APPLICATION_SYSTEM_CONFIG', $this->_config->application->servers->config);
        if (APPLICATION_ENV !== 'production') {
            error_reporting(E_ALL);
            ini_set('display_startup_errors', 1);
            ini_set('display_errors', 1);
        }
        Loader::getInstance()->registerLocalNameSpace(explode(',', $this->_config->application->namespaces));
    }

//    /**
//     * 初始化插件
//     * @param Yaf_Dispatcher $dispatcher
//     */
//    public function _initPlugin(Dispatcher $dispatcher)
//    {
//        
//    }

    /**
     * 初始化路由
     * @param Yaf_Dispatcher $dispatcher
     */
    public function _initRoute(Dispatcher $dispatcher)
    {
        $config = new Config\Ini(APPLICATION_PATH . '/configs/routes.ini');
        if (isset($config->routes)) {
            $dispatcher->getRouter()->addConfig($config->routes);
        }
    }

    /**
     * 初始化Layout
     * @param Yaf_Dispatcher $dispatcher
     */
    public function _initLayout(Dispatcher $dispatcher)
    {
        // 关闭自动渲染模板
        $dispatcher->autoRender(false);
        
        // 判断到不是AJAX请求时, 使用自定义的模板渲染 (Mustache or Handlebars)
        if (!$dispatcher->getRequest()->isXmlHttpRequest()) {
            $layout = new TemplateLayout();
            $layout->setScriptPath($this->_config->application->template->path);
            $dispatcher->setView($layout);
        } 
    }
    
//    /**
//     * 初始化第三方包
//     * @param Dispatcher $dispatcher
//     */
//    public function _initPackage(Dispatcher $dispatcher)
//    {
//        if ($this->_config->composer->autoload == 1) {
//            require $this->_config->composer->path . '/vendor/autoload.php';
//        }
//    }

}