Total Complexity | 67 |
Total Lines | 584 |
Duplicated Lines | 0 % |
Coverage | 93.38% |
Changes | 0 |
Complex classes like App often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use App, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class App extends Container |
||
25 | { |
||
26 | const VERSION = '6.0.0RC2'; |
||
27 | |||
28 | /** |
||
29 | * 应用调试模式 |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $appDebug = true; |
||
33 | |||
34 | /** |
||
35 | * 应用开始时间 |
||
36 | * @var float |
||
37 | */ |
||
38 | protected $beginTime; |
||
39 | |||
40 | /** |
||
41 | * 应用内存初始占用 |
||
42 | * @var integer |
||
43 | */ |
||
44 | protected $beginMem; |
||
45 | |||
46 | /** |
||
47 | * 当前应用类库命名空间 |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $namespace = 'app'; |
||
51 | |||
52 | /** |
||
53 | * 应用根目录 |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $rootPath = ''; |
||
57 | |||
58 | /** |
||
59 | * 框架目录 |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $thinkPath = ''; |
||
63 | |||
64 | /** |
||
65 | * 应用目录 |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $appPath = ''; |
||
69 | |||
70 | /** |
||
71 | * Runtime目录 |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $runtimePath = ''; |
||
75 | |||
76 | /** |
||
77 | * 配置后缀 |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $configExt = '.php'; |
||
81 | |||
82 | /** |
||
83 | * 应用初始化器 |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $initializers = [ |
||
87 | Error::class, |
||
88 | RegisterService::class, |
||
89 | BootService::class, |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * 注册的系统服务 |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $services = []; |
||
97 | |||
98 | /** |
||
99 | * 初始化 |
||
100 | * @var bool |
||
101 | */ |
||
102 | protected $initialized = false; |
||
103 | |||
104 | /** |
||
105 | * 架构方法 |
||
106 | * @access public |
||
107 | * @param string $rootPath 应用根目录 |
||
1 ignored issue
–
show
|
|||
108 | */ |
||
109 | 18 | public function __construct(string $rootPath = '') |
|
120 | 18 | } |
|
121 | |||
122 | /** |
||
123 | * 注册服务 |
||
124 | * @access public |
||
125 | * @param Service|string $service 服务 |
||
1 ignored issue
–
show
|
|||
126 | * @param bool $force 强制重新注册 |
||
1 ignored issue
–
show
|
|||
127 | * @return Service|null |
||
128 | */ |
||
129 | 1 | public function register($service, bool $force = false) |
|
130 | { |
||
131 | 1 | $registered = $this->getService($service); |
|
132 | |||
133 | 1 | if ($registered && !$force) { |
|
134 | 1 | return $registered; |
|
135 | } |
||
136 | |||
137 | 1 | if (is_string($service)) { |
|
138 | 1 | $service = new $service($this); |
|
139 | } |
||
140 | |||
141 | 1 | if (method_exists($service, 'register')) { |
|
142 | 1 | $service->register(); |
|
143 | } |
||
144 | |||
145 | 1 | if (property_exists($service, 'bind')) { |
|
146 | 1 | $this->bind($service->bind); |
|
147 | } |
||
148 | |||
149 | 1 | $this->services[] = $service; |
|
150 | 1 | } |
|
151 | |||
152 | /** |
||
153 | * 执行服务 |
||
154 | * @access public |
||
155 | * @param Service $service 服务 |
||
1 ignored issue
–
show
|
|||
156 | * @return mixed |
||
157 | */ |
||
158 | 1 | public function bootService($service) |
|
159 | { |
||
160 | 1 | if (method_exists($service, 'boot')) { |
|
161 | 1 | return $this->invoke([$service, 'boot']); |
|
162 | } |
||
163 | 1 | } |
|
164 | |||
165 | /** |
||
166 | * 获取服务 |
||
167 | * @param string|Service $service |
||
1 ignored issue
–
show
|
|||
168 | * @return Service|null |
||
169 | */ |
||
170 | 1 | public function getService($service) |
|
171 | { |
||
172 | 1 | $name = is_string($service) ? $service : get_class($service); |
|
173 | return array_values(array_filter($this->services, function ($value) use ($name) { |
||
174 | 1 | return $value instanceof $name; |
|
175 | 1 | }, ARRAY_FILTER_USE_BOTH))[0] ?? null; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * 开启应用调试模式 |
||
180 | * @access public |
||
181 | * @param bool $debug 开启应用调试模式 |
||
1 ignored issue
–
show
|
|||
182 | * @return $this |
||
183 | */ |
||
184 | 2 | public function debug(bool $debug = true) |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * 是否为调试模式 |
||
192 | * @access public |
||
193 | * @return bool |
||
194 | */ |
||
195 | 7 | public function isDebug(): bool |
|
196 | { |
||
197 | 7 | return $this->appDebug; |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * 设置应用命名空间 |
||
202 | * @access public |
||
203 | * @param string $namespace 应用命名空间 |
||
1 ignored issue
–
show
|
|||
204 | * @return $this |
||
205 | */ |
||
206 | 7 | public function setNamespace(string $namespace) |
|
207 | { |
||
208 | 7 | $this->namespace = $namespace; |
|
209 | 7 | return $this; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * 获取应用类库命名空间 |
||
214 | * @access public |
||
215 | * @return string |
||
216 | */ |
||
217 | 1 | public function getNamespace(): string |
|
218 | { |
||
219 | 1 | return $this->namespace; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * 获取框架版本 |
||
224 | * @access public |
||
225 | * @return string |
||
226 | */ |
||
227 | 1 | public function version(): string |
|
228 | { |
||
229 | 1 | return static::VERSION; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * 获取应用根目录 |
||
234 | * @access public |
||
235 | * @return string |
||
236 | */ |
||
237 | 1 | public function getRootPath(): string |
|
238 | { |
||
239 | 1 | return $this->rootPath; |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * 获取应用基础目录 |
||
244 | * @access public |
||
245 | * @return string |
||
246 | */ |
||
247 | 10 | public function getBasePath(): string |
|
248 | { |
||
249 | 10 | return $this->rootPath . 'app' . DIRECTORY_SEPARATOR; |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * 获取当前应用目录 |
||
254 | * @access public |
||
255 | * @return string |
||
256 | */ |
||
257 | 7 | public function getAppPath(): string |
|
258 | { |
||
259 | 7 | return $this->appPath; |
|
260 | } |
||
261 | |||
262 | /** |
||
263 | * 设置应用目录 |
||
264 | * @param $path |
||
265 | */ |
||
266 | 6 | public function setAppPath($path) |
|
267 | { |
||
268 | 6 | $this->appPath = $path; |
|
269 | 6 | } |
|
270 | |||
271 | /** |
||
272 | * 获取应用运行时目录 |
||
273 | * @access public |
||
274 | * @return string |
||
275 | */ |
||
276 | 8 | public function getRuntimePath(): string |
|
277 | { |
||
278 | 8 | return $this->runtimePath; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * 设置runtime目录 |
||
283 | * @param $path |
||
284 | */ |
||
285 | 6 | public function setRuntimePath($path) |
|
286 | { |
||
287 | 6 | $this->runtimePath = $path; |
|
288 | 6 | } |
|
289 | |||
290 | /** |
||
291 | * 获取核心框架目录 |
||
292 | * @access public |
||
293 | * @return string |
||
294 | */ |
||
295 | 1 | public function getThinkPath(): string |
|
296 | { |
||
297 | 1 | return $this->thinkPath; |
|
298 | } |
||
299 | |||
300 | /** |
||
301 | * 获取应用配置目录 |
||
302 | * @access public |
||
303 | * @return string |
||
304 | */ |
||
305 | 10 | public function getConfigPath(): string |
|
306 | { |
||
307 | 10 | return $this->rootPath . 'config' . DIRECTORY_SEPARATOR; |
|
308 | } |
||
309 | |||
310 | /** |
||
311 | * 获取配置后缀 |
||
312 | * @access public |
||
313 | * @return string |
||
314 | */ |
||
315 | 9 | public function getConfigExt(): string |
|
316 | { |
||
317 | 9 | return $this->configExt; |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * 获取应用开启时间 |
||
322 | * @access public |
||
323 | * @return float |
||
324 | */ |
||
325 | 1 | public function getBeginTime(): float |
|
326 | { |
||
327 | 1 | return $this->beginTime; |
|
328 | } |
||
329 | |||
330 | /** |
||
331 | * 获取应用初始内存占用 |
||
332 | * @access public |
||
333 | * @return integer |
||
334 | */ |
||
335 | 1 | public function getBeginMem(): int |
|
336 | { |
||
337 | 1 | return $this->beginMem; |
|
338 | } |
||
339 | |||
340 | /** |
||
341 | * 初始化应用 |
||
342 | * @access public |
||
343 | * @return $this |
||
344 | */ |
||
345 | 1 | public function initialize() |
|
346 | { |
||
347 | 1 | $this->initialized = true; |
|
348 | |||
349 | 1 | $this->beginTime = microtime(true); |
|
350 | 1 | $this->beginMem = memory_get_usage(); |
|
351 | |||
352 | // 加载环境变量 |
||
353 | 1 | if (is_file($this->rootPath . '.env')) { |
|
354 | 1 | $this->env->load($this->rootPath . '.env'); |
|
355 | } |
||
356 | |||
357 | 1 | $this->configExt = $this->env->get('config_ext', '.php'); |
|
358 | |||
359 | // 加载全局初始化文件 |
||
360 | 1 | if (is_file($this->getRuntimePath() . 'init.php')) { |
|
361 | //直接加载缓存 |
||
362 | include $this->getRuntimePath() . 'init.php'; |
||
363 | } else { |
||
364 | 1 | $this->load(); |
|
365 | } |
||
366 | |||
367 | 1 | $this->debugModeInit(); |
|
368 | |||
369 | // 监听AppInit |
||
370 | 1 | $this->event->trigger('AppInit'); |
|
371 | |||
372 | 1 | date_default_timezone_set($this->config->get('app.default_timezone', 'Asia/Shanghai')); |
|
373 | |||
374 | // 初始化 |
||
375 | 1 | foreach ($this->initializers as $initializer) { |
|
376 | 1 | $this->make($initializer)->init($this); |
|
377 | } |
||
378 | |||
379 | 1 | return $this; |
|
380 | } |
||
381 | |||
382 | /** |
||
383 | * 是否初始化过 |
||
384 | * @return bool |
||
385 | */ |
||
386 | 1 | public function initialized() |
|
389 | } |
||
390 | |||
391 | /** |
||
392 | * 引导应用 |
||
393 | * @access public |
||
394 | * @return void |
||
395 | */ |
||
396 | 1 | public function boot(): void |
|
400 | 1 | }); |
|
401 | 1 | } |
|
402 | |||
403 | /** |
||
404 | * 加载应用文件和配置 |
||
405 | * @access protected |
||
406 | * @return void |
||
407 | */ |
||
408 | 1 | protected function load(): void |
|
409 | { |
||
410 | 1 | $appPath = $this->getAppPath(); |
|
411 | |||
412 | 1 | if (is_file($appPath . 'common.php')) { |
|
413 | 1 | include_once $appPath . 'common.php'; |
|
442 | } |
||
443 | } |
||
444 | 1 | } |
|
445 | |||
446 | /** |
||
447 | * 调试模式设置 |
||
448 | * @access protected |
||
449 | * @return void |
||
450 | */ |
||
451 | 1 | protected function debugModeInit(): void |
|
452 | { |
||
453 | // 应用调试模式 |
||
454 | 1 | if (!$this->appDebug) { |
|
455 | 1 | $this->appDebug = $this->env->get('app_debug') ? true : false; |
|
456 | 1 | ini_set('display_errors', 'Off'); |
|
457 | } |
||
458 | |||
459 | 1 | if (!$this->runningInConsole()) { |
|
460 | //重新申请一块比较大的buffer |
||
461 | if (ob_get_level() > 0) { |
||
462 | $output = ob_get_clean(); |
||
463 | } |
||
464 | ob_start(); |
||
465 | if (!empty($output)) { |
||
466 | echo $output; |
||
467 | } |
||
468 | } |
||
469 | 1 | } |
|
470 | |||
471 | /** |
||
472 | * 注册应用事件 |
||
473 | * @access protected |
||
474 | * @param array $event 事件数据 |
||
1 ignored issue
–
show
|
|||
475 | * @return void |
||
476 | */ |
||
477 | 2 | public function loadEvent(array $event): void |
|
478 | { |
||
479 | 2 | if (isset($event['bind'])) { |
|
480 | 2 | $this->event->bind($event['bind']); |
|
481 | } |
||
482 | |||
483 | 2 | if (isset($event['listen'])) { |
|
484 | 2 | $this->event->listenEvents($event['listen']); |
|
485 | } |
||
486 | |||
487 | 2 | if (isset($event['subscribe'])) { |
|
488 | 2 | $this->event->subscribe($event['subscribe']); |
|
489 | } |
||
490 | 2 | } |
|
491 | |||
492 | /** |
||
493 | * 解析应用类的类名 |
||
494 | * @access public |
||
495 | * @param string $layer 层名 controller model ... |
||
1 ignored issue
–
show
|
|||
496 | * @param string $name 类名 |
||
1 ignored issue
–
show
|
|||
497 | * @return string |
||
498 | */ |
||
499 | 1 | public function parseClass(string $layer, string $name): string |
|
500 | { |
||
501 | 1 | $name = str_replace(['/', '.'], '\\', $name); |
|
502 | 1 | $array = explode('\\', $name); |
|
503 | 1 | $class = self::parseName(array_pop($array), 1); |
|
504 | 1 | $path = $array ? implode('\\', $array) . '\\' : ''; |
|
505 | |||
506 | 1 | return $this->namespace . '\\' . $layer . '\\' . $path . $class; |
|
507 | } |
||
508 | |||
509 | /** |
||
510 | * 是否运行在命令行下 |
||
511 | * @return bool |
||
512 | */ |
||
513 | 1 | public function runningInConsole() |
|
516 | } |
||
517 | |||
518 | /** |
||
519 | * 获取应用根目录 |
||
520 | * @access protected |
||
521 | * @return string |
||
522 | */ |
||
523 | 18 | protected function getDefaultRootPath(): string |
|
524 | { |
||
525 | 18 | $path = dirname(dirname(dirname(dirname($this->thinkPath)))); |
|
526 | |||
527 | 18 | return $path . DIRECTORY_SEPARATOR; |
|
528 | } |
||
529 | |||
530 | /** |
||
531 | * 字符串命名风格转换 |
||
532 | * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格 |
||
533 | * @access public |
||
534 | * @param string $name 字符串 |
||
1 ignored issue
–
show
|
|||
535 | * @param integer $type 转换类型 |
||
1 ignored issue
–
show
|
|||
536 | * @param bool $ucfirst 首字母是否大写(驼峰规则) |
||
1 ignored issue
–
show
|
|||
537 | * @return string |
||
538 | */ |
||
539 | 13 | public static function parseName(string $name = null, int $type = 0, bool $ucfirst = true): string |
|
549 | } |
||
550 | |||
551 | /** |
||
552 | * 获取类名(不包含命名空间) |
||
553 | * @access public |
||
554 | * @param string|object $class |
||
1 ignored issue
–
show
|
|||
555 | * @return string |
||
556 | */ |
||
557 | 1 | public static function classBaseName($class): string |
|
558 | { |
||
559 | 1 | $class = is_object($class) ? get_class($class) : $class; |
|
560 | 1 | return basename(str_replace('\\', '/', $class)); |
|
561 | } |
||
562 | |||
563 | /** |
||
564 | * 创建工厂对象实例 |
||
565 | * @access public |
||
566 | * @param string $name 工厂类名 |
||
1 ignored issue
–
show
|
|||
567 | * @param string $namespace 默认命名空间 |
||
1 ignored issue
–
show
|
|||
568 | * @param array $args |
||
1 ignored issue
–
show
|
|||
569 | * @return mixed |
||
570 | */ |
||
571 | 1 | public static function factory(string $name, string $namespace = '', ...$args) |
|
580 | } |
||
581 | |||
582 | /** |
||
583 | * @param $data |
||
1 ignored issue
–
show
|
|||
584 | * @codeCoverageIgnore |
||
585 | * @return string |
||
1 ignored issue
–
show
|
|||
586 | */ |
||
587 | public static function serialize($data): string |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * @param string $data |
||
1 ignored issue
–
show
|
|||
598 | * @codeCoverageIgnore |
||
599 | * @return mixed|string |
||
1 ignored issue
–
show
|
|||
600 | */ |
||
601 | public static function unserialize(string $data) |
||
610 |