Test Failed
Push — master ( f4c898...1a3120 )
by 世昌
01:58
created

NebulaApplication::initBaseConfig()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 64
nop 0
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
1
<?php
2
namespace nebula;
3
4
use nebula\Context;
5
use nebula\application\Route;
6
use nebula\application\Debugger;
7
use nebula\component\loader\Path;
8
use nebula\application\Application;
9
use nebula\application\config\Config;
10
use nebula\component\request\Request;
11
use nebula\application\cache\FileCache;
12
use nebula\component\runnable\Runnable;
13
use nebula\application\filesystem\FileSystem;
14
use nebula\component\cacheable\CacheInterface;
15
use nebula\component\debug\log\AbstractLogger;
16
use nebula\component\debug\log\logger\FileLogger;
17
use nebula\component\debug\log\logger\NullLogger;
18
19
/**
20
 * 应用
21
 *
22
 */
23
class NebulaApplication extends Context
24
{
25
    /**
26
     * 应用路径
27
     *
28
     * @var string
29
     */
30
    protected $path;
31
32
    /**
33
     * 数据路径
34
     *
35
     * @var string
36
     */
37
    protected $dataPath;
38
39
    /**
40
     * 静态实例
41
     *
42
     * @var NebulaApplication
43
     */
44
    protected static $instance;
45
46
47
    /**
48
     * 获取实例化对象
49
     *
50
     * @param string $path
51
     * @return NebulaApplication
52
     */
53
    public static function configuration(string $path)
54
    {
55
        $nebula = static::instance();
56
        $nebula->path = $path;
57
        if (!FileSystem::exist($path)) {
58
            FileSystem::copyDir(NEBULA_RESOURCE.'/application', $path);
59
        }
60
        $config = new Config;
61
        $manifast = $config->loadConfig($path.'/manifast.json');
62
        $nebula->dataPath = Path::toAbsolutePath(defined('NEBULA_DATA') ? constant('NEBULA_DATA') : '~/nebula-data');
63
        $config->set('app', $manifast);
64
        $nebula->setConfig($config);
65
        return $nebula;
66
    }
67
    
68
    /**
69
     * 初始化操作
70
     *
71
     * @return void
72
     */
73
    public function initialization()
74
    {
75
        $this->setRoute(new Route($this));
76
        $this->initBaseConfig();
77
        $this->initDebugger();
78
        $this->initCache();
79
        $this->getDebugger()->info('initialized nebula application');
80
    }
81
82
83
    /**
84
     * 初始化基本配置
85
     *
86
     * @return void
87
     */
88
    private function initBaseConfig()
89
    {
90
        if (!$this->config->has('app.route.active')) {
91
            $this->config->set('app.route.active', ['default']);
92
        }
93
        if (defined('NEBULA_ROUTE_GROUPS')) {
94
            $this->config->set('app.route.active', \explode(',', constant('NEBULA_ROUTE_GROUPS')));
95
        }
96
        if (!$this->config->has('app.import')) {
97
            $this->config->set('app.import', [
98
                0 => \constant('NEBULA_APP').'/' .'share',
99
            ]);
100
        }
101
        if (!$this->config->has('app.resource')) {
102
            $this->config->set('app.resource', [
103
                0 => \constant('NEBULA_APP').'/' .'resource',
104
            ]);
105
        }
106
        if (!$this->config->has('app.module.scan-path')) {
107
            $this->config->set('app.module.scan-path', [
108
                0 => \constant('NEBULA_APP').'/' .'modules',
109
            ]);
110
        }
111
        $timezone = defined('DEFAULT_TIMEZONE')?constant('DEFAULT_TIMEZONE'):'PRC';
112
        date_default_timezone_set($this->config->get('app.timezone', $timezone));
113
    }
114
115
116
    /**
117
     * 初始化调试工具
118
     *
119
     * @return void
120
     */
121
    private function initDebugger()
122
    {
123
        $logger = (new Runnable($this->getConfig()->get('app.logger-build', [$this, 'buildLogger'])))->run();
124
        $debugger = new Debugger;
125
        
126
        $debugger->addAttribute('remote-ip' , $this->getRequest()->ip() );
127
        $debugger->addAttribute('request-uri' , $this->getRequest()->getUrl());
128
        $debugger->addAttribute('request-method' , $this->getRequest()->getMethod());
129
        $debugger->addAttribute('request-time' , date('Y-m-d H:i:s', \constant('NEBULA_START_TIME')));
130
131
        $debugger->applyConfig([
132
            'start-time' => \constant('NEBULA_START_TIME'),
133
            'start-memory' => \constant('NEBULA_START_MEMORY'),
134
        ]);
135
        
136
        $debugger->setLogger($logger);
137
        $logger->notice(PHP_EOL.'{request-time} {remote-ip} {request-method} {request-uri}', $debugger->getAttribute());
138
        $this->setDebugger($debugger);
139
    }
140
141
    /**
142
     * 构建日志记录器
143
     *
144
     * @return AbstractLogger
145
     */
146
    private function buildLogger(): AbstractLogger
147
    {
148
        $dataPath = $this->dataPath.'/logs';
149
        if (\is_writable(dirname($dataPath))) {
150
            FileSystem::makes($dataPath.'/zip');
151
            FileSystem::makes($dataPath.'/dump');
152
            return new FileLogger(
153
                [
154
                    'log-level' => defined('NEBULA_DEBUG_LEVEL') ? defined('NEBULA_DEBUG_LEVEL') : 'debug',
155
                    'save-path' => $dataPath,
156
                    'save-zip-path' => $dataPath.'/zip',
157
                    'log-format' => '%message%',
158
                    'save-pack-path' => $dataPath.'/dump',
159
                ]
160
            );
161
        }
162
        return new NullLogger;
163
    }
164
165
    /**
166
     * 初始化缓存器
167
     *
168
     * @return void
169
     */
170
    private function initCache()
171
    {
172
        $cache = (new Runnable($this->getConfig()->get('app.cache-build', [$this, 'buildCache'])))->run();
173
        $this->cache = $cache;
174
    }
175
176
    /**
177
     * 构建文件缓存
178
     *
179
     * @return CacheInterface
180
     */
181
    private function buildCache(): CacheInterface
182
    {
183
        $dataPath = $this->dataPath.'/cache';
184
        FileSystem::makes($dataPath);
185
        return new FileCache($dataPath);
186
    }
187
188
    /**
189
     * 运行应用
190
     *
191
     * @return void
192
     */
193
    public function run()
194
    {
195
        $className  = $this->config->get('app.application', Application::class);
196
        $this->application = Runnable::newClassInstance($className);
197
        $this->application->setContext($this);
198
        $this->application->setPath($this->path);
199
        $this->application->setDataPath($this->dataPath);
200
        $this->application->initialization();
201
        $this->application->onRequest($this->getRequest());
202
        $this->getDebugger()->info('nebula application shutdown');
203
    }
204
205
    /**
206
     * Get 数据路径
207
     *
208
     * @return  string
209
     */ 
210
    public function getDataPath()
211
    {
212
        return $this->dataPath;
213
    }
214
215
    /**
216
     * Get 应用路径
217
     *
218
     * @return  string
219
     */ 
220
    public function getPath()
221
    {
222
        return $this->path;
223
    }
224
}
225