/
HelperRegistry.php
154 lines (138 loc) · 4.44 KB
/
HelperRegistry.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?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\View;
use Cake\Core\App;
use Cake\Core\ObjectRegistry;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\View\Exception\MissingHelperException;
/**
* HelperRegistry is used as a registry for loaded helpers and handles loading
* and constructing helper class objects.
*/
class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
{
use EventDispatcherTrait;
/**
* View object to use when making helpers.
*
* @var \Cake\View\View
*/
protected $_View;
/**
* Constructor
*
* @param \Cake\View\View $view View object.
*/
public function __construct(View $view)
{
$this->_View = $view;
$this->setEventManager($view->getEventManager());
}
/**
* Tries to lazy load a helper based on its name, if it cannot be found
* in the application folder, then it tries looking under the current plugin
* if any
*
* @param string $helper The helper name to be loaded
* @return bool whether the helper could be loaded or not
* @throws \Cake\View\Exception\MissingHelperException When a helper could not be found.
* App helpers are searched, and then plugin helpers.
*/
public function __isset($helper)
{
if (isset($this->_loaded[$helper])) {
return true;
}
try {
$this->load($helper);
} catch (Exception\MissingHelperException $exception) {
if ($this->_View->getPlugin()) {
$this->load($this->_View->getPlugin() . '.' . $helper);
return true;
}
}
if (!empty($exception)) {
throw $exception;
}
return true;
}
/**
* Provide public read access to the loaded objects
*
* @param string $name Name of property to read
* @return mixed
*/
public function __get($name)
{
if (isset($this->_loaded[$name])) {
return $this->_loaded[$name];
}
if (isset($this->{$name})) {
return $this->_loaded[$name];
}
return null;
}
/**
* Resolve a helper 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, 'View/Helper', 'Helper');
}
/**
* Throws an exception when a helper 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 helper is missing in.
* @return void
* @throws \Cake\View\Exception\MissingHelperException
*/
protected function _throwMissingClassError($class, $plugin)
{
throw new MissingHelperException([
'class' => $class . 'Helper',
'plugin' => $plugin,
]);
}
/**
* Create the helper instance.
*
* Part of the template method for Cake\Core\ObjectRegistry::load()
* Enabled helpers will be registered with the event manager.
*
* @param string $class The class to create.
* @param string $alias The alias of the loaded helper.
* @param array $settings An array of settings to use for the helper.
* @return \Cake\View\Helper The constructed helper class.
*/
protected function _create($class, $alias, $settings)
{
$instance = new $class($this->_View, $settings);
$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
if ($enable) {
$this->getEventManager()->on($instance);
}
return $instance;
}
}