1
|
|
|
<?php |
2
|
|
|
namespace nebula\application; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use nebula\NebulaApplication; |
6
|
|
|
use nebula\application\Resource; |
7
|
|
|
use nebula\component\loader\Path; |
8
|
|
|
use nebula\application\config\Config; |
9
|
|
|
use nebula\component\request\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 应用程序加载器 |
13
|
|
|
* |
14
|
|
|
* 负责加载 |
15
|
|
|
*/ |
16
|
|
|
class Application |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* 应用 |
20
|
|
|
* |
21
|
|
|
* @var NebulaApplication |
22
|
|
|
*/ |
23
|
|
|
protected $nebula; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 资源管理 |
27
|
|
|
* |
28
|
|
|
* @var Resource |
29
|
|
|
*/ |
30
|
|
|
protected $resource; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function setNebula(NebulaApplication $application) |
34
|
|
|
{ |
35
|
|
|
$this->nebula = $application; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get 资源管理 |
41
|
|
|
* |
42
|
|
|
* @return Resource |
43
|
|
|
*/ |
44
|
|
|
public function getResource() |
45
|
|
|
{ |
46
|
|
|
return $this->resource; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set 资源管理 |
51
|
|
|
* |
52
|
|
|
* @param Resource $resource 资源管理 |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public function setResource(Resource $resource) |
57
|
|
|
{ |
58
|
|
|
$this->resource = $resource; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get 应用 |
65
|
|
|
* |
66
|
|
|
* @return NebulaApplication |
67
|
|
|
*/ |
68
|
|
|
public function getNebula() |
69
|
|
|
{ |
70
|
|
|
return $this->nebula; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* 初始化 |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function initialization() |
79
|
|
|
{ |
80
|
|
|
$this->initResource(); |
81
|
|
|
$this->importClassLoader($this->getConfig('app.import', [])); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* 处理请求 |
86
|
|
|
* |
87
|
|
|
* @param Request $request |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
|
|
public function onRequest(Request $request): bool |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* 处理异常 |
97
|
|
|
* |
98
|
|
|
* @param Exception $exception |
99
|
|
|
* @return boolean |
100
|
|
|
*/ |
101
|
|
|
public function onException(Exception $exception): bool |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* 初始化资源 |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
protected function initResource() |
112
|
|
|
{ |
113
|
|
|
$resource = new Resource; |
114
|
|
|
foreach ($this->getConfig('app.resource') as $path) { |
115
|
|
|
if (Path::isRelativePath($path)) { |
116
|
|
|
$path = Resource::getPathByRelativedPath($path, constant('NEBULA_APP')); |
117
|
|
|
} |
118
|
|
|
$resource->addResourcePath($path); |
119
|
|
|
} |
120
|
|
|
$this->setResource($resource); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* 导入 ClassLoader 配置 |
126
|
|
|
* |
127
|
|
|
* @param array $import |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function importClassLoader(array $import) |
131
|
|
|
{ |
132
|
|
|
foreach ($import as $name => $path) { |
133
|
|
|
if (Path::isRelativePath($path)) { |
134
|
|
|
$path = Resource::getPathByRelativedPath($path, constant('NEBULA_APP')); |
135
|
|
|
} |
136
|
|
|
if (\is_numeric($name)) { |
137
|
|
|
$this->getNebula()->getClassLoader()->addIncludePath($path); |
138
|
|
|
} elseif (is_file($path)) { |
139
|
|
|
$this->getNebula()->getClassLoader()->import($path); |
140
|
|
|
} else { |
141
|
|
|
$this->getNebula()->getClassLoader()->addIncludePath($path, $name); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* 获取配置 |
148
|
|
|
* |
149
|
|
|
* @param string $name |
150
|
|
|
* @param mixed $default |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
public function getConfig(string $name, $default = null) |
154
|
|
|
{ |
155
|
|
|
return $this->getNebula()->getConfig()->get($name, $default); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.