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:
31: use EventDispatcherTrait;
32:
33: /**
34: * The controller that this collection was initialized with.
35: *
36: * @var \Cake\Controller\Controller
37: */
38: protected $_Controller;
39:
40: /**
41: * Constructor.
42: *
43: * @param \Cake\Controller\Controller|null $controller Controller instance.
44: */
45: public function __construct(Controller $controller = null)
46: {
47: if ($controller) {
48: $this->setController($controller);
49: }
50: }
51:
52: /**
53: * Get the controller associated with the collection.
54: *
55: * @return \Cake\Controller\Controller Controller instance
56: */
57: public function getController()
58: {
59: return $this->_Controller;
60: }
61:
62: /**
63: * Set the controller associated with the collection.
64: *
65: * @param \Cake\Controller\Controller $controller Controller instance.
66: * @return void
67: */
68: public function setController(Controller $controller)
69: {
70: $this->_Controller = $controller;
71: $this->setEventManager($controller->getEventManager());
72: }
73:
74: /**
75: * Resolve a component classname.
76: *
77: * Part of the template method for Cake\Core\ObjectRegistry::load()
78: *
79: * @param string $class Partial classname to resolve.
80: * @return string|false Either the correct classname or false.
81: */
82: protected function _resolveClassName($class)
83: {
84: return App::className($class, 'Controller/Component', 'Component');
85: }
86:
87: /**
88: * Throws an exception when a component is missing.
89: *
90: * Part of the template method for Cake\Core\ObjectRegistry::load()
91: * and Cake\Core\ObjectRegistry::unload()
92: *
93: * @param string $class The classname that is missing.
94: * @param string $plugin The plugin the component is missing in.
95: * @return void
96: * @throws \Cake\Controller\Exception\MissingComponentException
97: */
98: protected function _throwMissingClassError($class, $plugin)
99: {
100: throw new MissingComponentException([
101: 'class' => $class . 'Component',
102: 'plugin' => $plugin
103: ]);
104: }
105:
106: /**
107: * Create the component instance.
108: *
109: * Part of the template method for Cake\Core\ObjectRegistry::load()
110: * Enabled components will be registered with the event manager.
111: *
112: * @param string $class The classname to create.
113: * @param string $alias The alias of the component.
114: * @param array $config An array of config to use for the component.
115: * @return \Cake\Controller\Component The constructed component class.
116: */
117: protected function _create($class, $alias, $config)
118: {
119: $instance = new $class($this, $config);
120: $enable = isset($config['enabled']) ? $config['enabled'] : true;
121: if ($enable) {
122: $this->getEventManager()->on($instance);
123: }
124:
125: return $instance;
126: }
127: }
128: