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

Commit

Permalink
Remove Zend\Stdlib\SubClass
Browse files Browse the repository at this point in the history
The main reason for this class was to work around changes in PHP between
versions 5.3.3 and 5.3.7 regarding how is_subclass_of() works. In the earlier
versions, it was buggy. In 5.3.7 and up, the bugs are fixed, and the solution is
very fast.

There are very few places in the framework where an is_subclass_of() test makes
sense, and those are typically only with classes responsible for creating other
classes -- factories, primarily. As such, the number of places where this will
be of use is minimal, and can be maintained.

The main goal is to have a consistent, performant way to do the check when
required, and one which will work with case sensitive contexts. The solution by
@prolic in zendframework/zendframework#1807 is good; we just don't need to abstract this, particularly as
we'll be able to remove this entirely in ZF3. Additionally, abstracting it gives
additional dependencies for those components needing it, and in cases like
autoloaders, DI, and the service manager, the extra dependency is undesirable as
these should stand alone.

The code has been duplicated into each class requiring the lookup (I put the
check into AbstractPluginManager, as it's not unlikely other plugin managers may
need this).
  • Loading branch information
weierophinney committed Jul 11, 2012
1 parent d0c1a7e commit 9f76d61
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Zend\Di;

use Closure;
use Zend\Stdlib\SubClass;
use ReflectionClass;

class Di implements DependencyInjectionInterface
{
Expand Down Expand Up @@ -310,7 +310,7 @@ protected function handleInjectDependencies($instance, $injectionMethods, $param
if ($methodParams) {
foreach ($methodParams as $methodParam) {
$objectToInjectClass = $this->getClass($objectToInject);
if ($objectToInjectClass == $methodParam[1] || SubClass::isSubclassOf($objectToInjectClass, $methodParam[1])) {
if ($objectToInjectClass == $methodParam[1] || self::isSubclassOf($objectToInjectClass, $methodParam[1])) {
if ($this->resolveAndCallInjectionMethodForInstance($instance, $typeInjectionMethod, array($methodParam[0] => $objectToInject), $instanceAlias, true, $type)) {
$calledMethods[$typeInjectionMethod] = true;
}
Expand Down Expand Up @@ -583,7 +583,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
}
$pInstanceClass = ($this->instanceManager->hasAlias($pInstance)) ?
$this->instanceManager->getClassFromAlias($pInstance) : $pInstance;
if ($pInstanceClass === $type || SubClass::isSubclassOf($pInstanceClass, $type)) {
if ($pInstanceClass === $type || self::isSubclassOf($pInstanceClass, $type)) {
$computedParams['required'][$fqParamPos] = array($pInstance, $pInstanceClass);
continue 2;
}
Expand All @@ -600,7 +600,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
}
$pInstanceClass = ($this->instanceManager->hasAlias($pInstance)) ?
$this->instanceManager->getClassFromAlias($pInstance) : $pInstance;
if ($pInstanceClass === $type || SubClass::isSubclassOf($pInstanceClass, $type)) {
if ($pInstanceClass === $type || self::isSubclassOf($pInstanceClass, $type)) {
$computedParams['required'][$fqParamPos] = array($pInstance, $pInstanceClass);
continue 2;
}
Expand Down Expand Up @@ -682,4 +682,27 @@ protected function getClass($instance)
return get_class($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 (version_compare(PHP_VERSION, '5.3.7', '>=')) {
return is_subclass_of($className, $type);
}
if (is_subclass_of($className, $type)) {
return true;
}
if (!interface_exists($type)) {
return false;
}
$r = new ReflectionClass($className);
return $r->implementsInterface($type);
}
}

0 comments on commit 9f76d61

Please sign in to comment.