| Total Complexity | 48 |
| Total Lines | 505 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 66 | class Application extends Container |
||
| 67 | { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The application version |
||
| 71 | */ |
||
| 72 | public const VERSION = '1.0.0-dev'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The event dispatcher instance |
||
| 76 | * @var DispatcherInterface |
||
| 77 | */ |
||
| 78 | protected DispatcherInterface $dispatcher; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The base path for this application |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected string $basePath = ''; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The vendor path |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected string $vendorPath = ''; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The Application path |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected string $appPath = ''; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The application root path |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected string $rootPath = ''; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The application configuration path |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | protected string $configPath = 'config'; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The application storage path |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected string $storagePath = 'storage'; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The custom app name space |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | protected string $namespace = ''; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * The list of service providers |
||
| 124 | * @var array<string, ServiceProvider> |
||
| 125 | */ |
||
| 126 | protected array $providers = []; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Whether the system already booted |
||
| 130 | * @var bool |
||
| 131 | */ |
||
| 132 | protected bool $booted = false; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The application environment |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | protected string $env = 'dev'; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Create new instance |
||
| 142 | * @param string $basePath |
||
| 143 | */ |
||
| 144 | public function __construct(string $basePath = '') |
||
| 145 | { |
||
| 146 | parent::__construct(); |
||
| 147 | |||
| 148 | $this->basePath = $basePath; |
||
| 149 | $this->loadCoreServiceProviders(); |
||
| 150 | |||
| 151 | $this->dispatcher = $this->get(DispatcherInterface::class); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return the application version |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function version(): string |
||
| 159 | { |
||
| 160 | return self::VERSION; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Return the root path |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getRootPath(): string |
||
| 168 | { |
||
| 169 | return $this->rootPath; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set root path |
||
| 174 | * @param string $rootPath |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function setRootPath(string $rootPath): self |
||
| 178 | { |
||
| 179 | $this->rootPath = $rootPath; |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Return the application name space |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getNamespace(): string |
||
| 189 | { |
||
| 190 | return $this->namespace; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Set the application name space |
||
| 195 | * @param string $namespace |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setNamespace(string $namespace): self |
||
| 199 | { |
||
| 200 | $this->namespace = $namespace; |
||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return the current environment |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getEnvironment(): string |
||
| 209 | { |
||
| 210 | return $this->env; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Set the environment |
||
| 215 | * @param string $env |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function setEnvironment(string $env) |
||
| 219 | { |
||
| 220 | $this->env = $env; |
||
| 221 | |||
| 222 | return $this; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Return the storage path |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getStoragePath(): string |
||
| 230 | { |
||
| 231 | return $this->storagePath; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set the storage path |
||
| 236 | * @param string $storagePath |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function setStoragePath(string $storagePath): self |
||
| 240 | { |
||
| 241 | $this->storagePath = $storagePath; |
||
| 242 | |||
| 243 | return $this; |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Return the vendor path |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getVendorPath(): string |
||
| 252 | { |
||
| 253 | return $this->vendorPath; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set vendor path |
||
| 258 | * @param string $vendorPath |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function setVendorPath(string $vendorPath): self |
||
| 262 | { |
||
| 263 | $this->vendorPath = $vendorPath; |
||
| 264 | |||
| 265 | return $this; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return the application root path |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function getAppPath(): string |
||
| 273 | { |
||
| 274 | return $this->appPath; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Set Application path |
||
| 279 | * @param string $appPath |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function setAppPath(string $appPath): self |
||
| 283 | { |
||
| 284 | $this->appPath = $appPath; |
||
| 285 | |||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return the application base path |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function getBasePath(): string |
||
| 294 | { |
||
| 295 | return $this->basePath; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Set the application base path |
||
| 300 | * @param string $basePath |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | public function setBasePath(string $basePath): self |
||
| 304 | { |
||
| 305 | $this->basePath = $basePath; |
||
| 306 | |||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Return the application configuration path |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getConfigPath(): string |
||
| 315 | { |
||
| 316 | return $this->configPath; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Set the application configuration path |
||
| 321 | * @param string $configPath |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | public function setConfigPath(string $configPath): self |
||
| 325 | { |
||
| 326 | $this->configPath = $configPath; |
||
| 327 | |||
| 328 | return $this; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Dispatches an event to all registered listeners. |
||
| 333 | * @param string|EventInterface $eventName the name of event |
||
| 334 | * of instance of EventInterface |
||
| 335 | * @param EventInterface|null $event the instance of EventInterface or null |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function dispatch($eventName, EventInterface $event = null): self |
||
| 339 | { |
||
| 340 | $this->dispatcher->dispatch($eventName, $event); |
||
| 341 | |||
| 342 | return $this; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Register a listener for the given event. |
||
| 347 | * |
||
| 348 | * @param string $eventName the name of event |
||
| 349 | * @param ListenerInterface|callable|string $listener the Listener |
||
| 350 | * interface or any callable |
||
| 351 | * @param int $priority the listener execution priority |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | public function listen( |
||
| 355 | string $eventName, |
||
| 356 | $listener, |
||
| 357 | int $priority = DispatcherInterface::PRIORITY_DEFAULT |
||
| 358 | ): self { |
||
| 359 | if (is_string($listener)) { |
||
| 360 | $listener = $this->createListener($listener); |
||
| 361 | } |
||
| 362 | $this->dispatcher->addListener($eventName, $listener, $priority); |
||
| 363 | |||
| 364 | return $this; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Add event subscriber |
||
| 369 | * @param SubscriberInterface $subscriber |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function subscribe(SubscriberInterface $subscriber): self |
||
| 373 | { |
||
| 374 | $this->dispatcher->addSubscriber($subscriber); |
||
| 375 | |||
| 376 | return $this; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Return the list of providers |
||
| 381 | * @return array<string, ServiceProvider> |
||
| 382 | */ |
||
| 383 | public function getProviders(): array |
||
| 384 | { |
||
| 385 | return $this->providers; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Return the list of service providers commands |
||
| 390 | * @return array<class-string> |
||
|
|
|||
| 391 | */ |
||
| 392 | public function getProvidersCommands(): array |
||
| 393 | { |
||
| 394 | $commands = []; |
||
| 395 | foreach ($this->providers as /** @var ServiceProvider $provider */ $provider) { |
||
| 396 | $commands = array_merge($commands, $provider->getCommands()); |
||
| 397 | } |
||
| 398 | |||
| 399 | return $commands; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Boot the application |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | public function boot(): void |
||
| 407 | { |
||
| 408 | if ($this->booted) { |
||
| 409 | return; |
||
| 410 | } |
||
| 411 | |||
| 412 | foreach ($this->providers as $provider) { |
||
| 413 | $this->bootServiceProvider($provider); |
||
| 414 | } |
||
| 415 | |||
| 416 | $this->booted = true; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Register the service provider |
||
| 421 | * @param string|ServiceProvider $provider |
||
| 422 | * @param bool $force whether to force registration of provider |
||
| 423 | * if already loaded |
||
| 424 | * @return ServiceProvider |
||
| 425 | */ |
||
| 426 | public function registerServiceProvider( |
||
| 427 | $provider, |
||
| 428 | bool $force = false |
||
| 429 | ): ServiceProvider { |
||
| 430 | $registered = $this->getServiceProvider($provider); |
||
| 431 | if ($registered && !$force) { |
||
| 432 | return $registered; |
||
| 433 | } |
||
| 434 | |||
| 435 | if (is_string($provider)) { |
||
| 436 | $provider = $this->createServiceProvider($provider); |
||
| 437 | } |
||
| 438 | |||
| 439 | $provider->register(); |
||
| 440 | |||
| 441 | $this->markProviderAsRegistered($provider); |
||
| 442 | |||
| 443 | if ($this->booted) { |
||
| 444 | $this->bootServiceProvider($provider); |
||
| 445 | } |
||
| 446 | |||
| 447 | return $provider; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Return the registered service provider if exist |
||
| 452 | * @param string|ServiceProvider $provider |
||
| 453 | * @return ServiceProvider|null |
||
| 454 | */ |
||
| 455 | public function getServiceProvider($provider): ?ServiceProvider |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Load configured service providers |
||
| 466 | * @return void |
||
| 467 | */ |
||
| 468 | public function registerConfiguredServiceProviders(): void |
||
| 469 | { |
||
| 470 | /** @template T @var Config<T> $config */ |
||
| 471 | $config = $this->get(Config::class); |
||
| 472 | |||
| 473 | /** @var string[] $providers */ |
||
| 474 | $providers = $config->get('providers', []); |
||
| 475 | foreach ($providers as $provider) { |
||
| 476 | $this->registerServiceProvider($provider); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Load configured events and listeners |
||
| 482 | * @return void |
||
| 483 | */ |
||
| 484 | public function registerConfiguredEvents(): void |
||
| 485 | { |
||
| 486 | /** @template T @var Config<T> $config */ |
||
| 487 | $config = $this->get(Config::class); |
||
| 488 | |||
| 489 | /** @var array<string, string[]> $events */ |
||
| 490 | $events = $config->get('events', []); |
||
| 491 | foreach ($events as $eventName => $listeners) { |
||
| 492 | foreach ($listeners as $listener) { |
||
| 493 | $this->listen($eventName, $listener); |
||
| 494 | } |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Load the application configuration |
||
| 500 | * @return void |
||
| 501 | */ |
||
| 502 | public function registerConfiguration(): void |
||
| 503 | { |
||
| 504 | $loader = new FileLoader($this->getConfigPath()); |
||
| 505 | $config = new Config($loader, $this->env); |
||
| 506 | $this->instance($loader); |
||
| 507 | $this->instance($config); |
||
| 508 | |||
| 509 | date_default_timezone_set($config->get('app.timezone', 'UTC')); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Create service provider |
||
| 514 | * @param string $provider |
||
| 515 | * @return ServiceProvider |
||
| 516 | */ |
||
| 517 | protected function createServiceProvider(string $provider): ServiceProvider |
||
| 518 | { |
||
| 519 | return new $provider($this); |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Boot the given service provider |
||
| 524 | * @param ServiceProvider $provider |
||
| 525 | * @return void |
||
| 526 | */ |
||
| 527 | protected function bootServiceProvider(ServiceProvider $provider): void |
||
| 528 | { |
||
| 529 | $provider->boot(); |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Set the given service provider as registered |
||
| 534 | * @param ServiceProvider $provider |
||
| 535 | * @return void |
||
| 536 | */ |
||
| 537 | protected function markProviderAsRegistered(ServiceProvider $provider): void |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Load framework core service providers |
||
| 544 | * @return void |
||
| 545 | */ |
||
| 546 | protected function loadCoreServiceProviders(): void |
||
| 547 | { |
||
| 548 | $this->registerServiceProvider(new BaseServiceProvider($this)); |
||
| 549 | $this->registerServiceProvider(new EventServiceProvider($this)); |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Create listener using the container or direct class instance |
||
| 554 | * @param string $listener |
||
| 555 | * @return ListenerInterface |
||
| 556 | */ |
||
| 557 | protected function createListener(string $listener): ListenerInterface |
||
| 558 | { |
||
| 559 | if ($this->has($listener)) { |
||
| 560 | return $this->get($listener); |
||
| 561 | } |
||
| 562 | |||
| 563 | if (class_exists($listener)) { |
||
| 564 | return new $listener(); |
||
| 565 | } |
||
| 566 | |||
| 567 | throw new InvalidArgumentException(sprintf( |
||
| 568 | 'Can not resolve the listener class [%s], check if this is the' |
||
| 569 | . ' identifier of container or class exists', |
||
| 570 | $listener |
||
| 571 | )); |
||
| 572 | } |
||
| 574 |