Bootstrap.php
2.78 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
<?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';
}
}
}