Bootstrap.php 4.82 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)
    {
        $hostParts = explode('.', $dispatcher->getRequest()->getServer('HTTP_HOST', ''));
        $level = count($hostParts) - 1;

        /* 根据域名的级别,设置默认的模块、控制器、方法 */
        $module = 'Index';
        $controller = 'Index';
        $action = 'Index';
        // 二级域名
        if (3 === $level) {
            $subDomain = strval($hostParts[0]);
            switch (strtolower($subDomain)) {
                case 'www':  // 主站
                case 'new':  // 原新版
                case 'dev':  // 开发环境
                    break;
                case 'search': // 搜索
               		$searchRequest = new Yaf\Request\Http('/product/search/index');
                    $dispatcher->setRequest($searchRequest);
                    break;
                case 'guang': // 逛
                    $module = 'Guang';
                    break;
                case 'list': // 商品列表
                    $module = 'Product';
                    break;
                case 'sale'://促销
                    $module = 'Product';
                    $controller = 'sale';
                    $saleRequest = new Yaf\Request\Http('/product/sale/index');
                    $dispatcher->setRequest($saleRequest);
                    break;
                default: // 其它(识别为品牌)
                    $module = 'Product';
                    $action = 'Brand';
                    $dispatcher->getRequest()->setParam('named', $subDomain);
                    break;
            }
        }
        $dispatcher->getRequest()->module = $module;
        $dispatcher->getRequest()->controller = $controller;
        $dispatcher->getRequest()->action = $action;

        /* 根据对应模块的配置,添加相应的路由规则 */
        $iniFile = APPLICATION_PATH . '/configs/routes.' . strtolower($module) . '.ini';
        if (file_exists($iniFile)) {
            $config = new Config\Ini($iniFile);
            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';
//        }
//    }
}