1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by rozbo at 2017/3/18 下午8:52 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace puck; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Dotenv\Dotenv; |
10
|
|
|
use puck\Config; |
11
|
|
|
use puck\Route; |
12
|
|
|
use puck\tools\Arr; |
13
|
|
|
use Whoops\Run; |
14
|
|
|
|
15
|
|
|
class App extends Container { |
16
|
|
|
/** |
17
|
|
|
* 已经加载的config文件 |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $loadedConfigurations = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* 应用的根目录. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $basePath; |
29
|
|
|
public function __construct($basePath) { |
30
|
|
|
$this->basePath=$basePath; |
31
|
|
|
$this->initEnv(); |
32
|
|
|
$this->initContainer(); |
33
|
|
|
$this->initConfig(); |
34
|
|
|
} |
35
|
|
|
private function initConfig(){ |
36
|
|
|
$this->configure('core'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function initEnv(){ |
40
|
|
|
try{ |
41
|
|
|
$dotEnv = new Dotenv($this->basePath); |
42
|
|
|
$dotEnv->load(); |
43
|
|
|
} |
44
|
|
|
catch (\Dotenv\Exception\InvalidPathException $e){ |
45
|
|
|
die("环境配置文件`.env`没有被找到."); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
date_default_timezone_set(env('APP_TIMEZONE', 'Asia/Shanghai')); |
48
|
|
|
define('IS_CLI', $this->runningInConsole()); |
49
|
|
|
define('IS_DEBUG',env('DEBUG',false)); |
50
|
|
|
if (IS_DEBUG) { |
51
|
|
|
error_reporting(E_ALL); |
52
|
|
|
@ini_set('display_errors', 'On'); |
|
|
|
|
53
|
|
|
//@ob_start(); |
54
|
|
|
$whoops=new Run; |
55
|
|
|
$handle=IS_CLI ? "PlainTextHandler" : "PrettyPageHandler"; |
56
|
|
|
$handle="\\Whoops\\Handler\\".$handle; |
57
|
|
|
$whoops->pushHandler(new $handle); |
58
|
|
|
$whoops->register(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function run() { |
64
|
|
|
$this->route->dispatch(); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
/** |
67
|
|
|
* 初始化容器 |
68
|
|
|
*/ |
69
|
|
|
private function initContainer() { |
70
|
|
|
static::setInstance($this); |
71
|
|
|
$this->instance('app',$this); |
72
|
|
|
$this->instance('config',new Config()); |
73
|
|
|
$this->instance('request',new Request($this->config)); |
|
|
|
|
74
|
|
|
$this->instance('route',new Route($this->request)); |
|
|
|
|
75
|
|
|
$this->bind('pinyin','\puck\helpers\PinYin'); |
76
|
|
|
$this->bind('curl','\puck\helpers\Curl'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the base path for the application. |
81
|
|
|
* |
82
|
|
|
* @param string|null $path |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
3 |
|
public function basePath($path = null) |
86
|
|
|
{ |
87
|
3 |
|
if (isset($this->basePath)) { |
88
|
3 |
|
return $this->basePath.($path ? '/'.$path : $path); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->runningInConsole()) { |
92
|
|
|
$this->basePath = getcwd(); |
93
|
|
|
} else { |
94
|
|
|
$this->basePath = realpath(getcwd().'/../'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->basePath($path); |
98
|
|
|
} |
99
|
|
|
/** |
100
|
|
|
* 判断是否是cli模式 |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function runningInConsole() |
105
|
|
|
{ |
106
|
|
|
return php_sapi_name() == 'cli'; |
107
|
|
|
} |
108
|
|
|
/** |
109
|
|
|
* 加载一个配置文件 |
110
|
|
|
* |
111
|
|
|
* @param string $name |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function configure($name) |
115
|
|
|
{ |
116
|
|
|
if (isset($this->loadedConfigurations[$name])) { |
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
//标记为已加载 |
120
|
|
|
$this->loadedConfigurations[$name] = true; |
121
|
|
|
$path = $this->getConfigurationPath($name); |
122
|
|
|
if ($path) { |
|
|
|
|
123
|
|
|
$this->make('config')->set($name, require $path); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* 获取配置文件的路径。 |
130
|
|
|
* |
131
|
|
|
* 如果没有给定配置文件的名字,则返回目录。 |
132
|
|
|
* |
133
|
|
|
* 如果应用目录下有相应配置文件则优先返回。 |
134
|
|
|
* |
135
|
|
|
* @param string|null $name |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getConfigurationPath($name = null) |
139
|
|
|
{ |
140
|
|
|
if (! $name) { |
|
|
|
|
141
|
|
|
$appConfigDir = $this->basePath('configs').'/'; |
142
|
|
|
|
143
|
|
|
if (file_exists($appConfigDir)) { |
144
|
|
|
return $appConfigDir; |
145
|
|
|
} elseif (file_exists($path = __DIR__.'/configs/')) { |
146
|
|
|
return $path; |
147
|
|
|
} |
148
|
|
|
} else { |
149
|
|
|
$appConfigPath = $this->basePath('configs').'/'.$name.'.php'; |
150
|
|
|
if (file_exists($appConfigPath)) { |
151
|
|
|
return $appConfigPath; |
152
|
|
|
} elseif (file_exists($path = __DIR__.'/configs/'.$name.'.php')) { |
153
|
|
|
return $path; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.