Passed
Push — master ( 185757...a51c77 )
by 世昌
02:03
created

Application::onException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application;
3
4
use Exception;
5
use nebula\Context;
6
use nebula\NebulaApplication;
7
use nebula\application\Resource;
8
use nebula\component\loader\Path;
9
use nebula\application\config\Config;
10
use nebula\component\request\Request;
11
use nebula\application\module\Manager;
12
use nebula\application\filesystem\FileSystem;
13
14
/**
15
 * 应用程序加载器
16
 *
17
 * 负责加载
18
 */
19
class Application
20
{
21
    /**
22
     * 应用
23
     *
24
     * @var Context
25
     */
26
    protected $context;
27
28
    /**
29
     * 资源管理
30
     *
31
     * @var Resource
32
     */
33
    protected $resource;
34
35
36
    /**
37
     * 模块管理器
38
     *
39
     * @var Manager
40
     */
41
    protected $manager;
42
    
43
    /**
44
     * 应用路径
45
     *
46
     * @var string
47
     */
48
    protected $path;
49
50
    /**
51
     * 数据路径
52
     *
53
     * @var string
54
     */
55
    protected $dataPath;
56
57
    /**
58
     * Get 资源管理
59
     *
60
     * @return  Resource
61
     */
62
    public function getResource()
63
    {
64
        return $this->resource;
65
    }
66
67
    /**
68
     * Set 资源管理
69
     *
70
     * @param  Resource  $resource  资源管理
71
     *
72
     * @return  self
73
     */
74
    public function setResource(Resource $resource)
75
    {
76
        $this->resource = $resource;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Get 应用
83
     *
84
     * @return  Context
85
     */
86
    public function getContext()
87
    {
88
        return $this->context;
89
    }
90
91
    /**
92
     * Set 应用
93
     *
94
     * @param  Context  $context  应用
95
     *
96
     * @return  self
97
     */
98
    public function setContext(Context $context)
99
    {
100
        $this->context = $context;
101
102
        return $this;
103
    }
104
105
    /**
106
     * 初始化
107
     *
108
     * @return void
109
     */
110
    public function initialization()
111
    {
112
        $this->initResource();
113
        $this->registerModuleManager();
114
        $this->manager->registerResource();
115
        $this->manager->registerSharedClassLoader($this);
116
        $this->registerRoute();
117
        $this->importClassLoader($this->getConfig('app.import', []), constant('NEBULA_APP'));
118
    }
119
120
    /**
121
     * 初始化资源
122
     *
123
     * @return void
124
     */
125
    protected function initResource()
126
    {
127
        $resource = new Resource;
128
        foreach ($this->getConfig('app.resource') as $path) {
129
            if (Path::isRelativePath($path)) {
130
                $path = Resource::getPathByRelativedPath($path, constant('NEBULA_APP'));
131
            }
132
            $resource->addResourcePath($path);
133
        }
134
        $this->setResource($resource);
135
    }
136
137
    /**
138
     * 注册模块
139
     *
140
     * @return void
141
     */
142
    protected function registerModuleManager()
143
    {
144
       $this->manager = new Manager($this->getContext());
145
       $this->manager->registerModule();
146
    }
147
148
    /**
149
     * 注册路由
150
     *
151
     * @return void
152
     */ 
153
    protected function registerRoute()
154
    {
155
        $this->getContext()->getRoute()->loadFromModule($this->manager);
156
    }
157
158
    /**
159
     * 导入 ClassLoader 配置
160
     *
161
     * @param array $import
162
     * @return void
163
     */
164
    public function importClassLoader(array $import, string $relativePath)
165
    {
166
        foreach ($import as $name => $path) {
167
            if (Path::isRelativePath($path)) {
168
                $path = Resource::getPathByRelativedPath($path, $relativePath);
169
            }
170
            if (\is_numeric($name)) {
171
                $this->getContext()->getClassLoader()->addIncludePath($path);
172
            } elseif (is_file($path)) {
173
                $this->getContext()->getClassLoader()->import($path);
174
            } else {
175
                $this->getContext()->getClassLoader()->addIncludePath($path, $name);
176
            }
177
        }
178
    }
179
180
    /**
181
     * 获取配置
182
     *
183
     * @param string $name
184
     * @param mixed $default
185
     * @return mixed
186
     */
187
    public function getConfig(string $name, $default = null)
188
    {
189
        return $this->getContext()->getConfig()->get($name, $default);
190
    }
191
192
    /**
193
     * Get 模块管理器
194
     *
195
     * @return  Manager
196
     */
197
    public function getManager()
198
    {
199
        return $this->manager;
200
    }
201
202
    /**
203
     * Set 模块管理器
204
     *
205
     * @param  Manager  $manager  模块管理器
206
     *
207
     * @return  self
208
     */
209
    public function setManager(Manager $manager)
210
    {
211
        $this->manager = $manager;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Get 应用路径
218
     *
219
     * @return  string
220
     */ 
221
    public function getPath()
222
    {
223
        return $this->path;
224
    }
225
226
    /**
227
     * Set 应用路径
228
     *
229
     * @param  string  $path  应用路径
230
     *
231
     * @return  self
232
     */ 
233
    public function setPath(string $path)
234
    {
235
        $this->path = $path;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Get 数据路径
242
     *
243
     * @return  string
244
     */ 
245
    public function getDataPath()
246
    {
247
        return $this->dataPath;
248
    }
249
250
    /**
251
     * Set 数据路径
252
     *
253
     * @param  string  $dataPath  数据路径
254
     *
255
     * @return  self
256
     */ 
257
    public function setDataPath(string $dataPath)
258
    {
259
        $this->dataPath = $dataPath;
260
261
        return $this;
262
    }
263
}
264