-
Notifications
You must be signed in to change notification settings - Fork 18
/
BehaviorRegistry.php
283 lines (255 loc) · 9.08 KB
/
BehaviorRegistry.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?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 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM;
use BadMethodCallException;
use Cake\Core\App;
use Cake\Core\ObjectRegistry;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\ORM\Exception\MissingBehaviorException;
use LogicException;
/**
* BehaviorRegistry is used as a registry for loaded behaviors and handles loading
* and constructing behavior objects.
*
* This class also provides method for checking and dispatching behavior methods.
*
* @extends \Cake\Core\ObjectRegistry<\Cake\ORM\Behavior>
*/
class BehaviorRegistry extends ObjectRegistry implements EventDispatcherInterface
{
use EventDispatcherTrait;
/**
* The table using this registry.
*
* @var \Cake\ORM\Table
*/
protected $_table;
/**
* Method mappings.
*
* @var array
*/
protected $_methodMap = [];
/**
* Finder method mappings.
*
* @var array
*/
protected $_finderMap = [];
/**
* Constructor
*
* @param \Cake\ORM\Table|null $table The table this registry is attached to.
*/
public function __construct(?Table $table = null)
{
if ($table !== null) {
$this->setTable($table);
}
}
/**
* Attaches a table instance to this registry.
*
* @param \Cake\ORM\Table $table The table this registry is attached to.
* @return void
*/
public function setTable(Table $table): void
{
$this->_table = $table;
$this->setEventManager($table->getEventManager());
}
/**
* Resolve a behavior classname.
*
* @param string $class Partial classname to resolve.
* @return string|null Either the correct classname or null.
* @psalm-return class-string|null
*/
public static function className(string $class): ?string
{
return App::className($class, 'Model/Behavior', 'Behavior')
?: App::className($class, 'ORM/Behavior', 'Behavior');
}
/**
* Resolve a behavior classname.
*
* Part of the template method for Cake\Core\ObjectRegistry::load()
*
* @param string $class Partial classname to resolve.
* @return string|null Either the correct class name or null.
* @psalm-return class-string|null
*/
protected function _resolveClassName(string $class): ?string
{
return static::className($class);
}
/**
* Throws an exception when a behavior 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 behavior is missing in.
* @return void
* @throws \Cake\ORM\Exception\MissingBehaviorException
*/
protected function _throwMissingClassError(string $class, ?string $plugin): void
{
throw new MissingBehaviorException([
'class' => $class . 'Behavior',
'plugin' => $plugin,
]);
}
/**
* Create the behavior instance.
*
* Part of the template method for Cake\Core\ObjectRegistry::load()
* Enabled behaviors will be registered with the event manager.
*
* @param string $class The classname that is missing.
* @param string $alias The alias of the object.
* @param array $config An array of config to use for the behavior.
* @return \Cake\ORM\Behavior The constructed behavior class.
* @psalm-suppress MoreSpecificImplementedParamType
*/
protected function _create($class, string $alias, array $config): Behavior
{
/** @var \Cake\ORM\Behavior $instance */
$instance = new $class($this->_table, $config);
$enable = $config['enabled'] ?? true;
if ($enable) {
$this->getEventManager()->on($instance);
}
$methods = $this->_getMethods($instance, $class, $alias);
$this->_methodMap += $methods['methods'];
$this->_finderMap += $methods['finders'];
return $instance;
}
/**
* Get the behavior methods and ensure there are no duplicates.
*
* Use the implementedEvents() method to exclude callback methods.
* Methods starting with `_` will be ignored, as will methods
* declared on Cake\ORM\Behavior
*
* @param \Cake\ORM\Behavior $instance The behavior to get methods from.
* @param string $class The classname that is missing.
* @param string $alias The alias of the object.
* @return array A list of implemented finders and methods.
* @throws \LogicException when duplicate methods are connected.
*/
protected function _getMethods(Behavior $instance, string $class, string $alias): array
{
$finders = array_change_key_case($instance->implementedFinders());
$methods = array_change_key_case($instance->implementedMethods());
foreach ($finders as $finder => $methodName) {
if (isset($this->_finderMap[$finder]) && $this->has($this->_finderMap[$finder][0])) {
$duplicate = $this->_finderMap[$finder];
$error = sprintf(
'%s contains duplicate finder "%s" which is already provided by "%s"',
$class,
$finder,
$duplicate[0]
);
throw new LogicException($error);
}
$finders[$finder] = [$alias, $methodName];
}
foreach ($methods as $method => $methodName) {
if (isset($this->_methodMap[$method]) && $this->has($this->_methodMap[$method][0])) {
$duplicate = $this->_methodMap[$method];
$error = sprintf(
'%s contains duplicate method "%s" which is already provided by "%s"',
$class,
$method,
$duplicate[0]
);
throw new LogicException($error);
}
$methods[$method] = [$alias, $methodName];
}
return compact('methods', 'finders');
}
/**
* Check if any loaded behavior implements a method.
*
* Will return true if any behavior provides a public non-finder method
* with the chosen name.
*
* @param string $method The method to check for.
* @return bool
*/
public function hasMethod(string $method): bool
{
$method = strtolower($method);
return isset($this->_methodMap[$method]);
}
/**
* Check if any loaded behavior implements the named finder.
*
* Will return true if any behavior provides a public method with
* the chosen name.
*
* @param string $method The method to check for.
* @return bool
*/
public function hasFinder(string $method): bool
{
$method = strtolower($method);
return isset($this->_finderMap[$method]);
}
/**
* Invoke a method on a behavior.
*
* @param string $method The method to invoke.
* @param array $args The arguments you want to invoke the method with.
* @return mixed The return value depends on the underlying behavior method.
* @throws \BadMethodCallException When the method is unknown.
*/
public function call(string $method, array $args = [])
{
$method = strtolower($method);
if ($this->hasMethod($method) && $this->has($this->_methodMap[$method][0])) {
[$behavior, $callMethod] = $this->_methodMap[$method];
return $this->_loaded[$behavior]->{$callMethod}(...$args);
}
throw new BadMethodCallException(
sprintf('Cannot call "%s" it does not belong to any attached behavior.', $method)
);
}
/**
* Invoke a finder on a behavior.
*
* @param string $type The finder type to invoke.
* @param array $args The arguments you want to invoke the method with.
* @return \Cake\ORM\Query The return value depends on the underlying behavior method.
* @throws \BadMethodCallException When the method is unknown.
*/
public function callFinder(string $type, array $args = []): Query
{
$type = strtolower($type);
if ($this->hasFinder($type) && $this->has($this->_finderMap[$type][0])) {
[$behavior, $callMethod] = $this->_finderMap[$type];
$callable = [$this->_loaded[$behavior], $callMethod];
return $callable(...$args);
}
throw new BadMethodCallException(
sprintf('Cannot call finder "%s" it does not belong to any attached behavior.', $type)
);
}
}