Skip to content

[HttpKernel] Fix mem usage when stripping the prod container #18048

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
Mar 9, 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
27 changes: 20 additions & 7 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\NullDumper;
use Symfony\Component\HttpKernel\Kernel;

/**
* PhpDumper dumps a service container as a PHP class.
Expand Down Expand Up @@ -53,6 +54,7 @@ class PhpDumper extends Dumper
private $reservedVariables = array('instance', 'class');
private $targetDirRegex;
private $targetDirMaxMatches;
private $docStar;

/**
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
Expand Down Expand Up @@ -97,7 +99,9 @@ public function dump(array $options = array())
$options = array_merge(array(
'class' => 'ProjectServiceContainer',
'base_class' => 'Container',
'debug' => true,
), $options);
$this->docStar = $options['debug'] ? '*' : '';

if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
// Build a regexp where the first root dirs are mandatory,
Expand Down Expand Up @@ -219,9 +223,15 @@ private function addProxyClasses()
array($this->getProxyDumper(), 'isProxyCandidate')
);
$code = '';
$strip = '' === $this->docStar && method_exists('Symfony\Component\HttpKernel\Kernel', 'stripComments');

foreach ($definitions as $definition) {
$code .= "\n".$this->getProxyDumper()->getProxyCode($definition);
$proxyCode = "\n".$this->getProxyDumper()->getProxyCode($definition);
if ($strip) {
$proxyCode = "<?php\n".$proxyCode;
$proxyCode = substr(Kernel::stripComments($proxyCode), 5);
}
$code .= $proxyCode;
}

return $code;
Expand Down Expand Up @@ -589,7 +599,7 @@ private function addService($id, $definition)
$visibility = $isProxyCandidate ? 'public' : 'protected';
$code = <<<EOF

/**
/*{$this->docStar}
* Gets the '$id' service.$doc
*$lazyInitializationDoc
* $return
Expand Down Expand Up @@ -699,7 +709,7 @@ private function addServiceSynchronizer($id, Definition $definition)

return <<<EOF

/**
/*{$this->docStar}
* Updates the '$id' service.
*/
protected function synchronize{$this->camelize($id)}Service()
Expand Down Expand Up @@ -760,7 +770,7 @@ private function startClass($class, $baseClass)
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
$bagClass

/**
/*{$this->docStar}
* $class.
*
* This class has been auto-generated
Expand All @@ -786,7 +796,7 @@ private function addConstructor()

$code = <<<EOF

/**
/*{$this->docStar}
* Constructor.
*/
public function __construct()
Expand Down Expand Up @@ -823,7 +833,7 @@ private function addFrozenConstructor()

$code = <<<EOF

/**
/*{$this->docStar}
* Constructor.
*/
public function __construct()
Expand Down Expand Up @@ -970,11 +980,14 @@ public function getParameterBag()
return $this->parameterBag;
}
EOF;
if ('' === $this->docStar) {
$code = str_replace('/**', '/*', $code);
}
}

$code .= <<<EOF

/**
/*{$this->docStar}
* Gets the default parameters.
*
* @return array An array of the default parameters
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,7 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
$dumper->setProxyDumper(new ProxyDumper(md5((string) $cache)));
}

$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => (string) $cache));
if (!$this->debug) {
$content = static::stripComments($content);
}
$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => (string) $cache, 'debug' => $this->debug));

$cache->write($content, $container->getResources());
}
Expand Down