8000 [DI] never inline lazy services by nicolas-grekas · Pull Request #27366 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] never inline lazy services #27366

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
May 25, 2018
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 @@ -106,11 +106,15 @@ private function inlineArguments(ContainerBuilder $container, array $arguments,
*/
private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
{
if ($definition->isDeprecated() || $definition->isLazy() || $definition->isSynthetic()) {
return false;
}

if (!$definition->isShared() || ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
return true;
}

if ($definition->isDeprecated() || $definition->isPublic() || $definition->isLazy()) {
if ($definition->isPublic()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\DependencyInjection\Tests\Dumper;

use DummyProxyDumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
Expand Down Expand Up @@ -278,6 +277,19 @@ public function testInlinedDefinitionReferencingServiceContainer()
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
}

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

$dumper = new PhpDumper($container);
$dumper->setProxyDumper(new \DummyProxyDumper());

$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_non_shared_lazy.php', $dumper->dump());
}

public function testInitializePropertiesBeforeMethodCalls()
{
require_once self::$fixturesPath.'/includes/classes.php';
Expand Down Expand Up @@ -343,7 +355,7 @@ public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServic

$dumper = new PhpDumper($container);

$dumper->setProxyDumper(new DummyProxyDumper());
$dumper->setProxyDumper(new \DummyProxyDumper());
$dumper->dump();

$this->addToAssertionCount(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ class DummyProxyDumper implements ProxyDumper
{
public function isProxyCandidate(Definition $definition)
{
return false;
return $definition->isLazy();
}

public function getProxyFactoryCode(Definition $definition, $id)
{
return '';
return " // lazy factory\n\n";
}

public function getProxyCode(Definition $definition)
{
return '';
return "// proxy code\n";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

public function __construct()
{
$this->services =
$this->scopedServices =
$this->scopeStacks = array();
$this->scopes = array();
$this->scopeChildren = array();
$this->methodMap = array(
'bar' => 'getBarService',
'foo' => 'getFooService',
);

$this->aliases = array();
}

/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}

/**
* {@inheritdoc}
*/
public function isFrozen()
{
return true;
}

/**
* Gets the public 'bar' shared service.
*
* @return \stdClass
*/
protected function getBarService()
{
return $this->services['bar'] = new \stdClass($this->get('foo'));
}

/**
* Gets the public 'foo' service.
*
* @return \stdClass
*/
public function getFooService($lazyLoad = true)
{
// lazy factory

return new \stdClass();
}
}

// proxy code
0