Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Deduplicate generated proxy classes #27568

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
Jun 10, 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
[DI] Deduplicate generated proxy classes
  • Loading branch information
nicolas-grekas committed Jun 9, 2018
commit 322f58b334105e09330809e91e9b09e27b7c1e99
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ private function collectLineage($class, array &$lineage)

private function generateProxyClasses()
{
$alreadyGenerated = array();
$definitions = $this->container->getDefinitions();
$strip = '' === $this->docStar && method_exists('Symfony\Component\HttpKernel\Kernel', 'stripComments');
$proxyDumper = $this->getProxyDumper();
Expand All @@ -404,8 +405,12 @@ private function generateProxyClasses()
if (!$proxyDumper->isProxyCandidate($definition)) {
continue;
}
if (isset($alreadyGenerated[$class = $definition->getClass()])) {
continue;
}
$alreadyGenerated[$class] = true;
// register class' reflector for resource tracking
$this->container->getReflectionClass($definition->getClass());
$this->container->getReflectionClass($class);
$proxyCode = "\n".$proxyDumper->getProxyCode($definition);
if ($strip) {
$proxyCode = "<?php\n".$proxyCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,19 @@ public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServic
$this->addToAssertionCount(1);
}

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

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

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

public function testLazyArgumentProvideGenerator()
{
require_once self::$fixturesPath.'/includes/classes.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public function isProxyCandidate(Definition $definition)

public function getProxyFactoryCode(Definition $definition, $id, $factoryCall = null)
{
return " // lazy factory\n\n";
return " // lazy factory for {$definition->getClass()}\n\n";
}

public function getProxyCode(Definition $definition)
{
return "// proxy code\n";
return "// proxy code for {$definition->getClass()}\n";
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
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.
*
* @final since Symfony 3.3
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

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

$this->aliases = array();
}

public function getRemovedIds()
{
return array(
'Psr\\Container\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
);
}

public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}

public function isCompiled()
{
return true;
}

public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);

return true;
}

protected function createProxy($class, \Closure $factory)
{
return $factory();
}

/**
* Gets the public 'bar' shared service.
*
* @return \stdClass
*/
protected function getBarService($lazyLoad = true)
{
// lazy factory for stdClass

return new \stdClass();
}

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

return new \stdClass();
}
}

// proxy code for stdClass
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ protected function getBarService()
*/
protected function getFooService($lazyLoad = true)
{
// lazy factory
// lazy factory for stdClass

return new \stdClass();
}
}

// proxy code
// proxy code for stdClass
Morty Proxy This is a proxified and sanitized view of the page, visit original site.