Skip to content

[DependencyInjection] PhpDumper.php: hasReference() shouldn't search references in lazy service. #19902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2016
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,19 @@ private function checkOutEdges(array $edges)
$id = $node->getId();

if (empty($this->checkedNodes[$id])) {
$searchKey = array_search($id, $this->currentPath);
$this->currentPath[] = $id;

if (false !== $searchKey) {
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
}
// * don't check circular dependencies in lazy services.
$isLazy = $node->getValue() && $node->getValue()->isLazy();
if (!$isLazy) {
$searchKey = array_search($id, $this->currentPath);
$this->currentPath[] = $id;

if (false !== $searchKey) {
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
}

$this->checkOutEdges($node->getOutEdges());
$this->checkOutEdges($node->getOutEdges());
}

$this->checkedNodes[$id] = true;
array_pop($this->currentPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,13 @@ private function hasReference($id, array $arguments, $deep = false, array &$visi
$visited[$argumentId] = true;

$service = $this->container->getDefinition($argumentId);

// if exists proxy dumper (proxy-manager) don't search references in lazy services.
// As these services will be instantiated lazily and don't have direct related references.
if ($service->isLazy() && !($this->getProxyDumper() instanceof NullDumper)) {
continue;
}

$arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties());

if ($this->hasReference($id, $arguments, $deep, $visited)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Tests\Dumper;

use DummyProxyDumper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand All @@ -19,6 +20,8 @@
use Symfony\Component\DependencyInjection\Variable;
use Symfony\Component\ExpressionLanguage\Expression;

require_once __DIR__.'/../Fixtures/includes/classes.php';

class PhpDumperTest extends \PHPUnit_Framework_TestCase
{
protected static $fixturesPath;
Expand Down Expand Up @@ -286,4 +289,52 @@ public function testInitializePropertiesBeforeMethodCalls()
$container = new \Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls();
$this->assertTrue($container->get('bar')->callPassed(), '->dump() initializes properties before method calls');
}

public function testCircularReferenceAllowanceForLazyServices()
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->addArgument(new Reference('bar'));
$container->register('bar', 'stdClass')->setLazy(true)->addArgument(new Reference('foo'));
$container->compile();

$dumper = new PhpDumper($container);
$dumper->dump();
}

public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServices()
{
/*
* test graph:
* [connection] -> [event_manager] --> [entity_manager](lazy)
* |
* --(call)- addEventListener ("@lazy_service")
*
* [lazy_service](lazy) -> [entity_manager](lazy)
*
*/

$container = new ContainerBuilder();

$eventManagerDefinition = new Definition('stdClass');

$connectionDefinition = $container->register('connection', 'stdClass');
$connectionDefinition->addArgument($eventManagerDefinition);

$container->register('entity_manager', 'stdClass')
->setLazy(true)
->addArgument(new Reference('connection'));

$lazyServiceDefinition = $container->register('lazy_service', 'stdClass');
$lazyServiceDefinition->setLazy(true);
$lazyServiceDefinition->addArgument(new Reference('entity_manager'));

$eventManagerDefinition->addMethodCall('addEventListener', array(new Reference('lazy_service')));

$container->compile();

$dumper = new PhpDumper($container);

$dumper->setProxyDumper(new DummyProxyDumper());
$dumper->dump();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;

function sc_configure($instance)
{
$instance->configure();
Expand Down Expand Up @@ -76,3 +79,30 @@ public function callPassed()
return $this->callPassed;
}
}

class DummyProxyDumper implements ProxyDumper
{
/**
* {@inheritdoc}
*/
public function isProxyCandidate(Definition $definition)
{
return false;
}

/**
* {@inheritdoc}
*/
public function getProxyFactoryCode(Definition $definition, $id)
{
return '';
}

/**
* {@inheritdoc}
*/
public function getProxyCode(Definition $definition)
{
return '';
}
}