mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FIX Allow extension instances to be overridden by injector
This commit is contained in:
parent
de6afd4405
commit
7d66342496
@ -208,6 +208,7 @@ class ClassInfo
|
|||||||
* eg: self::class_name('dataobJEct'); //returns 'DataObject'
|
* eg: self::class_name('dataobJEct'); //returns 'DataObject'
|
||||||
*
|
*
|
||||||
* @param string|object $nameOrObject The classname or object you want to normalise
|
* @param string|object $nameOrObject The classname or object you want to normalise
|
||||||
|
* @throws \ReflectionException
|
||||||
* @return string The normalised class name
|
* @return string The normalised class name
|
||||||
*/
|
*/
|
||||||
public static function class_name($nameOrObject)
|
public static function class_name($nameOrObject)
|
||||||
|
@ -495,17 +495,22 @@ trait Extensible
|
|||||||
/**
|
/**
|
||||||
* Get an extension instance attached to this object by name.
|
* Get an extension instance attached to this object by name.
|
||||||
*
|
*
|
||||||
* @uses hasExtension()
|
|
||||||
*
|
|
||||||
* @param string $extension
|
* @param string $extension
|
||||||
* @return Extension
|
* @return Extension|null
|
||||||
*/
|
*/
|
||||||
public function getExtensionInstance($extension)
|
public function getExtensionInstance($extension)
|
||||||
{
|
{
|
||||||
$instances = $this->getExtensionInstances();
|
$instances = $this->getExtensionInstances();
|
||||||
return isset($instances[$extension])
|
if (array_key_exists($extension, $instances)) {
|
||||||
? $instances[$extension]
|
return $instances[$extension];
|
||||||
: null;
|
}
|
||||||
|
// in case Injector has been used to replace an extension
|
||||||
|
foreach ($instances as $instance) {
|
||||||
|
if (is_a($instance, $extension)) {
|
||||||
|
return $instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -524,8 +529,7 @@ trait Extensible
|
|||||||
*/
|
*/
|
||||||
public function hasExtension($extension)
|
public function hasExtension($extension)
|
||||||
{
|
{
|
||||||
$instances = $this->getExtensionInstances();
|
return (bool) $this->getExtensionInstance($extension);
|
||||||
return isset($instances[$extension]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -553,8 +557,16 @@ trait Extensible
|
|||||||
|
|
||||||
if ($extensions) {
|
if ($extensions) {
|
||||||
foreach ($extensions as $extension) {
|
foreach ($extensions as $extension) {
|
||||||
$instance = Injector::inst()->get($extension);
|
$name = $extension;
|
||||||
$this->extension_instances[get_class($instance)] = $instance;
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user