From 5784352cedcd7b942bf3e08e46d9f99d214a3075 Mon Sep 17 00:00:00 2001 From: Ralph Schindler Date: Wed, 29 Jun 2011 10:59:01 -0500 Subject: [PATCH] Fixed Zend\Di\DependnecyInjector matching of preferred classes with is_subclass_of due to a bug in PHP: https://bugs.php.net/bug.php?id=53727 --- src/DependencyInjector.php | 23 +++++++++++++++----- test/DependencyInjectorTest.php | 20 +++++++++++++++++ test/TestAsset/PreferredImplClasses/A.php | 7 ++++++ test/TestAsset/PreferredImplClasses/BofA.php | 7 ++++++ test/TestAsset/PreferredImplClasses/C.php | 12 ++++++++++ test/TestAsset/PreferredImplClasses/D.php | 8 +++++++ 6 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 test/TestAsset/PreferredImplClasses/A.php create mode 100644 test/TestAsset/PreferredImplClasses/BofA.php create mode 100644 test/TestAsset/PreferredImplClasses/C.php create mode 100644 test/TestAsset/PreferredImplClasses/D.php diff --git a/src/DependencyInjector.php b/src/DependencyInjector.php index 54fa04d0..ccf6cc26 100755 --- a/src/DependencyInjector.php +++ b/src/DependencyInjector.php @@ -288,6 +288,20 @@ protected function handleInjectionMethodForObject($object, $method, $params, $al */ protected function resolveMethodParameters($class, $method, array $userParams, $isInstantiator, $alias) { + static $isSubclassFunc = null; + static $isSubclassFuncCache = null; + + $isSubclassFunc = function($class, $type) use (&$isSubclassFuncCache) { + /* @see https://bugs.php.net/bug.php?id=53727 */ + if ($isSubclassFuncCache === null) { + $isSubclassFuncCache = array(); + } + if (!array_key_exists($class, $isSubclassFuncCache)) { + $isSubclassFuncCache[$class] = class_parents($class, true) + class_implements($class, true); + } + return (isset($isSubclassFuncCache[$class][$type])); + }; + $resolvedParams = array(); $injectionMethodParameters = $this->definition->getInjectionMethodParameters($class, $method); @@ -296,7 +310,6 @@ protected function resolveMethodParameters($class, $method, array $userParams, $ $computedLookupParams = array(); foreach ($injectionMethodParameters as $name => $type) { - //$computedValueParams[$name] = null; // first consult user provided parameters if (isset($userParams[$name])) { @@ -326,9 +339,9 @@ protected function resolveMethodParameters($class, $method, array $userParams, $ foreach ($pInstances as $pInstance) { $pInstanceClass = ($this->instanceManager->hasAlias($pInstance)) ? $this->instanceManager->getClassFromAlias($pInstance) : $pInstance; - if ($pInstanceClass === $type || is_subclass_of($pInstanceClass, $type)) { + if ($pInstanceClass === $type || $isSubclassFunc($pInstanceClass, $type)) { $computedLookupParams[$name] = array($pInstance, $pInstanceClass); - continue; + continue 2; } } } @@ -339,9 +352,9 @@ protected function resolveMethodParameters($class, $method, array $userParams, $ foreach ($pInstances as $pInstance) { $pInstanceClass = ($this->instanceManager->hasAlias($pInstance)) ? $this->instanceManager->getClassFromAlias($pInstance) : $pInstance; - if ($pInstanceClass === $type || is_subclass_of($pInstanceClass, $type)) { + if ($pInstanceClass === $type || $isSubclassFunc($pInstanceClass, $type)) { $computedLookupParams[$name] = array($pInstance, $pInstanceClass); - continue; + continue 2; } } } diff --git a/test/DependencyInjectorTest.php b/test/DependencyInjectorTest.php index 697fdb30..0a6d3404 100755 --- a/test/DependencyInjectorTest.php +++ b/test/DependencyInjectorTest.php @@ -279,4 +279,24 @@ public function testNewInstanceThrowsExceptionWhenEnteringInMiddleOfCircularDepe $di->newInstance('ZendTest\Di\TestAsset\CircularClasses\D'); } + /** + * Fix for PHP bug in is_subclass_of + * + * @see https://bugs.php.net/bug.php?id=53727 + */ + public function testNewInstanceWillUsePreferredClassForInterfaceHints() + { + $di = new DependencyInjector(); + $di->getInstanceManager()->addPreferredInstance( + 'ZendTest\Di\TestAsset\PreferredImplClasses\A', + 'ZendTest\Di\TestAsset\PreferredImplClasses\BofA' + ); + + $c = $di->get('ZendTest\Di\TestAsset\PreferredImplClasses\C'); + $a = $c->a; + $this->assertType('ZendTest\Di\TestAsset\PreferredImplClasses\BofA', $a); + $d = $di->get('ZendTest\Di\TestAsset\PreferredImplClasses\D'); + $this->assertSame($a, $d->a); + } + } diff --git a/test/TestAsset/PreferredImplClasses/A.php b/test/TestAsset/PreferredImplClasses/A.php new file mode 100644 index 00000000..15dd8604 --- /dev/null +++ b/test/TestAsset/PreferredImplClasses/A.php @@ -0,0 +1,7 @@ +a = $a; + } +} \ No newline at end of file diff --git a/test/TestAsset/PreferredImplClasses/D.php b/test/TestAsset/PreferredImplClasses/D.php new file mode 100644 index 00000000..c5993a27 --- /dev/null +++ b/test/TestAsset/PreferredImplClasses/D.php @@ -0,0 +1,8 @@ +