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
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use InvalidArgumentException;
|
2017-05-17 07:40:13 +02:00
|
|
|
use SilverStripe\Control\RequestHandler;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-08-22 01:22:08 +02:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-05-17 07:40:13 +02:00
|
|
|
use SilverStripe\View\ViewableData;
|
2016-01-27 01:46:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows an object to have extensions applied to it.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
trait Extensible
|
|
|
|
{
|
2017-08-21 07:22:42 +02:00
|
|
|
use CustomMethods {
|
2017-08-22 01:22:08 +02:00
|
|
|
defineMethods as defineMethodsCustom;
|
2017-08-21 07:22:42 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-03-16 04:47:26 +01:00
|
|
|
private static $extensions = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Classes that cannot be extended
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $unextendable_classes = array(
|
2017-05-17 07:40:13 +02:00
|
|
|
ViewableData::class,
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2017-08-21 07:22:42 +02:00
|
|
|
* @var Extension[] all current extension instances, or null if not declared yet.
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-08-21 07:22:42 +02:00
|
|
|
protected $extension_instances = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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] = array();
|
|
|
|
}
|
|
|
|
$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] = array();
|
|
|
|
}
|
|
|
|
$this->afterExtendCallbacks[$method][] = $callback;
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:22:08 +02:00
|
|
|
/**
|
|
|
|
* @deprecated 4.0..5.0 Extensions and methods are now lazy-loaded
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
protected function constructExtensions()
|
|
|
|
{
|
2017-08-22 01:22:08 +02:00
|
|
|
Deprecation::notice('5.0', 'constructExtensions does not need to be invoked and will be removed in 5.0');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function defineMethods()
|
|
|
|
{
|
|
|
|
$this->defineMethodsCustom();
|
|
|
|
|
|
|
|
// Define extension methods
|
|
|
|
$this->defineExtensionMethods();
|
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()}.
|
|
|
|
*
|
2017-08-22 01:22:08 +02:00
|
|
|
* @uses addCallbackMethod()
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected function defineExtensionMethods()
|
|
|
|
{
|
2017-08-21 07:22:42 +02:00
|
|
|
$extensions = $this->getExtensionInstances();
|
2017-08-22 01:22:08 +02:00
|
|
|
foreach ($extensions as $extensionClass => $extensionInstance) {
|
|
|
|
foreach ($this->findMethodsFromExtension($extensionInstance) as $method) {
|
|
|
|
$this->addCallbackMethod($method, function ($inst, $args) use ($method, $extensionClass) {
|
|
|
|
/** @var Extensible $inst */
|
|
|
|
$extension = $inst->getExtensionInstance($extensionClass);
|
|
|
|
try {
|
2017-10-05 06:23:02 +02:00
|
|
|
$extension->setOwner($inst);
|
2017-08-22 01:22:08 +02:00
|
|
|
return call_user_func_array([$extension, $method], $args);
|
|
|
|
} finally {
|
|
|
|
$extension->clearOwner();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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" or "Translatable('Param')"
|
|
|
|
* @return bool Flag if the extension was added
|
|
|
|
*/
|
|
|
|
public static function add_extension($classOrExtension, $extension = null)
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
if ($extension) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$class = $classOrExtension;
|
|
|
|
} else {
|
|
|
|
$class = get_called_class();
|
|
|
|
$extension = $classOrExtension;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!preg_match('/^([^(]*)/', $extension, $matches)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$extensionClass = $matches[1];
|
|
|
|
if (!class_exists($extensionClass)) {
|
|
|
|
user_error(
|
|
|
|
sprintf('Object::add_extension() - Can\'t find extension class for "%s"', $extensionClass),
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_subclass_of($extensionClass, 'SilverStripe\\Core\\Extension')) {
|
|
|
|
user_error(
|
|
|
|
sprintf('Object::add_extension() - Extension "%s" is not a subclass of Extension', $extensionClass),
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// unset some caches
|
|
|
|
$subclasses = ClassInfo::subclassesFor($class);
|
|
|
|
$subclasses[] = $class;
|
|
|
|
|
|
|
|
if ($subclasses) {
|
|
|
|
foreach ($subclasses as $subclass) {
|
|
|
|
unset(self::$extra_methods[$subclass]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 04:12:46 +01:00
|
|
|
Config::modify()
|
|
|
|
->merge($class, 'extensions', array(
|
|
|
|
$extension
|
|
|
|
));
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
Injector::inst()->unregisterNamedObject($class);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an extension from a class.
|
2017-02-22 04:12:46 +01:00
|
|
|
* Note: This will not remove extensions from parent classes, and must be called
|
|
|
|
* directly on the class assigned the extension.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @todo Add support for removing extensions with parameters
|
|
|
|
*
|
|
|
|
* @param string $extension class name of an {@link Extension} subclass, without parameters
|
|
|
|
*/
|
|
|
|
public static function remove_extension($extension)
|
|
|
|
{
|
|
|
|
$class = get_called_class();
|
|
|
|
|
2017-02-22 04:12:46 +01:00
|
|
|
// Build filtered extension list
|
|
|
|
$found = false;
|
2017-09-27 07:19:25 +02:00
|
|
|
$config = Config::inst()->get($class, 'extensions', Config::EXCLUDE_EXTRA_SOURCES | Config::UNINHERITED) ?: [];
|
2017-02-22 04:12:46 +01:00
|
|
|
foreach ($config as $key => $candidate) {
|
|
|
|
// extensions with parameters will be stored in config as ExtensionName("Param").
|
|
|
|
if (strcasecmp($candidate, $extension) === 0 ||
|
2018-01-16 19:39:30 +01:00
|
|
|
stripos($candidate, $extension . '(') === 0
|
2017-02-22 04:12:46 +01:00
|
|
|
) {
|
|
|
|
$found = true;
|
|
|
|
unset($config[$key]);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 04:12:46 +01:00
|
|
|
// Don't dirty cache if no changes
|
|
|
|
if (!$found) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Config::modify()->set($class, 'extensions', $config);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Unset singletons
|
|
|
|
Injector::inst()->unregisterObjects($class);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// unset some caches
|
|
|
|
$subclasses = ClassInfo::subclassesFor($class);
|
|
|
|
$subclasses[] = $class;
|
|
|
|
if ($subclasses) {
|
|
|
|
foreach ($subclasses as $subclass) {
|
|
|
|
unset(self::$extra_methods[$subclass]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-17 07:40:13 +02:00
|
|
|
* @param string $class If omitted, will get extensions for the current class
|
2016-11-29 00:31:16 +01:00
|
|
|
* @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.
|
|
|
|
*/
|
2017-05-17 07:40:13 +02:00
|
|
|
public static function get_extensions($class = null, $includeArgumentString = false)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
if (!$class) {
|
|
|
|
$class = get_called_class();
|
|
|
|
}
|
|
|
|
|
2017-02-22 04:12:46 +01:00
|
|
|
$extensions = Config::forClass($class)->get('extensions', Config::EXCLUDE_EXTRA_SOURCES);
|
2016-11-29 00:31:16 +01:00
|
|
|
if (empty($extensions)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean nullified named extensions
|
|
|
|
$extensions = array_filter(array_values($extensions));
|
|
|
|
|
|
|
|
if ($includeArgumentString) {
|
|
|
|
return $extensions;
|
|
|
|
} else {
|
|
|
|
$extensionClassnames = array();
|
|
|
|
if ($extensions) {
|
|
|
|
foreach ($extensions as $extension) {
|
|
|
|
$extensionClassnames[] = Extension::get_classname_without_arguments($extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $extensionClassnames;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-17 07:40:13 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public static function get_extra_config_sources($class = null)
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
if (!$class) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$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
|
2017-09-27 07:19:25 +02:00
|
|
|
$extensions = Config::inst()->get($class, 'extensions', Config::EXCLUDE_EXTRA_SOURCES | Config::UNINHERITED);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
if (!$extensions) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a list of all sources;
|
|
|
|
$sources = array();
|
|
|
|
|
|
|
|
foreach ($extensions as $extension) {
|
2017-05-17 07:40:13 +02:00
|
|
|
list($extensionClass, $extensionArgs) = ClassInfo::parse_class_spec($extension);
|
2017-10-05 06:23:02 +02:00
|
|
|
// Strip service name specifier
|
|
|
|
$extensionClass = strtok($extensionClass, '.');
|
2016-11-29 00:31:16 +01:00
|
|
|
$sources[] = $extensionClass;
|
|
|
|
|
|
|
|
if (!class_exists($extensionClass)) {
|
|
|
|
throw new InvalidArgumentException("$class references nonexistent $extensionClass in \$extensions");
|
|
|
|
}
|
|
|
|
|
|
|
|
call_user_func(array($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))
|
2017-05-17 07:40:13 +02:00
|
|
|
* @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.
|
2016-11-29 00:31:16 +01:00
|
|
|
* @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)
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
if ($requiredExtension) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 $a1
|
|
|
|
* @param mixed $a2
|
|
|
|
* @param mixed $a3
|
|
|
|
* @param mixed $a4
|
|
|
|
* @param mixed $a5
|
|
|
|
* @param mixed $a6
|
|
|
|
* @param mixed $a7
|
|
|
|
* @return array List of results with nulls filtered out
|
|
|
|
*/
|
|
|
|
public function invokeWithExtensions($method, &$a1 = null, &$a2 = null, &$a3 = null, &$a4 = null, &$a5 = null, &$a6 = null, &$a7 = null)
|
|
|
|
{
|
|
|
|
$result = array();
|
|
|
|
if (method_exists($this, $method)) {
|
|
|
|
$thisResult = $this->$method($a1, $a2, $a3, $a4, $a5, $a6, $a7);
|
|
|
|
if ($thisResult !== null) {
|
|
|
|
$result[] = $thisResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$extras = $this->extend($method, $a1, $a2, $a3, $a4, $a5, $a6, $a7);
|
|
|
|
|
|
|
|
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 $a1
|
|
|
|
* @param mixed $a2
|
|
|
|
* @param mixed $a3
|
|
|
|
* @param mixed $a4
|
|
|
|
* @param mixed $a5
|
|
|
|
* @param mixed $a6
|
|
|
|
* @param mixed $a7
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function extend($method, &$a1 = null, &$a2 = null, &$a3 = null, &$a4 = null, &$a5 = null, &$a6 = null, &$a7 = null)
|
|
|
|
{
|
|
|
|
$values = array();
|
|
|
|
|
|
|
|
if (!empty($this->beforeExtendCallbacks[$method])) {
|
|
|
|
foreach (array_reverse($this->beforeExtendCallbacks[$method]) as $callback) {
|
|
|
|
$value = call_user_func_array($callback, array(&$a1, &$a2, &$a3, &$a4, &$a5, &$a6, &$a7));
|
|
|
|
if ($value !== null) {
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->beforeExtendCallbacks[$method] = array();
|
|
|
|
}
|
|
|
|
|
2017-08-21 07:22:42 +02:00
|
|
|
foreach ($this->getExtensionInstances() as $instance) {
|
|
|
|
if (method_exists($instance, $method)) {
|
2017-08-22 01:22:08 +02:00
|
|
|
try {
|
2017-10-05 06:23:02 +02:00
|
|
|
$instance->setOwner($this);
|
2017-08-22 01:22:08 +02:00
|
|
|
$value = $instance->$method($a1, $a2, $a3, $a4, $a5, $a6, $a7);
|
|
|
|
} finally {
|
|
|
|
$instance->clearOwner();
|
|
|
|
}
|
2017-08-21 07:22:42 +02:00
|
|
|
if ($value !== null) {
|
|
|
|
$values[] = $value;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->afterExtendCallbacks[$method])) {
|
|
|
|
foreach (array_reverse($this->afterExtendCallbacks[$method]) as $callback) {
|
|
|
|
$value = call_user_func_array($callback, array(&$a1, &$a2, &$a3, &$a4, &$a5, &$a6, &$a7));
|
|
|
|
if ($value !== null) {
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->afterExtendCallbacks[$method] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an extension instance attached to this object by name.
|
|
|
|
*
|
|
|
|
* @param string $extension
|
2018-01-11 15:12:06 +01:00
|
|
|
* @return Extension|null
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function getExtensionInstance($extension)
|
|
|
|
{
|
2017-08-21 07:22:42 +02:00
|
|
|
$instances = $this->getExtensionInstances();
|
2018-01-11 15:12:06 +01:00
|
|
|
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;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2018-01-11 15:12:06 +01:00
|
|
|
return (bool) $this->getExtensionInstance($extension);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*
|
2017-08-21 07:22:42 +02:00
|
|
|
* This method also provides lazy-population of the extension_instances property.
|
|
|
|
*
|
|
|
|
* @return Extension[] Map of {@link DataExtension} instances, keyed by classname.
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function getExtensionInstances()
|
|
|
|
{
|
2017-08-21 07:22:42 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-09-27 07:19:25 +02:00
|
|
|
$extensions = Config::inst()->get($class, 'extensions', Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES);
|
2017-08-21 07:22:42 +02:00
|
|
|
|
|
|
|
if ($extensions) {
|
|
|
|
foreach ($extensions as $extension) {
|
2018-01-11 15:12:06 +01:00
|
|
|
$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);
|
2017-08-21 07:22:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return $this->extension_instances;
|
|
|
|
}
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|