Skip to content

Commit

Permalink
[DI] Allow dumping the container in one file instead of many files
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx authored and nicolas-grekas committed Jul 17, 2019
1 parent 52e9fb9 commit c95a05a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

* added support for dumping the container in one file instead of many files
* deprecated support for short factories and short configurators in Yaml
* deprecated `tagged` in favor of `tagged_iterator`
* deprecated passing an instance of `Symfony\Component\DependencyInjection\Parameter` as class name to `Symfony\Component\DependencyInjection\Definition`
Expand Down
35 changes: 24 additions & 11 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class PhpDumper extends Dumper
private $namespace;
private $asFiles;
private $hotPathTag;
private $inlineFactories;
private $inlineRequires;
private $inlinedRequires = [];
private $circularReferences = [];
Expand Down Expand Up @@ -134,6 +135,7 @@ public function dump(array $options = [])
'as_files' => false,
'debug' => true,
'hot_path_tag' => 'container.hot_path',
'inline_factories_parameter' => 'container.dumper.inline_factories',
'inline_class_loader_parameter' => 'container.dumper.inline_class_loader',
'service_locator_tag' => 'container.service_locator',
'build_time' => time(),
Expand All @@ -143,6 +145,7 @@ public function dump(array $options = [])
$this->namespace = $options['namespace'];
$this->asFiles = $options['as_files'];
$this->hotPathTag = $options['hot_path_tag'];
$this->inlineFactories = $options['inline_factories_parameter'] && $this->container->hasParameter($options['inline_factories_parameter']) && $this->container->getParameter($options['inline_factories_parameter']);
$this->inlineRequires = $options['inline_class_loader_parameter'] && $this->container->hasParameter($options['inline_class_loader_parameter']) && $this->container->getParameter($options['inline_class_loader_parameter']);
$this->serviceLocatorTag = $options['service_locator_tag'];

Expand Down Expand Up @@ -257,14 +260,24 @@ public function dump(array $options = [])
}
$files['removed-ids.php'] = $c .= "];\n";
}

foreach ($this->generateServiceFiles($services) as $file => $c) {
$files[$file] = $fileStart.$c;
if (!$this->inlineFactories) {
foreach ($this->generateServiceFiles($services) as $file => $c) {
$files[$file] = $fileStart.$c;
}
foreach ($this->generateProxyClasses() as $file => $c) {
$files[$file] = "<?php\n".$c;
}
}
foreach ($this->generateProxyClasses() as $file => $c) {
$files[$file] = "<?php\n".$c;

$code .= $this->endClass();

if ($this->inlineFactories) {
foreach ($this->generateProxyClasses() as $c) {
$code .= $c;
}
}
$files[$options['class'].'.php'] = $code.$this->endClass();

$files[$options['class'].'.php'] = $code;
$hash = ucfirst(strtr(ContainerBuilder::hash($files), '._', 'xx'));
$code = [];

Expand Down Expand Up @@ -685,7 +698,7 @@ private function addService(string $id, Definition $definition): array
$lazyInitialization = '';
}

$asFile = $this->asFiles && !$this->isHotPath($definition);
$asFile = $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition);
$methodName = $this->generateMethodName($id);
if ($asFile) {
$file = $methodName.'.php';
Expand Down Expand Up @@ -1140,7 +1153,7 @@ private function addMethodMap(): string
$definitions = $this->container->getDefinitions();
ksort($definitions);
foreach ($definitions as $id => $definition) {
if (!$definition->isSynthetic() && $definition->isPublic() && (!$this->asFiles || $this->isHotPath($definition))) {
if (!$definition->isSynthetic() && $definition->isPublic() && (!$this->asFiles || $this->inlineFactories || $this->isHotPath($definition))) {
$code .= ' '.$this->doExport($id).' => '.$this->doExport($this->generateMethodName($id)).",\n";
}
}
Expand All @@ -1163,7 +1176,7 @@ private function addFileMap(): string
$definitions = $this->container->getDefinitions();
ksort($definitions);
foreach ($definitions as $id => $definition) {
if (!$definition->isSynthetic() && $definition->isPublic() && !$this->isHotPath($definition)) {
if (!$definition->isSynthetic() && $definition->isPublic() && !$this->inlineFactories && !$this->isHotPath($definition)) {
$code .= sprintf(" %s => '%s.php',\n", $this->doExport($id), $this->generateMethodName($id));
}
}
Expand Down Expand Up @@ -1578,7 +1591,7 @@ private function dumpValue($value, bool $interpolate = true): string
continue;
}
$definition = $this->container->findDefinition($id = (string) $v);
$load = !($definition->hasErrors() && $e = $definition->getErrors()) ? $this->asFiles && !$this->isHotPath($definition) : reset($e);
$load = !($definition->hasErrors() && $e = $definition->getErrors()) ? $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) : reset($e);
$serviceMap .= sprintf("\n %s => [%s, %s, %s, %s],",
$this->export($k),
$this->export($definition->isShared() ? ($definition->isPublic() ? 'services' : 'privates') : false),
Expand Down Expand Up @@ -1716,7 +1729,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
$code = sprintf('$this->%s[%s] = %s', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code);
}
$code = "($code)";
} elseif ($this->asFiles && !$this->isHotPath($definition)) {
} elseif ($this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition)) {
$code = sprintf("\$this->load('%s.php')", $this->generateMethodName($id));
if (!$definition->isShared()) {
$factory = sprintf('$this->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id));
Expand Down
11 changes: 2 additions & 9 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,23 +486,16 @@ protected function initializeContainer()
if ($fresh = $cache->isFresh()) {
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
$fresh = $oldContainer = false;
try {
if (file_exists($cache->getPath()) && \is_object($this->container = include $cache->getPath())) {
$this->container->set('kernel', $this);
$oldContainer = $this->container;
$fresh = true;
}
} catch (\Throwable $e) {
} finally {
error_reporting($errorLevel);
}
}

if ($fresh) {
return;
}

if ($this->debug) {
$collectedLogs = [];
$previousHandler = \defined('PHPUNIT_COMPOSER_INSTALL');
Expand Down Expand Up @@ -557,7 +550,7 @@ protected function initializeContainer()
}
}

if (null === $oldContainer && file_exists($cache->getPath())) {
if (!$fresh && file_exists($cache->getPath())) {
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
try {
$oldContainer = include $cache->getPath();
Expand All @@ -566,7 +559,7 @@ protected function initializeContainer()
error_reporting($errorLevel);
}
}
$oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : false;
$oldContainer = \is_object($oldContainer) ? new \ReflectionClass($oldContainer) : null;

$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
$this->container = require $cache->getPath();
Expand Down

0 comments on commit c95a05a

Please sign in to comment.