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

Detect cyclic dependency using reflection abstract factory #261

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/AbstractFactory/ReflectionBasedAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionParameter;
use ReflectionProperty;
use Zend\ServiceManager\Exception\InvalidServiceException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceManager;

use function array_map;
use function class_exists;
Expand Down Expand Up @@ -197,8 +200,10 @@ private function resolveParameterWithConfigService(ContainerInterface $container
* @param ContainerInterface $container
* @param string $requestedName
* @return mixed
* @throws \Zend\ServiceManager\Exception\InvalidServiceException
* @throws ServiceNotFoundException If type-hinted parameter cannot be
* resolved to a service in the container.
* @throws \ReflectionException
*/
private function resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, $requestedName)
{
Expand All @@ -223,6 +228,19 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
$type = $this->aliases[$type] ?? $type;

if ($container->has($type)) {
if ($container instanceof ServiceManager) {
$aliasProperty = new ReflectionProperty($container, 'aliases');
$aliasProperty->setAccessible(true);
$serviceManagerAliases = $aliasProperty->getValue($container);
$unaliasedService = array_search($requestedName, $serviceManagerAliases, true);

if (false !== $unaliasedService && $unaliasedService === $type) {
throw new InvalidServiceException(sprintf(
'Unable to create service "%s"; a cyclic dependency was detected',
$unaliasedService
));
}
}
return $container->get($type);
}

Expand Down
24 changes: 24 additions & 0 deletions test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
use Zend\ServiceManager\Exception\InvalidServiceException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\ServiceManager;
use ZendTest\ServiceManager\AbstractFactory\TestAsset\SampleInterface;

use function sprintf;

Expand Down Expand Up @@ -173,4 +176,25 @@ public function testFactoryWillUseDefaultValueForTypeHintedArgument()
$this->assertInstanceOf(TestAsset\ClassWithTypehintedDefaultValue::class, $instance);
$this->assertNull($instance->value);
}

public function testFactoryThrowsExceptionWhenCyclicDependencyDetectedForDecorators(): void
{
$serviceManager = new ServiceManager();
$serviceManager->setAlias(
TestAsset\SampleInterface::class,
TestAsset\DecoratorWithCyclicDependencyOnInterface::class
);
$serviceManager->addAbstractFactory(ReflectionBasedAbstractFactory::class);
$serviceManager->setFactory(
TestAsset\DecoratorWithCyclicDependencyOnInterface::class,
ReflectionBasedAbstractFactory::class
);

$this->expectException(InvalidServiceException::class);
$this->expectExceptionMessage(sprintf(
'Unable to create service "%s"; a cyclic dependency was detected',
TestAsset\SampleInterface::class
));
$serviceManager->get(SampleInterface::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\ServiceManager\AbstractFactory\TestAsset;

final class DecoratorWithCyclicDependencyOnInterface implements SampleInterface
{
public function __construct(SampleInterface $wrapped)
{
}
}