2016-01-27 01:46:43 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Core;
|
2016-01-27 01:46:43 +01:00
|
|
|
|
|
|
|
use BadMethodCallException;
|
|
|
|
use InvalidArgumentException;
|
2017-08-22 01:22:08 +02:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2016-01-27 01:46:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows an object to declare a set of custom methods
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
trait CustomMethods
|
|
|
|
{
|
2016-01-27 01:46:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom method sources
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-22 01:22:08 +02:00
|
|
|
protected static $extra_methods = [];
|
2016-01-27 01:46:43 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Name of methods to invoke by defineMethods for this instance
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $extra_method_registers = [];
|
2016-01-27 01:46:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Non-custom methods
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $built_in_methods = [];
|
2016-01-27 01:46:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to locate and call a method dynamically added to a class at runtime if a default cannot be located
|
|
|
|
*
|
|
|
|
* You can add extra methods to a class using {@link Extensions}, {@link Object::createMethod()} or
|
|
|
|
* {@link Object::addWrapperMethod()}
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param array $arguments
|
|
|
|
* @return mixed
|
|
|
|
* @throws BadMethodCallException
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function __call($method, $arguments)
|
|
|
|
{
|
|
|
|
// If the method cache was cleared by an an Object::add_extension() / Object::remove_extension()
|
|
|
|
// call, then we should rebuild it.
|
2017-05-17 07:40:13 +02:00
|
|
|
$class = static::class;
|
2016-11-29 00:31:16 +01:00
|
|
|
$config = $this->getExtraMethodConfig($method);
|
|
|
|
if (empty($config)) {
|
|
|
|
throw new BadMethodCallException(
|
|
|
|
"Object->__call(): the method '$method' does not exist on '$class'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (true) {
|
2017-08-22 01:22:08 +02:00
|
|
|
case isset($config['callback']): {
|
|
|
|
return $config['callback']($this, $arguments);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
case isset($config['property']) : {
|
2017-09-03 23:23:07 +02:00
|
|
|
$property = $config['property'];
|
|
|
|
$index = $config['index'];
|
|
|
|
$obj = $index !== null ?
|
|
|
|
$this->{$property}[$index] :
|
|
|
|
$this->{$property};
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-10-05 06:23:02 +02:00
|
|
|
if (!$obj) {
|
|
|
|
throw new BadMethodCallException(
|
|
|
|
"Object->__call(): {$class} cannot pass control to {$property}({$index})."
|
|
|
|
. ' Perhaps this object was mistakenly destroyed?'
|
|
|
|
);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 06:23:02 +02:00
|
|
|
// Call without setOwner
|
|
|
|
if (empty($config['callSetOwnerFirst'])) {
|
|
|
|
return $obj->$method(...$arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Extension $obj */
|
|
|
|
try {
|
|
|
|
$obj->setOwner($this);
|
|
|
|
return $obj->$method(...$arguments);
|
|
|
|
} finally {
|
|
|
|
$obj->clearOwner();
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-08-22 01:22:08 +02:00
|
|
|
case isset($config['wrap']): {
|
2016-11-29 00:31:16 +01:00
|
|
|
array_unshift($arguments, $config['method']);
|
2017-10-05 06:23:02 +02:00
|
|
|
$wrapped = $config['wrap'];
|
|
|
|
return $this->$wrapped(...$arguments);
|
2017-08-22 01:22:08 +02:00
|
|
|
}
|
|
|
|
case isset($config['function']): {
|
2016-11-29 00:31:16 +01:00
|
|
|
return $config['function']($this, $arguments);
|
2017-08-22 01:22:08 +02:00
|
|
|
}
|
|
|
|
default: {
|
2016-11-29 00:31:16 +01:00
|
|
|
throw new BadMethodCallException(
|
|
|
|
"Object->__call(): extra method $method is invalid on $class:"
|
2017-08-22 01:22:08 +02:00
|
|
|
. var_export($config, true)
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
2017-08-22 01:22:08 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 addMethodsFrom()
|
|
|
|
*/
|
|
|
|
protected function defineMethods()
|
|
|
|
{
|
|
|
|
// Define from all registered callbacks
|
|
|
|
foreach ($this->extra_method_registers as $callback) {
|
|
|
|
call_user_func($callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register an callback to invoke that defines extra methods
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param callable $callback
|
|
|
|
*/
|
|
|
|
protected function registerExtraMethodCallback($name, $callback)
|
|
|
|
{
|
|
|
|
if (!isset($this->extra_method_registers[$name])) {
|
|
|
|
$this->extra_method_registers[$name] = $callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return TRUE if a method exists on this object
|
|
|
|
*
|
|
|
|
* This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via
|
|
|
|
* extensions
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasMethod($method)
|
|
|
|
{
|
|
|
|
return method_exists($this, $method) || $this->getExtraMethodConfig($method);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get meta-data details on a named method
|
|
|
|
*
|
2017-05-17 07:40:13 +02:00
|
|
|
* @param string $method
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return array List of custom method details, if defined for this method
|
|
|
|
*/
|
|
|
|
protected function getExtraMethodConfig($method)
|
|
|
|
{
|
2017-08-22 01:22:08 +02:00
|
|
|
// Lazy define methods
|
|
|
|
if (!isset(self::$extra_methods[static::class])) {
|
|
|
|
$this->defineMethods();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset(self::$extra_methods[static::class][strtolower($method)])) {
|
|
|
|
return self::$extra_methods[static::class][strtolower($method)];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-01-27 01:46:43 +01:00
|
|
|
|
|
|
|
/**
|
2016-11-29 00:31:16 +01:00
|
|
|
* Return the names of all the methods available on this object
|
|
|
|
*
|
|
|
|
* @param bool $custom include methods added dynamically at runtime
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function allMethodNames($custom = false)
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
$class = static::class;
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!isset(self::$built_in_methods[$class])) {
|
|
|
|
self::$built_in_methods[$class] = array_map('strtolower', get_class_methods($this));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($custom && isset(self::$extra_methods[$class])) {
|
|
|
|
return array_merge(self::$built_in_methods[$class], array_keys(self::$extra_methods[$class]));
|
|
|
|
} else {
|
|
|
|
return self::$built_in_methods[$class];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param object $extension
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function findMethodsFromExtension($extension)
|
|
|
|
{
|
|
|
|
if (method_exists($extension, 'allMethodNames')) {
|
|
|
|
if ($extension instanceof Extension) {
|
2017-10-05 06:23:02 +02:00
|
|
|
try {
|
|
|
|
$extension->setOwner($this);
|
|
|
|
$methods = $extension->allMethodNames(true);
|
|
|
|
} finally {
|
|
|
|
$extension->clearOwner();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$methods = $extension->allMethodNames(true);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-17 07:40:13 +02:00
|
|
|
$class = get_class($extension);
|
|
|
|
if (!isset(self::$built_in_methods[$class])) {
|
|
|
|
self::$built_in_methods[$class] = array_map('strtolower', get_class_methods($extension));
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-05-17 07:40:13 +02:00
|
|
|
$methods = self::$built_in_methods[$class];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add all the methods from an object property (which is an {@link Extension}) to this object.
|
|
|
|
*
|
|
|
|
* @param string $property the property name
|
|
|
|
* @param string|int $index an index to use if the property is an array
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function addMethodsFrom($property, $index = null)
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
$class = static::class;
|
2016-11-29 00:31:16 +01:00
|
|
|
$extension = ($index !== null) ? $this->{$property}[$index] : $this->$property;
|
|
|
|
|
|
|
|
if (!$extension) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
"Object->addMethodsFrom(): could not add methods from {$class}->{$property}[$index]"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$methods = $this->findMethodsFromExtension($extension);
|
|
|
|
if ($methods) {
|
2017-08-22 01:22:08 +02:00
|
|
|
if ($extension instanceof Extension) {
|
|
|
|
Deprecation::notice(
|
|
|
|
'5.0',
|
|
|
|
'Register custom methods from extensions with addCallbackMethod.'
|
|
|
|
. ' callSetOwnerFirst will be removed in 5.0'
|
|
|
|
);
|
|
|
|
}
|
2020-04-20 19:58:09 +02:00
|
|
|
$methodInfo = [
|
2016-11-29 00:31:16 +01:00
|
|
|
'property' => $property,
|
2017-08-22 01:22:08 +02:00
|
|
|
'index' => $index,
|
2016-11-29 00:31:16 +01:00
|
|
|
'callSetOwnerFirst' => $extension instanceof Extension,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
$newMethods = array_fill_keys($methods, $methodInfo);
|
|
|
|
|
|
|
|
if (isset(self::$extra_methods[$class])) {
|
|
|
|
self::$extra_methods[$class] =
|
|
|
|
array_merge(self::$extra_methods[$class], $newMethods);
|
|
|
|
} else {
|
|
|
|
self::$extra_methods[$class] = $newMethods;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add all the methods from an object property (which is an {@link Extension}) to this object.
|
|
|
|
*
|
|
|
|
* @param string $property the property name
|
|
|
|
* @param string|int $index an index to use if the property is an array
|
|
|
|
*/
|
|
|
|
protected function removeMethodsFrom($property, $index = null)
|
|
|
|
{
|
|
|
|
$extension = ($index !== null) ? $this->{$property}[$index] : $this->$property;
|
2017-05-17 07:40:13 +02:00
|
|
|
$class = static::class;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
if (!$extension) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
"Object->removeMethodsFrom(): could not remove methods from {$class}->{$property}[$index]"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$methods = $this->findMethodsFromExtension($extension);
|
|
|
|
if ($methods) {
|
|
|
|
foreach ($methods as $method) {
|
2019-01-08 21:27:38 +01:00
|
|
|
if (!isset(self::$extra_methods[$class][$method])) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-09 08:53:05 +01:00
|
|
|
|
2019-01-08 21:24:21 +01:00
|
|
|
$methodInfo = self::$extra_methods[$class][$method];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2020-11-29 23:37:35 +01:00
|
|
|
// always check for property, AND
|
|
|
|
// check for index only if provided
|
|
|
|
if ((isset($methodInfo['property']) && $methodInfo['property'] === $property) &&
|
|
|
|
(!$index || ($index && isset($methodInfo['index']) && $methodInfo['index'] === $index))) {
|
2019-01-08 21:24:21 +01:00
|
|
|
unset(self::$extra_methods[$class][$method]);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty(self::$extra_methods[$class])) {
|
|
|
|
unset(self::$extra_methods[$class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a wrapper method - a method which points to another method with a different name. For example, Thumbnail(x)
|
|
|
|
* can be wrapped to generateThumbnail(x)
|
|
|
|
*
|
|
|
|
* @param string $method the method name to wrap
|
|
|
|
* @param string $wrap the method name to wrap to
|
|
|
|
*/
|
|
|
|
protected function addWrapperMethod($method, $wrap)
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
self::$extra_methods[static::class][strtolower($method)] = [
|
2017-08-22 01:22:08 +02:00
|
|
|
'wrap' => $wrap,
|
2016-11-29 00:31:16 +01:00
|
|
|
'method' => $method
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-08-22 01:22:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add callback as a method.
|
|
|
|
*
|
|
|
|
* @param string $method Name of method
|
|
|
|
* @param callable $callback Callback to invoke.
|
|
|
|
* Note: $this is passed as first parameter to this callback and then $args as array
|
|
|
|
*/
|
|
|
|
protected function addCallbackMethod($method, $callback)
|
|
|
|
{
|
|
|
|
self::$extra_methods[static::class][strtolower($method)] = [
|
|
|
|
'callback' => $callback,
|
|
|
|
];
|
|
|
|
}
|
2016-01-27 01:46:43 +01:00
|
|
|
}
|