Bootstrap.php
4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?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';
// }
// }
}