ComponentRegistry.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Controller;
  17. use Cake\Controller\Component;
  18. use Cake\Controller\Exception\MissingComponentException;
  19. use Cake\Core\App;
  20. use Cake\Core\ObjectRegistry;
  21. use Cake\Event\EventDispatcherInterface;
  22. use Cake\Event\EventDispatcherTrait;
  23. /**
  24. * ComponentRegistry is a registry for loaded components
  25. *
  26. * Handles loading, constructing and binding events for component class objects.
  27. */
  28. class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterface
  29. {
  30. use EventDispatcherTrait;
  31. /**
  32. * The controller that this collection was initialized with.
  33. *
  34. * @var \Cake\Controller\Controller
  35. */
  36. protected $_Controller;
  37. /**
  38. * Constructor.
  39. *
  40. * @param \Cake\Controller\Controller|null $controller Controller instance.
  41. */
  42. public function __construct(Controller $controller = null)
  43. {
  44. if ($controller) {
  45. $this->setController($controller);
  46. }
  47. }
  48. /**
  49. * Get the controller associated with the collection.
  50. *
  51. * @return \Cake\Controller\Controller|null Controller instance
  52. */
  53. public function getController(): ?Controller
  54. {
  55. return $this->_Controller;
  56. }
  57. /**
  58. * Set the controller associated with the collection.
  59. *
  60. * @param \Cake\Controller\Controller $controller Controller instance.
  61. * @return void
  62. */
  63. public function setController(Controller $controller): void
  64. {
  65. $this->_Controller = $controller;
  66. $this->setEventManager($controller->getEventManager());
  67. }
  68. /**
  69. * Resolve a component classname.
  70. *
  71. * Part of the template method for Cake\Core\ObjectRegistry::load()
  72. *
  73. * @param string $class Partial classname to resolve.
  74. * @return string|false Either the correct classname or false.
  75. */
  76. protected function _resolveClassName($class)
  77. {
  78. return App::className($class, 'Controller/Component', 'Component');
  79. }
  80. /**
  81. * Throws an exception when a component is missing.
  82. *
  83. * Part of the template method for Cake\Core\ObjectRegistry::load()
  84. * and Cake\Core\ObjectRegistry::unload()
  85. *
  86. * @param string $class The classname that is missing.
  87. * @param string $plugin The plugin the component is missing in.
  88. * @return void
  89. * @throws \Cake\Controller\Exception\MissingComponentException
  90. */
  91. protected function _throwMissingClassError($class, $plugin): void
  92. {
  93. throw new MissingComponentException([
  94. 'class' => $class . 'Component',
  95. 'plugin' => $plugin
  96. ]);
  97. }
  98. /**
  99. * Create the component instance.
  100. *
  101. * Part of the template method for Cake\Core\ObjectRegistry::load()
  102. * Enabled components will be registered with the event manager.
  103. *
  104. * @param string $class The classname to create.
  105. * @param string $alias The alias of the component.
  106. * @param array $config An array of config to use for the component.
  107. * @return \Cake\Controller\Component The constructed component class.
  108. */
  109. protected function _create($class, $alias, $config): Component
  110. {
  111. $instance = new $class($this, $config);
  112. $enable = isset($config['enabled']) ? $config['enabled'] : true;
  113. if ($enable) {
  114. $this->getEventManager()->on($instance);
  115. }
  116. return $instance;
  117. }
  118. }