| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- declare(strict_types = 1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 2.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Controller;
- use Cake\Controller\Component;
- use Cake\Controller\Exception\MissingComponentException;
- use Cake\Core\App;
- use Cake\Core\ObjectRegistry;
- use Cake\Event\EventDispatcherInterface;
- use Cake\Event\EventDispatcherTrait;
- /**
- * ComponentRegistry is a registry for loaded components
- *
- * Handles loading, constructing and binding events for component class objects.
- */
- class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterface
- {
- use EventDispatcherTrait;
- /**
- * The controller that this collection was initialized with.
- *
- * @var \Cake\Controller\Controller
- */
- protected $_Controller;
- /**
- * Constructor.
- *
- * @param \Cake\Controller\Controller|null $controller Controller instance.
- */
- public function __construct(Controller $controller = null)
- {
- if ($controller) {
- $this->setController($controller);
- }
- }
- /**
- * Get the controller associated with the collection.
- *
- * @return \Cake\Controller\Controller|null Controller instance
- */
- public function getController(): ?Controller
- {
- return $this->_Controller;
- }
- /**
- * Set the controller associated with the collection.
- *
- * @param \Cake\Controller\Controller $controller Controller instance.
- * @return void
- */
- public function setController(Controller $controller): void
- {
- $this->_Controller = $controller;
- $this->setEventManager($controller->getEventManager());
- }
- /**
- * Resolve a component classname.
- *
- * Part of the template method for Cake\Core\ObjectRegistry::load()
- *
- * @param string $class Partial classname to resolve.
- * @return string|false Either the correct classname or false.
- */
- protected function _resolveClassName($class)
- {
- return App::className($class, 'Controller/Component', 'Component');
- }
- /**
- * Throws an exception when a component is missing.
- *
- * Part of the template method for Cake\Core\ObjectRegistry::load()
- * and Cake\Core\ObjectRegistry::unload()
- *
- * @param string $class The classname that is missing.
- * @param string $plugin The plugin the component is missing in.
- * @return void
- * @throws \Cake\Controller\Exception\MissingComponentException
- */
- protected function _throwMissingClassError($class, $plugin): void
- {
- throw new MissingComponentException([
- 'class' => $class . 'Component',
- 'plugin' => $plugin
- ]);
- }
- /**
- * Create the component instance.
- *
- * Part of the template method for Cake\Core\ObjectRegistry::load()
- * Enabled components will be registered with the event manager.
- *
- * @param string $class The classname to create.
- * @param string $alias The alias of the component.
- * @param array $config An array of config to use for the component.
- * @return \Cake\Controller\Component The constructed component class.
- */
- protected function _create($class, $alias, $config): Component
- {
- $instance = new $class($this, $config);
- $enable = isset($config['enabled']) ? $config['enabled'] : true;
- if ($enable) {
- $this->getEventManager()->on($instance);
- }
- return $instance;
- }
- }
|