/
Extensible.php
543 lines (484 loc) · 19.4 KB
/
Extensible.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
namespace SilverStripe\Core;
use InvalidArgumentException;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\View\ViewableData;
/**
* Allows an object to have extensions applied to it.
*/
trait Extensible
{
use CustomMethods {
defineMethods as defineMethodsCustom;
}
/**
* An array of extension names and parameters to be applied to this object upon construction.
*
* Example:
* <code>
* private static $extensions = array (
* 'Hierarchy',
* "Version('Stage', 'Live')"
* );
* </code>
*
* Use {@link Object::add_extension()} to add extensions without access to the class code,
* e.g. to extend core classes.
*
* Extensions are instantiated together with the object and stored in {@link $extension_instances}.
*
* @var array $extensions
* @config
*/
private static $extensions = [];
/**
* Classes that cannot be extended
*
* @var array
*/
private static $unextendable_classes = [
ViewableData::class,
];
/**
* @var Extension[] all current extension instances, or null if not declared yet.
*/
protected $extension_instances = null;
/**
* List of callbacks to call prior to extensions having extend called on them,
* each grouped by methodName.
*
* Top level array is method names, each of which is an array of callbacks for that name.
*
* @var callable[][]
*/
protected $beforeExtendCallbacks = [];
/**
* List of callbacks to call after extensions having extend called on them,
* each grouped by methodName.
*
* Top level array is method names, each of which is an array of callbacks for that name.
*
* @var callable[][]
*/
protected $afterExtendCallbacks = [];
/**
* Allows user code to hook into Object::extend prior to control
* being delegated to extensions. Each callback will be reset
* once called.
*
* @param string $method The name of the method to hook into
* @param callable $callback The callback to execute
*/
protected function beforeExtending($method, $callback)
{
if (empty($this->beforeExtendCallbacks[$method])) {
$this->beforeExtendCallbacks[$method] = [];
}
$this->beforeExtendCallbacks[$method][] = $callback;
}
/**
* Allows user code to hook into Object::extend after control
* being delegated to extensions. Each callback will be reset
* once called.
*
* @param string $method The name of the method to hook into
* @param callable $callback The callback to execute
*/
protected function afterExtending($method, $callback)
{
if (empty($this->afterExtendCallbacks[$method])) {
$this->afterExtendCallbacks[$method] = [];
}
$this->afterExtendCallbacks[$method][] = $callback;
}
protected function defineMethods()
{
$this->defineMethodsCustom();
// Define extension methods
$this->defineExtensionMethods();
}
/**
* Adds any methods from {@link Extension} instances attached to this object.
* All these methods can then be called directly on the instance (transparently
* mapped through {@link __call()}), or called explicitly through {@link extend()}.
*
* @uses addCallbackMethod()
*/
protected function defineExtensionMethods()
{
$extensions = $this->getExtensionInstances();
foreach ($extensions as $extensionClass => $extensionInstance) {
foreach ($this->findMethodsFrom($extensionInstance) as $method) {
$this->addCallbackMethod($method, function ($inst, $args) use ($method, $extensionClass) {
/** @var Extensible $inst */
$extension = $inst->getExtensionInstance($extensionClass);
if (!$extension) {
return null;
}
try {
$extension->setOwner($inst);
return call_user_func_array([$extension, $method], $args ?? []);
} finally {
$extension->clearOwner();
}
});
}
}
}
/**
* Add an extension to a specific class.
*
* The preferred method for adding extensions is through YAML config,
* since it avoids autoloading the class, and is easier to override in
* more specific configurations.
*
* As an alternative, extensions can be added to a specific class
* directly in the {@link Object::$extensions} array.
* See {@link SiteTree::$extensions} for examples.
* Keep in mind that the extension will only be applied to new
* instances, not existing ones (including all instances created through {@link singleton()}).
*
* @see http://doc.silverstripe.org/framework/en/trunk/reference/dataextension
* @param string $classOrExtension Class that should be extended - has to be a subclass of {@link Object}
* @param string $extension Subclass of {@link Extension} with optional parameters
* as a string, e.g. "Versioned"
* @return bool Flag if the extension was added
*/
public static function add_extension($classOrExtension, $extension = null)
{
if ($extension) {
$class = $classOrExtension;
} else {
$class = get_called_class();
$extension = $classOrExtension;
}
if (!preg_match('/^([^(]*)/', $extension ?? '', $matches)) {
return false;
}
$extensionClass = $matches[1];
if (!class_exists($extensionClass ?? '')) {
throw new InvalidArgumentException(sprintf(
'Object::add_extension() - Can\'t find extension class for "%s"',
$extensionClass
));
}
if (!is_subclass_of($extensionClass, Extension::class)) {
throw new InvalidArgumentException(sprintf(
'Object::add_extension() - Extension "%s" is not a subclass of Extension',
$extensionClass
));
}
// unset some caches
$subclasses = ClassInfo::subclassesFor($class);
$subclasses[] = $class;
foreach ($subclasses as $subclass) {
unset(self::$extra_methods[strtolower($subclass)]);
}
Config::modify()
->merge($class, 'extensions', [
$extension
]);
Injector::inst()->unregisterNamedObject($class);
return true;
}
/**
* Remove an extension from a class.
* Note: This will not remove extensions from parent classes, and must be called
* directly on the class assigned the extension.
*
* Keep in mind that this won't revert any datamodel additions
* of the extension at runtime, unless its used before the
* schema building kicks in (in your _config.php).
* Doesn't remove the extension from any {@link Object}
* instances which are already created, but will have an
* effect on new extensions.
* Clears any previously created singletons through {@link singleton()}
* to avoid side-effects from stale extension information.
*
* @param string $extension class name of an {@link Extension} subclass, without parameters
*/
public static function remove_extension($extension)
{
$class = get_called_class();
// Build filtered extension list
$found = false;
$config = Config::inst()->get($class, 'extensions', Config::EXCLUDE_EXTRA_SOURCES | Config::UNINHERITED) ?: [];
foreach ($config as $key => $candidate) {
// extensions with parameters will be stored in config as ExtensionName("Param").
if (strcasecmp($candidate ?? '', $extension ?? '') === 0 ||
stripos($candidate ?? '', $extension . '(') === 0
) {
$found = true;
unset($config[$key]);
}
}
// Don't dirty cache if no changes
if (!$found) {
return;
}
Config::modify()->set($class, 'extensions', $config);
// Unset singletons
Injector::inst()->unregisterObjects($class);
// unset some caches
$subclasses = ClassInfo::subclassesFor($class);
$subclasses[] = $class;
foreach ($subclasses as $subclass) {
unset(self::$extra_methods[strtolower($subclass)]);
}
}
/**
* @param string $class If omitted, will get extensions for the current class
* @param bool $includeArgumentString Include the argument string in the return array,
* FALSE would return array("Versioned"), TRUE returns array("Versioned('Stage','Live')").
* @return array Numeric array of either {@link DataExtension} class names,
* or eval'ed class name strings with constructor arguments.
*/
public static function get_extensions($class = null, $includeArgumentString = false)
{
if (!$class) {
$class = get_called_class();
}
$extensions = Config::forClass($class)->get('extensions', Config::EXCLUDE_EXTRA_SOURCES);
if (empty($extensions)) {
return [];
}
// Clean nullified named extensions
$extensions = array_filter(array_values($extensions ?? []));
if ($includeArgumentString) {
return $extensions;
} else {
$extensionClassnames = [];
if ($extensions) {
foreach ($extensions as $extension) {
$extensionClassnames[] = Extension::get_classname_without_arguments($extension);
}
}
return $extensionClassnames;
}
}
/**
* Get extra config sources for this class
*
* @param string $class Name of class. If left null will return for the current class
* @return array|null
*/
public static function get_extra_config_sources($class = null)
{
if (!$class) {
$class = get_called_class();
}
// If this class is unextendable, NOP
if (in_array($class, self::$unextendable_classes)) {
return null;
}
// Variable to hold sources in
$sources = null;
// Get a list of extensions
$extensions = Config::inst()->get($class, 'extensions', Config::EXCLUDE_EXTRA_SOURCES | Config::UNINHERITED);
if (!$extensions) {
return null;
}
// Build a list of all sources;
$sources = [];
foreach ($extensions as $extension) {
[$extensionClass, $extensionArgs] = ClassInfo::parse_class_spec($extension);
// Strip service name specifier
$extensionClass = strtok($extensionClass ?? '', '.');
$sources[] = $extensionClass;
if (!class_exists($extensionClass ?? '')) {
throw new InvalidArgumentException("$class references nonexistent $extensionClass in \$extensions");
}
call_user_func([$extensionClass, 'add_to_class'], $class, $extensionClass, $extensionArgs);
foreach (array_reverse(ClassInfo::ancestry($extensionClass) ?? []) as $extensionClassParent) {
if (ClassInfo::has_method_from($extensionClassParent, 'get_extra_config', $extensionClassParent)) {
$extras = $extensionClassParent::get_extra_config($class, $extensionClass, $extensionArgs);
if ($extras) {
$sources[] = $extras;
}
}
}
}
return $sources;
}
/**
* Return TRUE if a class has a specified extension.
* This supports backwards-compatible format (static Object::has_extension($requiredExtension))
* and new format ($object->has_extension($class, $requiredExtension))
* @param string $classOrExtension Class to check extension for, or the extension name to check
* if the second argument is null.
* @param string $requiredExtension If the first argument is the parent class, this is the extension to check.
* If left null, the first parameter will be treated as the extension.
* @param boolean $strict if the extension has to match the required extension and not be a subclass
* @return bool Flag if the extension exists
*/
public static function has_extension($classOrExtension, $requiredExtension = null, $strict = false)
{
if ($requiredExtension) {
$class = $classOrExtension;
} else {
$class = get_called_class();
$requiredExtension = $classOrExtension;
}
$requiredExtension = Extension::get_classname_without_arguments($requiredExtension);
$extensions = self::get_extensions($class);
foreach ($extensions as $extension) {
if (strcasecmp($extension ?? '', $requiredExtension ?? '') === 0) {
return true;
}
if (!$strict && is_subclass_of($extension, $requiredExtension ?? '')) {
return true;
}
$inst = Injector::inst()->get($extension);
if ($inst instanceof $requiredExtension) {
return $strict ? strcasecmp(get_class($inst), $requiredExtension) === 0 : true;
}
}
return false;
}
/**
* Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge
* all results into an array
*
* @param string $method the method name to call
* @param mixed ...$arguments List of arguments
* @return array List of results with nulls filtered out
*/
public function invokeWithExtensions($method, &...$arguments)
{
$result = [];
if (method_exists($this, $method ?? '')) {
$thisResult = $this->$method(...$arguments);
if ($thisResult !== null) {
$result[] = $thisResult;
}
}
$extras = $this->extend($method, ...$arguments);
return $extras ? array_merge($result, $extras) : $result;
}
/**
* Run the given function on all of this object's extensions. Note that this method originally returned void, so if
* you wanted to return results, you're hosed
*
* Currently returns an array, with an index resulting every time the function is called. Only adds returns if
* they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for
* permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't
* do type checking, an included NULL return would fail the permission checks.
*
* The extension methods are defined during {@link __construct()} in {@link defineMethods()}.
*
* @param string $method the name of the method to call on each extension
* @param mixed &...$arguments
* @return array
*/
public function extend($method, &...$arguments)
{
$values = [];
if (!empty($this->beforeExtendCallbacks[$method])) {
foreach (array_reverse($this->beforeExtendCallbacks[$method ?? '']) as $callback) {
$value = call_user_func_array($callback, $arguments);
if ($value !== null) {
$values[] = $value;
}
}
$this->beforeExtendCallbacks[$method] = [];
}
foreach ($this->getExtensionInstances() as $instance) {
// Prefer `extend` prefixed methods
$value = $instance->invokeExtension($this, $method, ...$arguments);
if ($value !== null) {
$values[] = $value;
}
}
if (!empty($this->afterExtendCallbacks[$method])) {
foreach (array_reverse($this->afterExtendCallbacks[$method ?? '']) as $callback) {
$value = call_user_func_array($callback, $arguments);
if ($value !== null) {
$values[] = $value;
}
}
$this->afterExtendCallbacks[$method] = [];
}
return $values;
}
/**
* Get an extension instance attached to this object by name.
*
* @param string $extension
* @return Extension|null
*/
public function getExtensionInstance($extension)
{
$instances = $this->getExtensionInstances();
if (array_key_exists($extension, $instances ?? [])) {
return $instances[$extension];
}
// in case Injector has been used to replace an extension
foreach ($instances as $instance) {
if (is_a($instance, $extension ?? '')) {
return $instance;
}
}
return null;
}
/**
* Returns TRUE if this object instance has a specific extension applied
* in {@link $extension_instances}. Extension instances are initialized
* at constructor time, meaning if you use {@link add_extension()}
* afterwards, the added extension will just be added to new instances
* of the extended class. Use the static method {@link has_extension()}
* to check if a class (not an instance) has a specific extension.
* Caution: Don't use singleton(<class>)->hasExtension() as it will
* give you inconsistent results based on when the singleton was first
* accessed.
*
* @param string $extension Classname of an {@link Extension} subclass without parameters
* @return bool
*/
public function hasExtension($extension)
{
return (bool) $this->getExtensionInstance($extension);
}
/**
* Get all extension instances for this specific object instance.
* See {@link get_extensions()} to get all applied extension classes
* for this class (not the instance).
*
* This method also provides lazy-population of the extension_instances property.
*
* @return Extension[] Map of {@link DataExtension} instances, keyed by classname.
*/
public function getExtensionInstances()
{
if (isset($this->extension_instances)) {
return $this->extension_instances;
}
// Setup all extension instances for this instance
$this->extension_instances = [];
foreach (ClassInfo::ancestry(static::class) as $class) {
if (in_array($class, self::$unextendable_classes)) {
continue;
}
$extensions = Config::inst()->get($class, 'extensions', Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES);
if ($extensions) {
foreach ($extensions as $extension) {
// Allow removing extensions via yaml config by setting named extension config to null
if ($extension === null) {
continue;
}
$name = $extension;
// Allow service names of the form "%$ServiceName"
if (substr($name ?? '', 0, 2) == '%$') {
$name = substr($name ?? '', 2);
}
$name = trim(strtok($name ?? '', '(') ?? '');
if (class_exists($name ?? '')) {
$name = ClassInfo::class_name($name);
}
$this->extension_instances[$name] = Injector::inst()->get($extension);
}
}
}
return $this->extension_instances;
}
}