| Total Complexity | 42 |
| Total Lines | 596 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | class Container |
||
| 62 | { |
||
| 63 | /** |
||
| 64 | * The Dependency Injection Container |
||
| 65 | * |
||
| 66 | * @var PimpleContainer |
||
| 67 | */ |
||
| 68 | private $libContainer = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The Dependency Injection Container |
||
| 72 | * |
||
| 73 | * @var ContainerInterface |
||
| 74 | */ |
||
| 75 | private $appContainer = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The class constructor |
||
| 79 | * |
||
| 80 | * @param array $aOptions The default options |
||
| 81 | */ |
||
| 82 | public function __construct(array $aOptions) |
||
| 83 | { |
||
| 84 | $this->libContainer = new PimpleContainer(); |
||
| 85 | $this->libContainer[Container::class] = $this; |
||
| 86 | |||
| 87 | $sTranslationDir = realpath(__DIR__ . '/../../../translations'); |
||
| 88 | $sTemplateDir = realpath(__DIR__ . '/../../../templates'); |
||
| 89 | $this->init($sTranslationDir, $sTemplateDir); |
||
| 90 | $this->getConfig()->setOptions($aOptions); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the container provided by the integrated framework |
||
| 95 | * |
||
| 96 | * @return ContainerInterface |
||
| 97 | */ |
||
| 98 | public function getAppContainer() |
||
| 99 | { |
||
| 100 | return $this->appContainer; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set the container provided by the integrated framework |
||
| 105 | * |
||
| 106 | * @param ContainerInterface $container The container implementation |
||
| 107 | * |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | public function setAppContainer(ContainerInterface $container) |
||
| 111 | { |
||
| 112 | $this->appContainer = $container; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set the parameters and create the objects in the dependency injection container |
||
| 117 | * |
||
| 118 | * @param string $sTranslationDir The translation directory |
||
| 119 | * @param string $sTemplateDir The template directory |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | private function init($sTranslationDir, $sTemplateDir) |
||
| 124 | { |
||
| 125 | /* |
||
| 126 | * Parameters |
||
| 127 | */ |
||
| 128 | // Translation directory |
||
| 129 | $this->libContainer['jaxon.core.translation_dir'] = $sTranslationDir; |
||
| 130 | // Template directory |
||
| 131 | $this->libContainer['jaxon.core.template_dir'] = $sTemplateDir; |
||
| 132 | |||
| 133 | /* |
||
| 134 | * Core library objects |
||
| 135 | */ |
||
| 136 | // Global Response |
||
| 137 | $this->libContainer[Response::class] = function() { |
||
| 138 | return new Response(); |
||
| 139 | }; |
||
| 140 | // Dialog |
||
| 141 | $this->libContainer[Dialog::class] = function() { |
||
| 142 | return new Dialog(); |
||
| 143 | }; |
||
| 144 | // Jaxon App |
||
| 145 | $this->libContainer[App::class] = function() { |
||
| 146 | return new App(); |
||
| 147 | }; |
||
| 148 | // Jaxon App bootstrap |
||
| 149 | $this->libContainer[Bootstrap::class] = function() { |
||
| 150 | return new Bootstrap(); |
||
| 151 | }; |
||
| 152 | |||
| 153 | /* |
||
| 154 | * Plugins |
||
| 155 | */ |
||
| 156 | // Callable objects repository |
||
| 157 | $this->libContainer[CallableRepository::class] = function() { |
||
| 158 | return new CallableRepository(); |
||
| 159 | }; |
||
| 160 | // Callable objects registry |
||
| 161 | $this->libContainer[CallableRegistry::class] = function($c) { |
||
| 162 | return new CallableRegistry($c[CallableRepository::class]); |
||
| 163 | }; |
||
| 164 | // Callable class plugin |
||
| 165 | $this->libContainer[CallableClass::class] = function($c) { |
||
| 166 | return new CallableClass($c[CallableRegistry::class], $c[CallableRepository::class]); |
||
| 167 | }; |
||
| 168 | // Callable dir plugin |
||
| 169 | $this->libContainer[CallableDir::class] = function($c) { |
||
| 170 | return new CallableDir($c[CallableRegistry::class]); |
||
| 171 | }; |
||
| 172 | // Callable function plugin |
||
| 173 | $this->libContainer[CallableFunction::class] = function() { |
||
| 174 | return new CallableFunction(); |
||
| 175 | }; |
||
| 176 | // File upload support |
||
| 177 | $this->libContainer[FileUploadSupport::class] = function() { |
||
| 178 | return new FileUploadSupport(); |
||
| 179 | }; |
||
| 180 | // File upload plugin |
||
| 181 | $this->libContainer[FileUpload::class] = function($c) { |
||
| 182 | return new FileUpload($c[FileUploadSupport::class]); |
||
| 183 | }; |
||
| 184 | // JQuery response plugin |
||
| 185 | $this->libContainer[JQueryPlugin::class] = function() { |
||
| 186 | return new JQueryPlugin(); |
||
| 187 | }; |
||
| 188 | |||
| 189 | /* |
||
| 190 | * Managers |
||
| 191 | */ |
||
| 192 | // Plugin Manager |
||
| 193 | $this->libContainer[PluginManager::class] = function($c) { |
||
| 194 | return new PluginManager($c[CodeGenerator::class]); |
||
| 195 | }; |
||
| 196 | // Request Handler |
||
| 197 | $this->libContainer[RequestHandler::class] = function($c) { |
||
| 198 | return new RequestHandler($c[PluginManager::class], $c[ResponseManager::class], $c[FileUpload::class]); |
||
| 199 | }; |
||
| 200 | // Request Factory |
||
| 201 | $this->libContainer[RequestFactory::class] = function($c) { |
||
| 202 | return new RequestFactory($c[CallableRegistry::class]); |
||
| 203 | }; |
||
| 204 | // Parameter Factory |
||
| 205 | $this->libContainer[ParameterFactory::class] = function() { |
||
| 206 | return new ParameterFactory(); |
||
| 207 | }; |
||
| 208 | // Response Manager |
||
| 209 | $this->libContainer[ResponseManager::class] = function() { |
||
| 210 | return new ResponseManager(); |
||
| 211 | }; |
||
| 212 | // Code Generator |
||
| 213 | $this->libContainer[CodeGenerator::class] = function($c) { |
||
| 214 | return new CodeGenerator($c[TemplateEngine::class]); |
||
| 215 | }; |
||
| 216 | // View Manager |
||
| 217 | $this->libContainer[ViewManager::class] = function() { |
||
| 218 | $xViewManager = new ViewManager(); |
||
| 219 | // Add the default view renderer |
||
| 220 | $xViewManager->addRenderer('jaxon', function($di) { |
||
| 221 | return new TemplateView($di->get(TemplateEngine::class)); |
||
| 222 | }); |
||
| 223 | // By default, render pagination templates with Jaxon. |
||
| 224 | $xViewManager->addNamespace('pagination', '', '.php', 'jaxon'); |
||
| 225 | return $xViewManager; |
||
| 226 | }; |
||
| 227 | // View Renderer |
||
| 228 | $this->libContainer[ViewRenderer::class] = function($c) { |
||
| 229 | return new ViewRenderer($c[ViewManager::class]); |
||
| 230 | }; |
||
| 231 | // Set the default session manager |
||
| 232 | $this->libContainer[SessionContract::class] = function() { |
||
| 233 | return new SessionManager(); |
||
| 234 | }; |
||
| 235 | |||
| 236 | /* |
||
| 237 | * Config |
||
| 238 | */ |
||
| 239 | $this->libContainer[Config::class] = function() { |
||
| 240 | return new Config(); |
||
| 241 | }; |
||
| 242 | $this->libContainer[ConfigReader::class] = function() { |
||
| 243 | return new ConfigReader(); |
||
| 244 | }; |
||
| 245 | |||
| 246 | /* |
||
| 247 | * Services |
||
| 248 | */ |
||
| 249 | // Minifier |
||
| 250 | $this->libContainer[Minifier::class] = function() { |
||
| 251 | return new Minifier(); |
||
| 252 | }; |
||
| 253 | // Translator |
||
| 254 | $this->libContainer[Translator::class] = function($c) { |
||
| 255 | return new Translator($c['jaxon.core.translation_dir'], $c[Config::class]); |
||
| 256 | }; |
||
| 257 | // Template engine |
||
| 258 | $this->libContainer[TemplateEngine::class] = function($c) { |
||
| 259 | return new TemplateEngine($c['jaxon.core.template_dir']); |
||
| 260 | }; |
||
| 261 | // Validator |
||
| 262 | $this->libContainer[Validator::class] = function($c) { |
||
| 263 | return new Validator($c[Translator::class], $c[Config::class]); |
||
| 264 | }; |
||
| 265 | // Pagination Paginator |
||
| 266 | $this->libContainer[Paginator::class] = function($c) { |
||
| 267 | return new Paginator($c[PaginationRenderer::class]); |
||
| 268 | }; |
||
| 269 | // Pagination Renderer |
||
| 270 | $this->libContainer[PaginationRenderer::class] = function($c) { |
||
| 271 | return new PaginationRenderer($c[ViewRenderer::class]); |
||
| 272 | }; |
||
| 273 | // Event Dispatcher |
||
| 274 | $this->libContainer[EventDispatcher::class] = function() { |
||
| 275 | return new EventDispatcher(); |
||
| 276 | }; |
||
| 277 | // URI decoder |
||
| 278 | $this->libContainer[URI::class] = function() { |
||
| 279 | return new URI(); |
||
| 280 | }; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get a class instance |
||
| 285 | * |
||
| 286 | * @return object The class instance |
||
| 287 | */ |
||
| 288 | public function get($sClass) |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Save a closure in the container |
||
| 299 | * |
||
| 300 | * @param string $sClass The full class name |
||
| 301 | * @param Closure $xClosure The closure |
||
| 302 | * |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function set($sClass, Closure $xClosure) |
||
| 309 | }; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Save a value in the container |
||
| 314 | * |
||
| 315 | * @param string $sKey The key |
||
| 316 | * @param mixed $xValue The value |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | public function val($sKey, $xValue) |
||
| 321 | { |
||
| 322 | $this->libContainer[$sKey] = $xValue; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set an alias in the container |
||
| 327 | * |
||
| 328 | * @param string $sAlias The alias name |
||
| 329 | * @param string $sClass The class name |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | */ |
||
| 333 | public function alias($sAlias, $sClass) |
||
| 334 | { |
||
| 335 | $this->libContainer[$sAlias] = function($c) use ($sClass) { |
||
| 336 | return $c[$sClass]; |
||
| 337 | }; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Create an instance of a class, getting the contructor parameters from the DI container |
||
| 342 | * |
||
| 343 | * @param string|ReflectionClass $xClass The class name or the reflection class |
||
| 344 | * |
||
| 345 | * @return mixed |
||
| 346 | */ |
||
| 347 | public function make($xClass) |
||
| 348 | { |
||
| 349 | if(is_string($xClass)) |
||
| 350 | { |
||
| 351 | // Create the reflection class instance |
||
| 352 | $xClass = new ReflectionClass($xClass); |
||
| 353 | } |
||
| 354 | if(!($xClass instanceof ReflectionClass)) |
||
| 355 | { |
||
| 356 | return null; |
||
| 357 | } |
||
| 358 | // Use the Reflection class to get the parameters of the constructor |
||
| 359 | if(($constructor = $xClass->getConstructor()) == null) |
||
| 360 | { |
||
| 361 | return $xClass->newInstance(); |
||
| 362 | } |
||
| 363 | $parameters = $constructor->getParameters(); |
||
| 364 | $parameterInstances = []; |
||
| 365 | foreach($parameters as $parameter) |
||
| 366 | { |
||
| 367 | // Get the parameter instance from the DI |
||
| 368 | $parameterInstances[] = $this->get($parameter->getClass()->getName()); |
||
| 369 | } |
||
| 370 | return $xClass->newInstanceArgs($parameterInstances); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Create an instance of a class by automatically fetching the dependencies from the constructor. |
||
| 375 | * |
||
| 376 | * @param string $sClass The class name |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | public function auto($sClass) |
||
| 381 | { |
||
| 382 | $this->libContainer[$sClass] = function($c) use ($sClass) { |
||
| 383 | return $this->make($sClass); |
||
| 384 | }; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the plugin manager |
||
| 389 | * |
||
| 390 | * @return PluginManager |
||
| 391 | */ |
||
| 392 | public function getPluginManager() |
||
| 393 | { |
||
| 394 | return $this->libContainer[PluginManager::class]; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get the request handler |
||
| 399 | * |
||
| 400 | * @return RequestHandler |
||
| 401 | */ |
||
| 402 | public function getRequestHandler() |
||
| 403 | { |
||
| 404 | return $this->libContainer[RequestHandler::class]; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get the request factory |
||
| 409 | * |
||
| 410 | * @return RequestFactory |
||
| 411 | */ |
||
| 412 | public function getRequestFactory() |
||
| 413 | { |
||
| 414 | return $this->libContainer[RequestFactory::class]; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get the parameter factory |
||
| 419 | * |
||
| 420 | * @return ParameterFactory |
||
| 421 | */ |
||
| 422 | public function getParameterFactory() |
||
| 423 | { |
||
| 424 | return $this->libContainer[ParameterFactory::class]; |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get the response manager |
||
| 429 | * |
||
| 430 | * @return ResponseManager |
||
| 431 | */ |
||
| 432 | public function getResponseManager() |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get the code generator |
||
| 439 | * |
||
| 440 | * @return CodeGenerator |
||
| 441 | */ |
||
| 442 | public function getCodeGenerator() |
||
| 443 | { |
||
| 444 | return $this->libContainer[CodeGenerator::class]; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get the callable registry |
||
| 449 | * |
||
| 450 | * @return CallableRegistry |
||
| 451 | */ |
||
| 452 | public function getCallableRegistry() |
||
| 453 | { |
||
| 454 | return $this->libContainer[CallableRegistry::class]; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Get the config manager |
||
| 459 | * |
||
| 460 | * @return Config |
||
| 461 | */ |
||
| 462 | public function getConfig() |
||
| 463 | { |
||
| 464 | return $this->libContainer[Config::class]; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Create a new the config manager |
||
| 469 | * |
||
| 470 | * @param array $aOptions The options array |
||
| 471 | * @param string $sKeys The keys of the options in the array |
||
| 472 | * |
||
| 473 | * @return Config The config manager |
||
| 474 | */ |
||
| 475 | public function newConfig(array $aOptions = [], $sKeys = '') |
||
| 476 | { |
||
| 477 | return new Config($aOptions, $sKeys); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get the dialog wrapper |
||
| 482 | * |
||
| 483 | * @return Dialog |
||
| 484 | */ |
||
| 485 | public function getDialog() |
||
| 486 | { |
||
| 487 | return $this->libContainer[Dialog::class]; |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get the minifier |
||
| 492 | * |
||
| 493 | * @return Minifier |
||
| 494 | */ |
||
| 495 | public function getMinifier() |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Get the translator |
||
| 502 | * |
||
| 503 | * @return Translator |
||
| 504 | */ |
||
| 505 | public function getTranslator() |
||
| 506 | { |
||
| 507 | return $this->libContainer[Translator::class]; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Get the template engine |
||
| 512 | * |
||
| 513 | * @return TemplateEngine |
||
| 514 | */ |
||
| 515 | public function getTemplateEngine() |
||
| 516 | { |
||
| 517 | return $this->libContainer[TemplateEngine::class]; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Get the validator |
||
| 522 | * |
||
| 523 | * @return Validator |
||
| 524 | */ |
||
| 525 | public function getValidator() |
||
| 526 | { |
||
| 527 | return $this->libContainer[Validator::class]; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Get the paginator |
||
| 532 | * |
||
| 533 | * @return Paginator |
||
| 534 | */ |
||
| 535 | public function getPaginator() |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Get the event dispatcher |
||
| 542 | * |
||
| 543 | * @return EventDispatcher |
||
| 544 | */ |
||
| 545 | public function getEventDispatcher() |
||
| 546 | { |
||
| 547 | return $this->libContainer[EventDispatcher::class]; |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Get the global Response object |
||
| 552 | * |
||
| 553 | * @return Response |
||
| 554 | */ |
||
| 555 | public function getResponse() |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Create a new Jaxon response object |
||
| 562 | * |
||
| 563 | * @return Response |
||
| 564 | */ |
||
| 565 | public function newResponse() |
||
| 566 | { |
||
| 567 | return new Response(); |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Get the App instance |
||
| 572 | * |
||
| 573 | * @return App |
||
| 574 | */ |
||
| 575 | public function getApp() |
||
| 576 | { |
||
| 577 | return $this->libContainer[App::class]; |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get the App bootstrap |
||
| 582 | * |
||
| 583 | * @return Bootstrap |
||
| 584 | */ |
||
| 585 | public function getBootstrap() |
||
| 586 | { |
||
| 587 | return $this->libContainer[Bootstrap::class]; |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Get the view manager |
||
| 592 | * |
||
| 593 | * @return ViewManager |
||
| 594 | */ |
||
| 595 | public function getViewManager() |
||
| 596 | { |
||
| 597 | return $this->libContainer[ViewManager::class]; |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Get the view facade |
||
| 602 | * |
||
| 603 | * @return ViewRenderer |
||
| 604 | */ |
||
| 605 | public function getViewRenderer() |
||
| 606 | { |
||
| 607 | return $this->libContainer[ViewRenderer::class]; |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Get the session manager |
||
| 612 | * |
||
| 613 | * @return SessionContract |
||
| 614 | */ |
||
| 615 | public function getSessionManager() |
||
| 616 | { |
||
| 617 | return $this->libContainer[SessionContract::class]; |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Set the session manager |
||
| 622 | * |
||
| 623 | * @param Closure $xClosure A closure to create the session manager instance |
||
| 624 | * |
||
| 625 | * @return void |
||
| 626 | */ |
||
| 627 | public function setSessionManager(Closure $xClosure) |
||
| 628 | { |
||
| 629 | $this->libContainer[SessionContract::class] = $xClosure; |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Set the callable class request factory |
||
| 634 | * |
||
| 635 | * @param string $sClassName The callable class name |
||
| 636 | * @param CallableObject $xCallableObject The corresponding callable object |
||
| 637 | * |
||
| 638 | * @return void |
||
| 639 | */ |
||
| 640 | public function setCallableClassRequestFactory($sClassName, CallableObject $xCallableObject) |
||
| 644 | }; |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Get the callable class request factory |
||
| 649 | * |
||
| 650 | * @param string $sClassName The callable class name |
||
| 651 | * |
||
| 652 | * @return CallableClassRequestFactory |
||
| 653 | */ |
||
| 654 | public function getCallableClassRequestFactory($sClassName) |
||
| 655 | { |
||
| 656 | return $this->libContainer[$sClassName . '_RequestFactory']; |
||
| 657 | } |
||
| 658 | } |
||
| 659 |