Passed
Push — master ( f03095...9cdc70 )
by 世昌
02:15
created

NebulaApplication::configuration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace nebula;
3
4
use nebula\application\Route;
5
use nebula\application\Resource;
6
use nebula\component\debug\Debug;
7
use nebula\component\loader\Path;
8
use nebula\application\Application;
0 ignored issues
show
Bug introduced by
The type nebula\application\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use nebula\component\loader\Loader;
10
use nebula\application\config\Config;
11
use nebula\component\runnable\Runnable;
12
use nebula\application\filesystem\FileSystem;
13
14
/**
15
 * 应用
16
 *
17
 */
18
class NebulaApplication
19
{
20
    /**
21
     * 应用路径
22
     *
23
     * @var string
24
     */
25
    protected $path;
26
27
    /**
28
     * 类加载器
29
     *
30
     * @var Loader
31
     */
32
    protected $classLoader;
33
34
    /**
35
     * 调试工具
36
     *
37
     * @var Debug
38
     */
39
    protected $debugger;
40
41
    /**
42
     * 路由
43
     *
44
     * @var Route
45
     */
46
    protected $route;
47
    
48
    /**
49
     * 配置信息
50
     *
51
     * @var Config
52
     */
53
    protected $config;
54
55
    /**
56
     * 应用引用程序
57
     *
58
     * @var Application
59
     */
60
    protected $application;
61
62
    /**
63
     * 静态实例
64
     *
65
     * @var NebulaApplication
66
     */
67
    protected static $instance;
68
69
    /**
70
     * 返回实例
71
     *
72
     * @return NebulaApplication
73
     */
74
    public static function instance()
75
    {
76
        if (isset(static::$instance)) {
77
            return static::$instance;
78
        }
79
        return static::$instance = new static;
80
    }
81
    
82
    /**
83
     * Get 类加载器
84
     *
85
     * @return  Loader
86
     */
87
    public function getClassLoader()
88
    {
89
        return $this->classLoader;
90
    }
91
92
    /**
93
     * Set 类加载器
94
     *
95
     * @param  Loader  $classLoader  类加载器
96
     *
97
     * @return  self
98
     */
99
    public function setClassLoader(Loader $classLoader)
100
    {
101
        $this->classLoader = $classLoader;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Get 调试工具
108
     *
109
     * @return  Debug
110
     */
111
    public function getDebugger()
112
    {
113
        return $this->debugger;
114
    }
115
116
    /**
117
     * Set 调试工具
118
     *
119
     * @param  Debug  $debugger  调试工具
120
     *
121
     * @return  self
122
     */
123
    public function setDebugger(Debug $debugger)
124
    {
125
        $this->debugger = $debugger;
126
127
        return $this;
128
    }
129
130
131
    /**
132
     * Get 配置信息
133
     *
134
     * @return  Config
135
     */
136
    public function getConfig()
137
    {
138
        return $this->config;
139
    }
140
141
    /**
142
     * Set 配置信息
143
     *
144
     * @param  Config  $config  配置信息
145
     *
146
     * @return  self
147
     */
148
    public function setConfig(Config $config)
149
    {
150
        $this->config = $config;
151
152
        return $this;
153
    }
154
155
    /**
156
     * Get 路由
157
     *
158
     * @return  Route
159
     */
160
    public function getRoute()
161
    {
162
        return $this->route;
163
    }
164
165
    /**
166
     * Set 路由
167
     *
168
     * @param  Route  $route  路由
169
     *
170
     * @return  self
171
     */
172
    public function setRoute(Route $route)
173
    {
174
        $this->route = $route;
175
176
        return $this;
177
    }
178
 
179
    public static function configuration(string $path)
180
    {
181
        $nebula = static::instance();
182
        $nebula->path = $path;
183
        if (!FileSystem::exist($path)) {
184
            FileSystem::copyDir(NEBULA_RESOURCE.'/application', $path);
185
        }
186
        $config = new Config;
187
        $manifast = $config->loadConfig($path.'/manifast.json');
188
        $config->set('app', $manifast);
189
        $nebula->setConfig($config);
190
        $nebula->initiation();
191
        return $nebula;
192
    }
193
    
194
    /**
195
     * 初始化操作
196
     *
197
     * @return void
198
     */
199
    protected function initiation()
200
    {
201
        $this->initBaseConfig();
202
    }
203
204
205
    /**
206
     * 初始化路由组
207
     *
208
     * @return void
209
     */
210
    private function initBaseConfig()
211
    {
212
        if (!$this->config->has('app.route.active')) {
213
            $this->config->set('app.route.active', ['default']);
214
        }
215
        if (defined('NEBULA_ROUTE_GROUPS')) {
216
            $this->config->set('app.route.active', \explode(',', constant('NEBULA_ROUTE_GROUPS')));
217
        }
218
        if (!$this->config->has('app.import')) {
219
            $this->config->set('app.import', [
220
                0 => \constant('NEBULA_APP').'/' .'share',
221
            ]);
222
        }
223
224
        if (!$this->config->has('app.resource')) {
225
            $this->config->set('app.resource', [
226
                0 => \constant('NEBULA_APP').'/' .'resource',
227
            ]);
228
        }
229
    }
230
231
    /**
232
     * 运行应用
233
     *
234
     * @return void
235
     */
236
    public function run()
237
    {
238
        $className  = $this->config->get('app.application', Application::class);
239
        $this->application = Runnable::newClassInstance($className);
240
        $this->application->setNebula($this);
241
        $this->application->initiation();
242
    }
243
}
244