mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #7746 from dhensby/pulls/4.0/allow-injector-extensions
FIX Allow extension instances to be overridden by injector
This commit is contained in:
commit
a15c6887fc
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -195,11 +195,13 @@ class ObjectTest extends SapphireTest
|
|||||||
);
|
);
|
||||||
$inst = new ExtensionTest();
|
$inst = new ExtensionTest();
|
||||||
$extensions = $inst->getExtensionInstances();
|
$extensions = $inst->getExtensionInstances();
|
||||||
$this->assertEquals(count($extensions), 2);
|
$this->assertCount(2, $extensions);
|
||||||
|
$this->assertArrayHasKey(ExtendTest1::class, $extensions);
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
ExtendTest1::class,
|
ExtendTest1::class,
|
||||||
$extensions[ExtendTest1::class]
|
$extensions[ExtendTest1::class]
|
||||||
);
|
);
|
||||||
|
$this->assertArrayHasKey(ExtendTest2::class, $extensions);
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
ExtendTest2::class,
|
ExtendTest2::class,
|
||||||
$extensions[ExtendTest2::class]
|
$extensions[ExtendTest2::class]
|
||||||
@ -507,4 +509,33 @@ class ObjectTest extends SapphireTest
|
|||||||
ClassInfo::parse_class_spec('\Test\MyClass')
|
ClassInfo::parse_class_spec('\Test\MyClass')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInjectedExtensions()
|
||||||
|
{
|
||||||
|
$mockExtension = $this->createMock(TestExtension::class);
|
||||||
|
$mockClass = get_class($mockExtension);
|
||||||
|
|
||||||
|
$object = new ExtensionTest2();
|
||||||
|
|
||||||
|
// sanity check
|
||||||
|
$this->assertNotEquals(TestExtension::class, $mockClass);
|
||||||
|
|
||||||
|
$this->assertTrue($object->hasExtension(TestExtension::class));
|
||||||
|
$this->assertFalse($object->hasExtension($mockClass));
|
||||||
|
$this->assertCount(1, $object->getExtensionInstances());
|
||||||
|
$this->assertInstanceOf(TestExtension::class, $object->getExtensionInstance(TestExtension::class));
|
||||||
|
$this->assertNotInstanceOf($mockClass, $object->getExtensionInstance(TestExtension::class));
|
||||||
|
|
||||||
|
Injector::inst()->registerService($mockExtension, TestExtension::class);
|
||||||
|
|
||||||
|
$object = new ExtensionTest2();
|
||||||
|
|
||||||
|
$this->assertTrue($object->hasExtension(TestExtension::class));
|
||||||
|
$this->assertTrue($object->hasExtension($mockClass));
|
||||||
|
$this->assertCount(1, $object->getExtensionInstances());
|
||||||
|
$this->assertInstanceOf(TestExtension::class, $object->getExtensionInstance(TestExtension::class));
|
||||||
|
$this->assertInstanceOf($mockClass, $object->getExtensionInstance(TestExtension::class));
|
||||||
|
$this->assertInstanceOf(TestExtension::class, $object->getExtensionInstance($mockClass));
|
||||||
|
$this->assertInstanceOf($mockClass, $object->getExtensionInstance($mockClass));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user