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
|
|
|
$dotEnv = new Dotenv($this->basePath); |
41
|
|
|
$dotEnv->load(); |
42
|
|
|
date_default_timezone_set(env('APP_TIMEZONE', 'Asia/Shanghai')); |
43
|
|
|
define('IS_CLI', $this->runningInConsole()); |
44
|
|
|
define('IS_DEBUG',env('DEBUG',false)); |
45
|
|
|
if (IS_DEBUG) { |
46
|
|
|
error_reporting(E_ALL); |
47
|
|
|
@ini_set('display_errors', 'On'); |
|
|
|
|
48
|
|
|
//@ob_start(); |
49
|
|
|
$whoops=new Run; |
50
|
|
|
$handle=IS_CLI ? "PlainTextHandler" : "PrettyPageHandler"; |
51
|
|
|
$handle="\\Whoops\\Handler\\".$handle; |
52
|
|
|
$whoops->pushHandler(new $handle); |
53
|
|
|
$whoops->register(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function run() { |
59
|
|
|
$this->route->dispatch(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
/** |
62
|
|
|
* 初始化容器 |
63
|
|
|
*/ |
64
|
|
|
private function initContainer() { |
65
|
|
|
static::setInstance($this); |
66
|
|
|
$this->instance('app',$this); |
67
|
|
|
$this->instance('config',new Config()); |
68
|
|
|
$this->instance('request',new Request($this->config)); |
|
|
|
|
69
|
|
|
$this->instance('route',new Route($this->request)); |
|
|
|
|
70
|
|
|
$this->bind('pinyin','\puck\helpers\PinYin'); |
71
|
|
|
$this->bind('curl','\puck\helpers\Curl'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the base path for the application. |
76
|
|
|
* |
77
|
|
|
* @param string|null $path |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
public function basePath($path = null) |
81
|
|
|
{ |
82
|
3 |
|
if (isset($this->basePath)) { |
83
|
3 |
|
return $this->basePath.($path ? '/'.$path : $path); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->runningInConsole()) { |
87
|
|
|
$this->basePath = getcwd(); |
88
|
|
|
} else { |
89
|
|
|
$this->basePath = realpath(getcwd().'/../'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->basePath($path); |
93
|
|
|
} |
94
|
|
|
/** |
95
|
|
|
* 判断是否是cli模式 |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function runningInConsole() |
100
|
|
|
{ |
101
|
|
|
return php_sapi_name() == 'cli'; |
102
|
|
|
} |
103
|
|
|
/** |
104
|
|
|
* 加载一个配置文件 |
105
|
|
|
* |
106
|
|
|
* @param string $name |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function configure($name) |
110
|
|
|
{ |
111
|
|
|
if (isset($this->loadedConfigurations[$name])) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
//标记为已加载 |
115
|
|
|
$this->loadedConfigurations[$name] = true; |
116
|
|
|
$path = $this->getConfigurationPath($name); |
117
|
|
|
if ($path) { |
|
|
|
|
118
|
|
|
$this->make('config')->set($name, require $path); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* 获取配置文件的路径。 |
125
|
|
|
* |
126
|
|
|
* 如果没有给定配置文件的名字,则返回目录。 |
127
|
|
|
* |
128
|
|
|
* 如果应用目录下有相应配置文件则优先返回。 |
129
|
|
|
* |
130
|
|
|
* @param string|null $name |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getConfigurationPath($name = null) |
134
|
|
|
{ |
135
|
|
|
if (! $name) { |
|
|
|
|
136
|
|
|
$appConfigDir = $this->basePath('configs').'/'; |
137
|
|
|
|
138
|
|
|
if (file_exists($appConfigDir)) { |
139
|
|
|
return $appConfigDir; |
140
|
|
|
} elseif (file_exists($path = __DIR__.'/configs/')) { |
141
|
|
|
return $path; |
142
|
|
|
} |
143
|
|
|
} else { |
144
|
|
|
$appConfigPath = $this->basePath('configs').'/'.$name.'.php'; |
145
|
|
|
if (file_exists($appConfigPath)) { |
146
|
|
|
return $appConfigPath; |
147
|
|
|
} elseif (file_exists($path = __DIR__.'/configs/'.$name.'.php')) { |
148
|
|
|
return $path; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: