-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBootstrap.php
159 lines (138 loc) · 3.34 KB
/
Bootstrap.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace HuiLib;
use HuiLib\App\Front;
/**
* 系统初始化引导文件,非单例模式
*
* SYS_PATH 系统根目录,在库和应用目录上一级
* LIB_PATH 库根目录,约定库目录在系统目录下
* APP_PATH 应用根目录,约定应用目录在系统目录下
* WWW_PATH 网页根目录
* APP_ENV 当前应用执行环境,匹配相关配置
* RUN_METHOD 应用执行方式web || bin
*
* @author 祝景法
* @since 2013/08/11
*/
class Bootstrap
{
//运行方式
const RUN_BIN='Bin';
const RUN_WEB='Web';
//运行环境
//产品环境
const ENV_PRODUCTION='production';
//演示环境
const ENV_STAGING='staging';
//测试环境
const ENV_TESTING='testing';
//开发环境
const ENV_DEVELOPMENT='development';
//默认环境
const DEFAULT_ENV = 'production';
/**
* 运行环境
* @var string Enum
*/
private $runEnv;
/**
* 运行入口
*
* 支持运行方式:web、bin
*/
private $runMethod;
/**
* 应用单例
* @var \HuiLib\App\AppBase
*/
private $application;
/**
* 期末执行绑定
* @var HuiLib\Runtime\ShutCall
*/
private $shutCall;
private $allowedEnv = array (self::ENV_PRODUCTION, self::ENV_STAGING, self::ENV_TESTING, self::ENV_DEVELOPMENT );
/**
* 加载器实例
* @var \HuiLib\Loader\AutoLoad
*/
private $loadInstance;
private function __construct()
{
if (! defined ( 'RUN_METHOD' ) ) {
throw new \Exception ( "Please define Constant var RUN_METHOD in the entry!" );
}
$this->runMethod=RUN_METHOD;
$this->initPath ();
$this->initEnv ();
$this->initLoader ();
}
/**
* 定义系统路径常量
*
* @throws \Exception
*/
private function initPath()
{
define ( 'SEP', DIRECTORY_SEPARATOR );
//URL地址分隔符
define ( 'URL_SEP', '/' );
//命名空间分隔符
define ( 'NAME_SEP', '\\' );
define ( 'LIB_PATH', dirname ( __FILE__ ) . SEP );
define ( 'SYS_PATH', dirname ( LIB_PATH ) . SEP );
if (! defined ( 'APP_PATH' ) || ! defined ( 'WWW_PATH' )) {
throw new \Exception ( "Please define Constant var APP_PATH & WWW_PATH in the entry!" );
}
}
/**
* 引入注册自动加载类
*/
private function initLoader()
{
include_once LIB_PATH . 'Loader/AutoLoad.php';
$this->loadInstance = \HuiLib\Loader\AutoLoad::getInstance ();
spl_autoload_register ( array ($this->loadInstance, 'loadClass' ) );
Front::getInstance()->setLoader($this->loadInstance);
}
public function autoLoaderInstance(){
return self::$loadInstance;
}
/**
* 初始化应用
* @return \HuiLib\App\AppBase
*/
public function createApp($config)
{
$this->application=\HuiLib\App\AppBase::factory($this->runMethod, $config);
Front::getInstance()->setApp($this->application);
return $this->application;
}
/**
* 初始化应用当前运行环境
*/
private function initEnv()
{
if (isset ( $_SERVER ['SERVER_ENV'] ) && in_array ( $_SERVER ['SERVER_ENV'], $this->allowedEnv )) {
define ( "APP_ENV", $_SERVER ['SERVER_ENV'] );
} else {
define ( "APP_ENV", self::DEFAULT_ENV );
}
}
/**
* 获取允许的服务器环境配置
*/
public function getAllowEnv(){
return $this->allowedEnv;
}
/**
* 获取引导类实例
* @return \HuiLib\Bootstrap
*/
public static function create()
{
$instance = new self ();
Front::getInstance()->setBootstrap($instance);
return $instance;
}
}