Total Complexity | 94 |
Total Lines | 704 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Base 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 Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | abstract class Base extends Container |
||
40 | { |
||
41 | /** |
||
42 | * 应用名称 |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $name; |
||
46 | |||
47 | /** |
||
48 | * 应用调试模式 |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $appDebug = true; |
||
52 | |||
53 | /** |
||
54 | * 是否多应用模式 |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $multi = false; |
||
58 | |||
59 | /** |
||
60 | * 是否自动多应用 |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected $auto = false; |
||
64 | |||
65 | /** |
||
66 | * 应用映射 |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $map = []; |
||
70 | |||
71 | /** |
||
72 | * 应用开始时间 |
||
73 | * @var float |
||
74 | */ |
||
75 | protected $beginTime; |
||
76 | |||
77 | /** |
||
78 | * 应用内存初始占用 |
||
79 | * @var integer |
||
80 | */ |
||
81 | protected $beginMem; |
||
82 | |||
83 | /** |
||
84 | * 应用类库顶级命名空间 |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $rootNamespace = 'app'; |
||
88 | |||
89 | /** |
||
90 | * 当前应用类库命名空间 |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $namespace = ''; |
||
94 | |||
95 | /** |
||
96 | * 应用根目录 |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $rootPath = ''; |
||
100 | |||
101 | /** |
||
102 | * 框架目录 |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $thinkPath = ''; |
||
106 | |||
107 | /** |
||
108 | * 应用基础目录 |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $basePath = ''; |
||
112 | |||
113 | /** |
||
114 | * 应用类库目录 |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $appPath = ''; |
||
118 | |||
119 | /** |
||
120 | * 运行时目录 |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $runtimePath = ''; |
||
124 | |||
125 | /** |
||
126 | * 配置目录 |
||
127 | * @var string |
||
128 | */ |
||
129 | protected $configPath = ''; |
||
130 | |||
131 | /** |
||
132 | * 路由目录 |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $routePath = ''; |
||
136 | |||
137 | /** |
||
138 | * URL |
||
139 | * @var string |
||
140 | */ |
||
141 | protected $urlPath = ''; |
||
142 | |||
143 | /** |
||
144 | * 配置后缀 |
||
145 | * @var string |
||
146 | */ |
||
147 | protected $configExt = '.php'; |
||
148 | |||
149 | /** |
||
150 | * 是否需要事件响应 |
||
151 | * @var bool |
||
152 | */ |
||
153 | protected $withEvent = true; |
||
154 | |||
155 | /** |
||
156 | * 设置是否使用事件机制 |
||
157 | * @access public |
||
158 | * @param bool $event |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function withEvent(bool $event) |
||
162 | { |
||
163 | $this->withEvent = $event; |
||
164 | return $this; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * 设置应用路径 |
||
169 | * @access public |
||
170 | * @param string $path 应用目录 |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function path(string $path) |
||
174 | { |
||
175 | $this->appPath = $path; |
||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * 开启应用调试模式 |
||
181 | * @access public |
||
182 | * @param bool $debug 开启应用调试模式 |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function debug(bool $debug = true) |
||
186 | { |
||
187 | $this->appDebug = $debug; |
||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * 是否为调试模式 |
||
193 | * @access public |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function isDebug(): bool |
||
197 | { |
||
198 | return $this->appDebug; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * 设置应用名称 |
||
203 | * @access public |
||
204 | * @param string $name 应用名称 |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function name(string $name) |
||
208 | { |
||
209 | $this->name = $name; |
||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * 设置应用命名空间 |
||
215 | * @access public |
||
216 | * @param string $namespace 应用命名空间 |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function setNamespace(string $namespace) |
||
220 | { |
||
221 | $this->namespace = $namespace; |
||
222 | return $this; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * 设置应用根命名空间 |
||
227 | * @access public |
||
228 | * @param string $rootNamespace 应用命名空间 |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function setRootNamespace(string $rootNamespace) |
||
232 | { |
||
233 | $this->rootNamespace = $rootNamespace; |
||
234 | return $this; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * 获取框架版本 |
||
239 | * @access public |
||
240 | * @return string |
||
241 | */ |
||
242 | public function version(): string |
||
243 | { |
||
244 | return static::VERSION; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * 获取应用名称 |
||
249 | * @access public |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getName(): string |
||
253 | { |
||
254 | return $this->name ?: ''; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * 获取应用根目录 |
||
259 | * @access public |
||
260 | * @return string |
||
261 | */ |
||
262 | public function getRootPath(): string |
||
263 | { |
||
264 | return $this->rootPath; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * 获取应用基础目录 |
||
269 | * @access public |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getBasePath(): string |
||
273 | { |
||
274 | return $this->basePath; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * 获取当前应用目录 |
||
279 | * @access public |
||
280 | * @return string |
||
281 | */ |
||
282 | public function getAppPath(): string |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * 获取应用运行时目录 |
||
289 | * @access public |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getRuntimePath(): string |
||
293 | { |
||
294 | return $this->runtimePath; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * 获取核心框架目录 |
||
299 | * @access public |
||
300 | * @return string |
||
301 | */ |
||
302 | public function getThinkPath(): string |
||
303 | { |
||
304 | return $this->thinkPath; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * 获取路由目录 |
||
309 | * @access public |
||
310 | * @return string |
||
311 | */ |
||
312 | public function getRoutePath(): string |
||
313 | { |
||
314 | return $this->routePath; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * 获取应用配置目录 |
||
319 | * @access public |
||
320 | * @return string |
||
321 | */ |
||
322 | public function getConfigPath(): string |
||
323 | { |
||
324 | return $this->configPath; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * 获取配置后缀 |
||
329 | * @access public |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getConfigExt(): string |
||
333 | { |
||
334 | return $this->configExt; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * 获取应用类基础命名空间 |
||
339 | * @access public |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getRootNamespace(): string |
||
343 | { |
||
344 | return $this->rootNamespace; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * 获取应用类库命名空间 |
||
349 | * @access public |
||
350 | * @return string |
||
351 | */ |
||
352 | public function getNamespace(): string |
||
353 | { |
||
354 | return $this->namespace; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * 获取应用开启时间 |
||
359 | * @access public |
||
360 | * @return float |
||
361 | */ |
||
362 | public function getBeginTime(): float |
||
363 | { |
||
364 | return $this->beginTime; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * 获取应用初始内存占用 |
||
369 | * @access public |
||
370 | * @return integer |
||
371 | */ |
||
372 | public function getBeginMem(): int |
||
373 | { |
||
374 | return $this->beginMem; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * 初始化应用 |
||
379 | * @access public |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function initialize() |
||
383 | { |
||
384 | $this->beginTime = microtime(true); |
||
385 | $this->beginMem = memory_get_usage(); |
||
386 | |||
387 | $this->parse(); |
||
388 | |||
389 | $this->init(); |
||
390 | |||
391 | return $this; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * 分析应用(参数) |
||
396 | * @access protected |
||
397 | * @return void |
||
398 | */ |
||
399 | protected function parse(): void |
||
400 | { |
||
401 | if (is_file($this->rootPath . '.env')) { |
||
402 | $this->env->load($this->rootPath . '.env'); |
||
403 | } |
||
404 | |||
405 | $this->parseAppName(); |
||
406 | |||
407 | $this->parsePath(); |
||
408 | |||
409 | if (!$this->namespace) { |
||
410 | $this->namespace = $this->multi ? $this->rootNamespace . '\\' . $this->name : $this->rootNamespace; |
||
411 | } |
||
412 | |||
413 | $this->configExt = $this->env->get('config_ext', '.php'); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * 分析当前请求的应用名 |
||
418 | * @access protected |
||
419 | * @return void |
||
420 | */ |
||
421 | protected function parseAppName(): void |
||
422 | { |
||
423 | $this->urlPath = $this->request->path(); |
||
424 | |||
425 | if ($this->auto && $this->urlPath) { |
||
426 | // 自动多应用识别 |
||
427 | $name = current(explode('/', $this->urlPath)); |
||
428 | |||
429 | if (isset($this->map[$name])) { |
||
430 | if ($this->map[$name] instanceof \Closure) { |
||
431 | call_user_func_array($this->map[$name], [$this]); |
||
432 | } else { |
||
433 | $this->name = $this->map[$name]; |
||
434 | } |
||
435 | } elseif ($name && false !== array_search($name, $this->map)) { |
||
436 | throw new HttpException(404, 'app not exists:' . $name); |
||
437 | } else { |
||
438 | $this->name = $name ?: $this->defaultApp; |
||
439 | } |
||
440 | } elseif ($this->multi) { |
||
441 | $this->name = $this->name ?: $this->getScriptName(); |
||
442 | } |
||
443 | |||
444 | $this->request->setApp($this->name ?: ''); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * 分析应用路径 |
||
449 | * @access protected |
||
450 | * @return void |
||
451 | */ |
||
452 | protected function parsePath(): void |
||
453 | { |
||
454 | if ($this->multi) { |
||
455 | $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR . $this->name . DIRECTORY_SEPARATOR; |
||
456 | $this->routePath = $this->rootPath . 'route' . DIRECTORY_SEPARATOR . $this->name . DIRECTORY_SEPARATOR; |
||
457 | } else { |
||
458 | $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR; |
||
459 | $this->routePath = $this->rootPath . 'route' . DIRECTORY_SEPARATOR; |
||
460 | } |
||
461 | |||
462 | if (!$this->appPath) { |
||
463 | $this->appPath = $this->multi ? $this->basePath . $this->name . DIRECTORY_SEPARATOR : $this->basePath; |
||
464 | } |
||
465 | |||
466 | $this->configPath = $this->rootPath . 'config' . DIRECTORY_SEPARATOR; |
||
467 | |||
468 | $this->env->set([ |
||
469 | 'think_path' => $this->thinkPath, |
||
470 | 'root_path' => $this->rootPath, |
||
471 | 'app_path' => $this->appPath, |
||
472 | 'runtime_path' => $this->runtimePath, |
||
473 | 'route_path' => $this->routePath, |
||
474 | 'config_path' => $this->configPath, |
||
475 | ]); |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * 初始化应用 |
||
480 | * @access public |
||
481 | * @return void |
||
482 | */ |
||
483 | public function init(): void |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * 加载应用文件和配置 |
||
509 | * @access protected |
||
510 | * @return void |
||
511 | */ |
||
512 | protected function load(): void |
||
513 | { |
||
514 | if ($this->multi && is_file($this->basePath . 'event.php')) { |
||
515 | $this->loadEvent(include $this->basePath . 'event.php'); |
||
516 | } |
||
517 | |||
518 | if (is_file($this->appPath . 'event.php')) { |
||
519 | $this->loadEvent(include $this->appPath . 'event.php'); |
||
520 | } |
||
521 | |||
522 | if ($this->multi && is_file($this->basePath . 'common.php')) { |
||
523 | include_once $this->basePath . 'common.php'; |
||
524 | } |
||
525 | |||
526 | if (is_file($this->appPath . 'common.php')) { |
||
527 | include_once $this->appPath . 'common.php'; |
||
528 | } |
||
529 | |||
530 | include $this->thinkPath . 'helper.php'; |
||
531 | |||
532 | if ($this->multi && is_file($this->basePath . 'middleware.php')) { |
||
533 | $this->middleware->import(include $this->basePath . 'middleware.php'); |
||
534 | } |
||
535 | |||
536 | if (is_file($this->appPath . 'middleware.php')) { |
||
537 | $this->middleware->import(include $this->appPath . 'middleware.php'); |
||
538 | } |
||
539 | |||
540 | if ($this->multi && is_file($this->basePath . 'provider.php')) { |
||
541 | $this->bind(include $this->basePath . 'provider.php'); |
||
542 | } |
||
543 | |||
544 | if (is_file($this->appPath . 'provider.php')) { |
||
545 | $this->bind(include $this->appPath . 'provider.php'); |
||
546 | } |
||
547 | |||
548 | $files = []; |
||
549 | |||
550 | if (is_dir($this->configPath)) { |
||
551 | $files = glob($this->configPath . '*' . $this->configExt); |
||
552 | } |
||
553 | |||
554 | if ($this->multi) { |
||
555 | if (is_dir($this->appPath . 'config')) { |
||
556 | $files = array_merge($files, glob($this->appPath . 'config' . DIRECTORY_SEPARATOR . '*' . $this->configExt)); |
||
557 | } elseif (is_dir($this->configPath . $this->name)) { |
||
558 | $files = array_merge($files, glob($this->configPath . $this->name . DIRECTORY_SEPARATOR . '*' . $this->configExt)); |
||
559 | } |
||
560 | } |
||
561 | |||
562 | foreach ($files as $file) { |
||
563 | $this->config->load($file, pathinfo($file, PATHINFO_FILENAME)); |
||
564 | } |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * 调试模式设置 |
||
569 | * @access protected |
||
570 | * @return void |
||
571 | */ |
||
572 | protected function debugModeInit(): void |
||
573 | { |
||
574 | // 应用调试模式 |
||
575 | if (!$this->appDebug) { |
||
576 | $this->appDebug = $this->env->get('app_debug', false); |
||
577 | } |
||
578 | |||
579 | if (!$this->appDebug) { |
||
580 | ini_set('display_errors', 'Off'); |
||
581 | } elseif (PHP_SAPI != 'cli') { |
||
582 | //重新申请一块比较大的buffer |
||
583 | if (ob_get_level() > 0) { |
||
584 | $output = ob_get_clean(); |
||
585 | } |
||
586 | ob_start(); |
||
587 | if (!empty($output)) { |
||
588 | echo $output; |
||
589 | } |
||
590 | } |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * 注册应用事件 |
||
595 | * @access protected |
||
596 | * @return void |
||
597 | */ |
||
598 | protected function loadEvent(array $event): void |
||
599 | { |
||
600 | if (isset($event['bind'])) { |
||
601 | $this->event->bind($event['bind']); |
||
602 | } |
||
603 | |||
604 | if (isset($event['listen'])) { |
||
605 | $this->event->listenEvents($event['listen']); |
||
606 | } |
||
607 | |||
608 | if (isset($event['subscribe'])) { |
||
609 | $this->event->subscribe($event['subscribe']); |
||
610 | } |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * 获取自动多应用模式下的实际URL Path |
||
615 | * @access public |
||
616 | * @return string |
||
617 | */ |
||
618 | public function getRealPath(): string |
||
619 | { |
||
620 | $path = $this->urlPath; |
||
621 | |||
622 | if ($path && $this->auto) { |
||
623 | $path = substr_replace($path, '', 0, strpos($path, '/') ? strpos($path, '/') + 1 : strlen($path)); |
||
624 | } |
||
625 | |||
626 | return $path; |
||
627 | } |
||
628 | |||
629 | abstract public function run(); |
||
630 | |||
631 | /** |
||
632 | * 解析应用类的类名 |
||
633 | * @access public |
||
634 | * @param string $layer 层名 controller model ... |
||
635 | * @param string $name 类名 |
||
636 | * @return string |
||
637 | */ |
||
638 | public function parseClass(string $layer, string $name): string |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * 获取应用根目录 |
||
650 | * @access protected |
||
651 | * @return string |
||
652 | */ |
||
653 | protected function getDefaultRootPath(): string |
||
654 | { |
||
655 | $path = dirname(dirname(dirname(dirname($this->thinkPath)))); |
||
656 | |||
657 | return $path . DIRECTORY_SEPARATOR; |
||
658 | } |
||
659 | |||
660 | /** |
||
661 | * 获取当前运行入口名称 |
||
662 | * @access protected |
||
663 | * @return string |
||
664 | */ |
||
665 | protected function getScriptName(): string |
||
666 | { |
||
667 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
668 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
669 | } elseif (isset($_SERVER['argv'][0])) { |
||
670 | $file = realpath($_SERVER['argv'][0]); |
||
671 | } |
||
672 | |||
673 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : $this->defaultApp; |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * 字符串命名风格转换 |
||
678 | * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格 |
||
679 | * @access public |
||
680 | * @param string $name 字符串 |
||
681 | * @param integer $type 转换类型 |
||
682 | * @param bool $ucfirst 首字母是否大写(驼峰规则) |
||
683 | * @return string |
||
684 | */ |
||
685 | public static function parseName(string $name = null, int $type = 0, bool $ucfirst = true): string |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * 获取类名(不包含命名空间) |
||
699 | * @access public |
||
700 | * @param string|object $class |
||
701 | * @return string |
||
702 | */ |
||
703 | public static function classBaseName($class): string |
||
704 | { |
||
705 | $class = is_object($class) ? get_class($class) : $class; |
||
706 | return basename(str_replace('\\', '/', $class)); |
||
707 | } |
||
708 | |||
709 | /** |
||
710 | * 创建工厂对象实例 |
||
711 | * @access public |
||
712 | * @param string $name 工厂类名 |
||
713 | * @param string $namespace 默认命名空间 |
||
714 | * @return mixed |
||
715 | */ |
||
716 | public static function factory(string $name, string $namespace = '', ...$args) |
||
717 | { |
||
718 | $class = false !== strpos($name, '\\') ? $name : $namespace . ucwords($name); |
||
719 | |||
720 | if (class_exists($class)) { |
||
721 | return Container::getInstance()->invokeClass($class, $args); |
||
722 | } |
||
723 | |||
724 | throw new ClassNotFoundException('class not exists:' . $class, $class); |
||
725 | } |
||
726 | |||
727 | public static function serialize($data): string |
||
728 | { |
||
729 | SerializableClosure::enterContext(); |
||
730 | SerializableClosure::wrapClosures($data); |
||
731 | $data = \serialize($data); |
||
732 | SerializableClosure::exitContext(); |
||
733 | return $data; |
||
734 | } |
||
735 | |||
736 | public static function unserialize(string $data) |
||
743 | } |
||
744 | } |
||
745 |