Total Complexity | 67 |
Total Lines | 589 |
Duplicated Lines | 0 % |
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 = '5.2.0RC1'; |
||
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 $rootNamespace = 'app'; |
||
51 | |||
52 | /** |
||
53 | * 当前应用类库命名空间 |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $namespace = 'app'; |
||
57 | |||
58 | /** |
||
59 | * 应用根目录 |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $rootPath = ''; |
||
63 | |||
64 | /** |
||
65 | * 框架目录 |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $thinkPath = ''; |
||
69 | |||
70 | /** |
||
71 | * 应用目录 |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $appPath = ''; |
||
75 | |||
76 | /** |
||
77 | * Runtime目录 |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $runtimePath = ''; |
||
81 | |||
82 | /** |
||
83 | * 配置后缀 |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $configExt = '.php'; |
||
87 | |||
88 | /** |
||
89 | * 应用初始化器 |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $initializers = [ |
||
93 | Error::class, |
||
94 | RegisterService::class, |
||
95 | BootService::class, |
||
96 | ]; |
||
97 | |||
98 | /** |
||
99 | * 注册的系统服务 |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $services = []; |
||
103 | |||
104 | /** |
||
105 | * 初始化 |
||
106 | * @var bool |
||
107 | */ |
||
108 | protected $initialized = false; |
||
109 | |||
110 | /** |
||
111 | * 架构方法 |
||
112 | * @access public |
||
113 | * @param string $rootPath 应用根目录 |
||
114 | */ |
||
115 | public function __construct(string $rootPath = '') |
||
116 | { |
||
117 | $this->thinkPath = dirname(__DIR__) . DIRECTORY_SEPARATOR; |
||
118 | $this->rootPath = $rootPath ? realpath($rootPath) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath(); |
||
119 | $this->appPath = $this->rootPath . 'app' . DIRECTORY_SEPARATOR; |
||
120 | $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR; |
||
121 | |||
122 | static::setInstance($this); |
||
123 | |||
124 | $this->instance('app', $this); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * 注册服务 |
||
129 | * @access public |
||
130 | * @param Service|string $service 服务 |
||
131 | * @param bool $force 强制重新注册 |
||
132 | * @return Service|null |
||
133 | */ |
||
134 | public function register($service, bool $force = false) |
||
135 | { |
||
136 | $registered = $this->getService($service); |
||
137 | |||
138 | if ($registered && !$force) { |
||
139 | return $registered; |
||
140 | } |
||
141 | |||
142 | if (is_string($service)) { |
||
143 | $service = new $service($this); |
||
144 | } |
||
145 | |||
146 | if (method_exists($service, 'register')) { |
||
147 | $service->register(); |
||
148 | } |
||
149 | |||
150 | $this->services[] = $service; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * 执行服务 |
||
155 | * @access public |
||
156 | * @param Service $service 服务 |
||
157 | * @return mixed |
||
158 | */ |
||
159 | public function bootService($service) |
||
160 | { |
||
161 | if (method_exists($service, 'boot')) { |
||
162 | return $this->invoke([$service, 'boot']); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * 获取服务 |
||
168 | * @param string|Service $service |
||
169 | * @return Service|null |
||
170 | */ |
||
171 | public function getService($service) |
||
172 | { |
||
173 | $name = is_string($service) ? $service : get_class($service); |
||
174 | return array_values(array_filter($this->services, function ($value) use ($name) { |
||
175 | return $value instanceof $name; |
||
176 | }, ARRAY_FILTER_USE_BOTH))[0] ?? null; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * 开启应用调试模式 |
||
181 | * @access public |
||
182 | * @param bool $debug 开启应用调试模式 |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function debug(bool $debug = true) |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * 是否为调试模式 |
||
193 | * @access public |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function isDebug(): bool |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * 设置应用命名空间 |
||
203 | * @access public |
||
204 | * @param string $namespace 应用命名空间 |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function setNamespace(string $namespace) |
||
208 | { |
||
209 | $this->namespace = $namespace; |
||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * 设置应用根命名空间 |
||
215 | * @access public |
||
216 | * @param string $rootNamespace 应用命名空间 |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function setRootNamespace(string $rootNamespace) |
||
220 | { |
||
221 | $this->rootNamespace = $rootNamespace; |
||
222 | return $this; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * 获取应用类基础命名空间 |
||
227 | * @access public |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getRootNamespace(): string |
||
231 | { |
||
232 | return $this->rootNamespace; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * 获取应用类库命名空间 |
||
237 | * @access public |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getNamespace(): string |
||
241 | { |
||
242 | return $this->namespace; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * 获取框架版本 |
||
247 | * @access public |
||
248 | * @return string |
||
249 | */ |
||
250 | public function version(): string |
||
251 | { |
||
252 | return static::VERSION; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * 获取应用根目录 |
||
257 | * @access public |
||
258 | * @return string |
||
259 | */ |
||
260 | public function getRootPath(): string |
||
261 | { |
||
262 | return $this->rootPath; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * 获取应用基础目录 |
||
267 | * @access public |
||
268 | * @return string |
||
269 | */ |
||
270 | public function getBasePath(): string |
||
271 | { |
||
272 | return $this->rootPath . 'app' . DIRECTORY_SEPARATOR; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * 获取当前应用目录 |
||
277 | * @access public |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getAppPath(): string |
||
281 | { |
||
282 | return $this->appPath; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * 设置应用目录 |
||
287 | * @param $path |
||
288 | */ |
||
289 | public function setAppPath($path) |
||
290 | { |
||
291 | $this->appPath = $path; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * 获取应用运行时目录 |
||
296 | * @access public |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getRuntimePath(): string |
||
300 | { |
||
301 | return $this->runtimePath; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * 设置runtime目录 |
||
306 | * @param $path |
||
307 | */ |
||
308 | public function setRuntimePath($path) |
||
309 | { |
||
310 | $this->runtimePath = $path; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * 获取核心框架目录 |
||
315 | * @access public |
||
316 | * @return string |
||
317 | */ |
||
318 | public function getThinkPath(): string |
||
319 | { |
||
320 | return $this->thinkPath; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * 获取应用配置目录 |
||
325 | * @access public |
||
326 | * @return string |
||
327 | */ |
||
328 | public function getConfigPath(): string |
||
329 | { |
||
330 | return $this->rootPath . 'config' . DIRECTORY_SEPARATOR; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * 获取配置后缀 |
||
335 | * @access public |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getConfigExt(): string |
||
339 | { |
||
340 | return $this->configExt; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * 获取应用开启时间 |
||
345 | * @access public |
||
346 | * @return float |
||
347 | */ |
||
348 | public function getBeginTime(): float |
||
349 | { |
||
350 | return $this->beginTime; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * 获取应用初始内存占用 |
||
355 | * @access public |
||
356 | * @return integer |
||
357 | */ |
||
358 | public function getBeginMem(): int |
||
359 | { |
||
360 | return $this->beginMem; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * 初始化应用 |
||
365 | * @access public |
||
366 | * @return $this |
||
367 | */ |
||
368 | public function initialize() |
||
369 | { |
||
370 | if ($this->initialized) { |
||
371 | return $this; |
||
372 | } |
||
373 | |||
374 | $this->initialized = true; |
||
375 | |||
376 | $this->beginTime = microtime(true); |
||
377 | $this->beginMem = memory_get_usage(); |
||
378 | |||
379 | // 加载环境变量 |
||
380 | if (is_file($this->rootPath . '.env')) { |
||
381 | $this->env->load($this->rootPath . '.env'); |
||
382 | } |
||
383 | |||
384 | $this->configExt = $this->env->get('config_ext', '.php'); |
||
385 | |||
386 | // 加载全局初始化文件 |
||
387 | if (is_file($this->getRuntimePath() . 'init.php')) { |
||
388 | //直接加载缓存 |
||
389 | include $this->getRuntimePath() . 'init.php'; |
||
390 | } else { |
||
391 | $this->load(); |
||
392 | } |
||
393 | |||
394 | $this->debugModeInit(); |
||
395 | |||
396 | // 监听AppInit |
||
397 | $this->app->event->trigger('AppInit'); |
||
398 | |||
399 | date_default_timezone_set($this->config->get('app.default_timezone', 'Asia/Shanghai')); |
||
400 | |||
401 | // 初始化 |
||
402 | foreach ($this->initializers as $initializer) { |
||
403 | $this->make($initializer)->init($this); |
||
404 | } |
||
405 | |||
406 | return $this; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * 引导应用 |
||
411 | * @access public |
||
412 | * @return void |
||
413 | */ |
||
414 | public function boot(): void |
||
415 | { |
||
416 | array_walk($this->services, function ($service) { |
||
417 | $this->bootService($service); |
||
418 | }); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * 加载应用文件和配置 |
||
423 | * @access protected |
||
424 | * @return void |
||
425 | */ |
||
426 | protected function load(): void |
||
427 | { |
||
428 | $appPath = $this->getAppPath(); |
||
429 | |||
430 | if (is_file($appPath . 'common.php')) { |
||
431 | include_once $appPath . 'common.php'; |
||
432 | } |
||
433 | |||
434 | include $this->thinkPath . 'helper.php'; |
||
435 | |||
436 | $configPath = $this->getConfigPath(); |
||
437 | |||
438 | $files = []; |
||
439 | |||
440 | if (is_dir($configPath)) { |
||
441 | $files = glob($configPath . '*' . $this->configExt); |
||
442 | } |
||
443 | |||
444 | foreach ($files as $file) { |
||
445 | $this->config->load($file, pathinfo($file, PATHINFO_FILENAME)); |
||
446 | } |
||
447 | |||
448 | if (is_file($appPath . 'event.php')) { |
||
449 | $this->loadEvent(include $appPath . 'event.php'); |
||
450 | } |
||
451 | |||
452 | if (is_file($appPath . 'middleware.php')) { |
||
453 | $this->middleware->import(include $appPath . 'middleware.php'); |
||
454 | } |
||
455 | |||
456 | if (is_file($appPath . 'provider.php')) { |
||
457 | $this->bind(include $appPath . 'provider.php'); |
||
458 | } |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * 调试模式设置 |
||
463 | * @access protected |
||
464 | * @return void |
||
465 | */ |
||
466 | protected function debugModeInit(): void |
||
467 | { |
||
468 | // 应用调试模式 |
||
469 | if (!$this->appDebug) { |
||
470 | $this->appDebug = $this->env->get('app_debug', false); |
||
471 | } |
||
472 | |||
473 | if (!$this->appDebug) { |
||
474 | ini_set('display_errors', 'Off'); |
||
475 | } elseif (!$this->runningInConsole()) { |
||
476 | //重新申请一块比较大的buffer |
||
477 | if (ob_get_level() > 0) { |
||
478 | $output = ob_get_clean(); |
||
479 | } |
||
480 | ob_start(); |
||
481 | if (!empty($output)) { |
||
482 | echo $output; |
||
483 | } |
||
484 | } |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * 注册应用事件 |
||
489 | * @access protected |
||
490 | * @param array $event 事件数据 |
||
491 | * @return void |
||
492 | */ |
||
493 | public function loadEvent(array $event): void |
||
494 | { |
||
495 | if (isset($event['bind'])) { |
||
496 | $this->event->bind($event['bind']); |
||
497 | } |
||
498 | |||
499 | if (isset($event['listen'])) { |
||
500 | $this->event->listenEvents($event['listen']); |
||
501 | } |
||
502 | |||
503 | if (isset($event['subscribe'])) { |
||
504 | $this->event->subscribe($event['subscribe']); |
||
505 | } |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * 解析应用类的类名 |
||
510 | * @access public |
||
511 | * @param string $layer 层名 controller model ... |
||
512 | * @param string $name 类名 |
||
513 | * @return string |
||
514 | */ |
||
515 | public function parseClass(string $layer, string $name): string |
||
516 | { |
||
517 | $name = str_replace(['/', '.'], '\\', $name); |
||
518 | $array = explode('\\', $name); |
||
519 | $class = self::parseName(array_pop($array), 1); |
||
520 | $path = $array ? implode('\\', $array) . '\\' : ''; |
||
521 | |||
522 | return $this->namespace . '\\' . $layer . '\\' . $path . $class; |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * 是否运行在命令行下 |
||
527 | * @return bool |
||
528 | */ |
||
529 | public function runningInConsole() |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * 获取应用根目录 |
||
536 | * @access protected |
||
537 | * @return string |
||
538 | */ |
||
539 | protected function getDefaultRootPath(): string |
||
540 | { |
||
541 | $path = dirname(dirname(dirname(dirname($this->thinkPath)))); |
||
542 | |||
543 | return $path . DIRECTORY_SEPARATOR; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * 字符串命名风格转换 |
||
548 | * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格 |
||
549 | * @access public |
||
550 | * @param string $name 字符串 |
||
551 | * @param integer $type 转换类型 |
||
552 | * @param bool $ucfirst 首字母是否大写(驼峰规则) |
||
553 | * @return string |
||
554 | */ |
||
555 | public static function parseName(string $name = null, int $type = 0, bool $ucfirst = true): string |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * 获取类名(不包含命名空间) |
||
569 | * @access public |
||
570 | * @param string|object $class |
||
571 | * @return string |
||
572 | */ |
||
573 | public static function classBaseName($class): string |
||
574 | { |
||
575 | $class = is_object($class) ? get_class($class) : $class; |
||
576 | return basename(str_replace('\\', '/', $class)); |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * 创建工厂对象实例 |
||
581 | * @access public |
||
582 | * @param string $name 工厂类名 |
||
583 | * @param string $namespace 默认命名空间 |
||
584 | * @return mixed |
||
585 | */ |
||
586 | public static function factory(string $name, string $namespace = '', ...$args) |
||
595 | } |
||
596 | |||
597 | public static function serialize($data): string |
||
604 | } |
||
605 | |||
606 | public static function unserialize(string $data) |
||
607 | { |
||
608 | SerializableClosure::enterContext(); |
||
609 | $data = \unserialize($data); |
||
610 | SerializableClosure::unwrapClosures($data); |
||
611 | SerializableClosure::exitContext(); |
||
612 | return $data; |
||
613 | } |
||
614 | } |
||
615 |