Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#1990 from weierophinne…
Browse files Browse the repository at this point in the history
…y/hotfix/servicemanager-initializer

Allow passing initializer class names in configuration
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
26 changes: 0 additions & 26 deletions src/AbstractPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace Zend\ServiceManager;

use ReflectionClass;

/**
* ServiceManager implementation for managing plugins
*
Expand Down Expand Up @@ -188,28 +186,4 @@ protected function createFromInvokable($canonicalName, $requestedName)

return $instance;
}

/**
* Checks if the object has this class as one of its parents
*
* @see https://bugs.php.net/bug.php?id=53727
* @see https://github.com/zendframework/zf2/pull/1807
*
* @param string $className
* @param string $type
*/
protected static function isSubclassOf($className, $type)
{
if (is_subclass_of($className, $type)) {
return true;
}
if (version_compare(PHP_VERSION, '5.3.7', '>=')) {
return false;
}
if (!interface_exists($type)) {
return false;
}
$r = new ReflectionClass($className);
return $r->implementsInterface($type);
}
}
33 changes: 32 additions & 1 deletion src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Zend\ServiceManager;

use ReflectionClass;

class ServiceManager implements ServiceLocatorInterface
{

Expand Down Expand Up @@ -284,7 +286,12 @@ public function addAbstractFactory($factory, $topOfStack = true)
public function addInitializer($initializer, $topOfStack = true)
{
if (!is_callable($initializer) && !$initializer instanceof InitializerInterface) {
throw new Exception\InvalidArgumentException('$initializer should be callable.');
if (!is_string($initializer)
|| !$this->isSubclassOf($initializer, __NAMESPACE__ . '\InitializerInterface')
) {
throw new Exception\InvalidArgumentException('$initializer should be callable.');
}
$initializer = new $initializer;
}

if ($topOfStack) {
Expand Down Expand Up @@ -844,4 +851,28 @@ protected function createFromAbstractFactory($canonicalName, $requestedName)

return $instance;
}

/**
* Checks if the object has this class as one of its parents
*
* @see https://bugs.php.net/bug.php?id=53727
* @see https://github.com/zendframework/zf2/pull/1807
*
* @param string $className
* @param string $type
*/
protected static function isSubclassOf($className, $type)
{
if (is_subclass_of($className, $type)) {
return true;
}
if (version_compare(PHP_VERSION, '5.3.7', '>=')) {
return false;
}
if (!interface_exists($type)) {
return false;
}
$r = new ReflectionClass($className);
return $r->implementsInterface($type);
}
}
12 changes: 12 additions & 0 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,16 @@ public function testWillNotCreateCircularReferences()
$foo = $sm->get('foo');
$this->assertSame($abstractFactory->expectedInstance, $foo);
}

public function testShouldAllowAddingInitializersAsClassNames()
{
$result = $this->serviceManager->addInitializer('ZendTest\ServiceManager\TestAsset\FooInitializer');
$this->assertSame($this->serviceManager, $result);
}

public function testShouldRaiseExceptionIfInitializerClassIsNotAnInitializerInterfaceImplementation()
{
$this->setExpectedException('Zend\ServiceManager\Exception\InvalidArgumentException');
$result = $this->serviceManager->addInitializer(get_class($this));
}
}

0 comments on commit 8e251cd

Please sign in to comment.