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