| Total Complexity | 66 |
| Total Lines | 578 |
| Duplicated Lines | 0 % |
| Coverage | 94.59% |
| 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.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 $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; |
|
|
1 ignored issue
–
show
|
|||
| 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) |
|
| 185 | { |
||
| 186 | 2 | $this->appDebug = $debug; |
|
| 187 | 2 | return $this; |
|
| 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() |
|
| 387 | { |
||
| 388 | 1 | return $this->initialized; |
|
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * 引导应用 |
||
| 393 | * @access public |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | 1 | public function boot(): void |
|
| 397 | { |
||
| 398 | array_walk($this->services, function ($service) { |
||
| 399 | 1 | $this->bootService($service); |
|
| 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'; |
|
| 414 | } |
||
| 415 | |||
| 416 | 1 | include $this->thinkPath . 'helper.php'; |
|
| 417 | |||
| 418 | 1 | $configPath = $this->getConfigPath(); |
|
| 419 | |||
| 420 | 1 | $files = []; |
|
| 421 | |||
| 422 | 1 | if (is_dir($configPath)) { |
|
| 423 | 1 | $files = glob($configPath . '*' . $this->configExt); |
|
| 424 | } |
||
| 425 | |||
| 426 | 1 | foreach ($files as $file) { |
|
| 427 | $this->config->load($file, pathinfo($file, PATHINFO_FILENAME)); |
||
| 428 | } |
||
| 429 | |||
| 430 | 1 | if (is_file($appPath . 'event.php')) { |
|
| 431 | 1 | $this->loadEvent(include $appPath . 'event.php'); |
|
| 432 | } |
||
| 433 | |||
| 434 | 1 | if (is_file($appPath . 'provider.php')) { |
|
| 435 | 1 | $this->bind(include $appPath . 'provider.php'); |
|
| 436 | } |
||
| 437 | 1 | } |
|
| 438 | |||
| 439 | /** |
||
| 440 | * 调试模式设置 |
||
| 441 | * @access protected |
||
| 442 | * @return void |
||
| 443 | */ |
||
| 444 | 1 | protected function debugModeInit(): void |
|
| 445 | { |
||
| 446 | // 应用调试模式 |
||
| 447 | 1 | if (!$this->appDebug) { |
|
| 448 | 1 | $this->appDebug = $this->env->get('app_debug') ? true : false; |
|
| 449 | } |
||
| 450 | |||
| 451 | 1 | if (!$this->appDebug) { |
|
| 452 | ini_set('display_errors', 'Off'); |
||
| 453 | 1 | } elseif (!$this->runningInConsole()) { |
|
| 454 | //重新申请一块比较大的buffer |
||
| 455 | if (ob_get_level() > 0) { |
||
| 456 | $output = ob_get_clean(); |
||
| 457 | } |
||
| 458 | ob_start(); |
||
| 459 | if (!empty($output)) { |
||
| 460 | echo $output; |
||
| 461 | } |
||
| 462 | } |
||
| 463 | 1 | } |
|
| 464 | |||
| 465 | /** |
||
| 466 | * 注册应用事件 |
||
| 467 | * @access protected |
||
| 468 | * @param array $event 事件数据 |
||
|
1 ignored issue
–
show
|
|||
| 469 | * @return void |
||
| 470 | */ |
||
| 471 | 2 | public function loadEvent(array $event): void |
|
| 472 | { |
||
| 473 | 2 | if (isset($event['bind'])) { |
|
| 474 | 2 | $this->event->bind($event['bind']); |
|
| 475 | } |
||
| 476 | |||
| 477 | 2 | if (isset($event['listen'])) { |
|
| 478 | 2 | $this->event->listenEvents($event['listen']); |
|
| 479 | } |
||
| 480 | |||
| 481 | 2 | if (isset($event['subscribe'])) { |
|
| 482 | 2 | $this->event->subscribe($event['subscribe']); |
|
| 483 | } |
||
| 484 | 2 | } |
|
| 485 | |||
| 486 | /** |
||
| 487 | * 解析应用类的类名 |
||
| 488 | * @access public |
||
| 489 | * @param string $layer 层名 controller model ... |
||
|
1 ignored issue
–
show
|
|||
| 490 | * @param string $name 类名 |
||
|
1 ignored issue
–
show
|
|||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | 1 | public function parseClass(string $layer, string $name): string |
|
| 494 | { |
||
| 495 | 1 | $name = str_replace(['/', '.'], '\\', $name); |
|
| 496 | 1 | $array = explode('\\', $name); |
|
| 497 | 1 | $class = self::parseName(array_pop($array), 1); |
|
| 498 | 1 | $path = $array ? implode('\\', $array) . '\\' : ''; |
|
| 499 | |||
| 500 | 1 | return $this->namespace . '\\' . $layer . '\\' . $path . $class; |
|
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * 是否运行在命令行下 |
||
| 505 | * @return bool |
||
| 506 | */ |
||
| 507 | 1 | public function runningInConsole() |
|
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * 获取应用根目录 |
||
| 514 | * @access protected |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | 18 | protected function getDefaultRootPath(): string |
|
| 518 | { |
||
| 519 | 18 | $path = dirname(dirname(dirname(dirname($this->thinkPath)))); |
|
| 520 | |||
| 521 | 18 | return $path . DIRECTORY_SEPARATOR; |
|
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * 字符串命名风格转换 |
||
| 526 | * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格 |
||
| 527 | * @access public |
||
| 528 | * @param string $name 字符串 |
||
|
1 ignored issue
–
show
|
|||
| 529 | * @param integer $type 转换类型 |
||
|
1 ignored issue
–
show
|
|||
| 530 | * @param bool $ucfirst 首字母是否大写(驼峰规则) |
||
|
1 ignored issue
–
show
|
|||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | 13 | public static function parseName(string $name = null, int $type = 0, bool $ucfirst = true): string |
|
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * 获取类名(不包含命名空间) |
||
| 547 | * @access public |
||
| 548 | * @param string|object $class |
||
|
1 ignored issue
–
show
|
|||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | 1 | public static function classBaseName($class): string |
|
| 552 | { |
||
| 553 | 1 | $class = is_object($class) ? get_class($class) : $class; |
|
| 554 | 1 | return basename(str_replace('\\', '/', $class)); |
|
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * 创建工厂对象实例 |
||
| 559 | * @access public |
||
| 560 | * @param string $name 工厂类名 |
||
|
1 ignored issue
–
show
|
|||
| 561 | * @param string $namespace 默认命名空间 |
||
|
1 ignored issue
–
show
|
|||
| 562 | * @param array $args |
||
|
1 ignored issue
–
show
|
|||
| 563 | * @return mixed |
||
| 564 | */ |
||
| 565 | 1 | public static function factory(string $name, string $namespace = '', ...$args) |
|
| 566 | { |
||
| 567 | 1 | $class = false !== strpos($name, '\\') ? $name : $namespace . ucwords($name); |
|
| 568 | |||
| 569 | 1 | if (class_exists($class)) { |
|
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param $data |
||
|
1 ignored issue
–
show
|
|||
| 578 | * @codeCoverageIgnore |
||
| 579 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 580 | */ |
||
| 581 | public static function serialize($data): string |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param string $data |
||
|
1 ignored issue
–
show
|
|||
| 592 | * @codeCoverageIgnore |
||
| 593 | * @return mixed|string |
||
|
1 ignored issue
–
show
|
|||
| 594 | */ |
||
| 595 | public static function unserialize(string $data) |
||
| 604 |