Repository navigation
Expand file tree
/
Copy pathComponentRegistry.php
More file actions
130 lines (119 loc) · 3.93 KB
/
Copy pathComponentRegistry.php
File metadata and controls
130 lines (119 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* 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\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.
*
* @extends \Cake\Core\ObjectRegistry<\Cake\Controller\Component>
*/
class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterface
{
use EventDispatcherTrait;
/**
* The controller that this collection was initialized with.
*
* @var \Cake\Controller\Controller|null
*/
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 or null if not set.
*/
public function getController()
{
return $this->_Controller;
}
/**
* Set the controller associated with the collection.
*
* @param \Cake\Controller\Controller $controller Controller instance.
* @return $this
*/
public function setController(Controller $controller)
{
$this->_Controller = $controller;
$this->setEventManager($controller->getEventManager());
return $this;
}
/**
* 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|null $plugin The plugin the component is missing in.
* @return void
* @throws \Cake\Controller\Exception\MissingComponentException
*/
protected function _throwMissingClassError($class, $plugin)
{
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)
{
$instance = new $class($this, $config);
$enable = isset($config['enabled']) ? $config['enabled'] : true;
if ($enable) {
$this->getEventManager()->on($instance);
}
return $instance;
}
}