[DI] Fix dumping service locators

This commit is contained in:
Nicolas Grekas 2018-08-09 21:02:29 +02:00
parent 00e8b493d1
commit e17f650754
2 changed files with 25 additions and 12 deletions

View File

@ -207,7 +207,7 @@ class PhpDumper extends Dumper
$code = $code =
$this->startClass($options['class'], $baseClass, $baseClassWithNamespace). $this->startClass($options['class'], $baseClass, $baseClassWithNamespace).
$this->addServices(). $this->addServices($services).
$this->addDefaultParametersMethod() $this->addDefaultParametersMethod()
; ;
@ -238,7 +238,7 @@ EOF;
$files['removed-ids.php'] = $c .= ");\n"; $files['removed-ids.php'] = $c .= ");\n";
} }
foreach ($this->generateServiceFiles() as $file => $c) { foreach ($this->generateServiceFiles($services) as $file => $c) {
$files[$file] = $fileStart.$c; $files[$file] = $fileStart.$c;
} }
foreach ($this->generateProxyClasses() as $file => $c) { foreach ($this->generateProxyClasses() as $file => $c) {
@ -708,7 +708,7 @@ EOTXT
return sprintf(" %s(\$%s);\n", $callable, $variableName); return sprintf(" %s(\$%s);\n", $callable, $variableName);
} }
private function addService(string $id, Definition $definition, string &$file = null): string private function addService(string $id, Definition $definition): array
{ {
$this->definitionVariables = new \SplObjectStorage(); $this->definitionVariables = new \SplObjectStorage();
$this->referenceVariables = array(); $this->referenceVariables = array();
@ -759,6 +759,7 @@ EOTXT
$file = $methodName.'.php'; $file = $methodName.'.php';
$code = " // Returns the $public '$id'$shared$autowired service.\n\n"; $code = " // Returns the $public '$id'$shared$autowired service.\n\n";
} else { } else {
$file = null;
$code = <<<EOF $code = <<<EOF
/*{$this->docStar} /*{$this->docStar}
@ -825,36 +826,38 @@ EOF;
$this->definitionVariables = null; $this->definitionVariables = null;
$this->referenceVariables = null; $this->referenceVariables = null;
return $code; return array($file, $code);
} }
private function addServices(): string private function addServices(array &$services = null): string
{ {
$publicServices = $privateServices = ''; $publicServices = $privateServices = '';
$definitions = $this->container->getDefinitions(); $definitions = $this->container->getDefinitions();
ksort($definitions); ksort($definitions);
foreach ($definitions as $id => $definition) { foreach ($definitions as $id => $definition) {
if ($definition->isSynthetic() || ($this->asFiles && !$this->isHotPath($definition))) { $services[$id] = $definition->isSynthetic() ? null : $this->addService($id, $definition);
}
foreach ($definitions as $id => $definition) {
if (!(list($file, $code) = $services[$id]) || null !== $file) {
continue; continue;
} }
if ($definition->isPublic()) { if ($definition->isPublic()) {
$publicServices .= $this->addService($id, $definition); $publicServices .= $code;
} elseif (!$this->isTrivialInstance($definition) || isset($this->locatedIds[$id])) { } elseif (!$this->isTrivialInstance($definition) || isset($this->locatedIds[$id])) {
$privateServices .= $this->addService($id, $definition); $privateServices .= $code;
} }
} }
return $publicServices.$privateServices; return $publicServices.$privateServices;
} }
private function generateServiceFiles() private function generateServiceFiles(array $services)
{ {
$definitions = $this->container->getDefinitions(); $definitions = $this->container->getDefinitions();
ksort($definitions); ksort($definitions);
foreach ($definitions as $id => $definition) { foreach ($definitions as $id => $definition) {
if (!$definition->isSynthetic() && !$this->isHotPath($definition) && ($definition->isPublic() || !$this->isTrivialInstance($definition) || isset($this->locatedIds[$id]))) { if ((list($file, $code) = $services[$id]) && null !== $file && ($definition->isPublic() || !$this->isTrivialInstance($definition) || isset($this->locatedIds[$id]))) {
$code = $this->addService($id, $definition, $file);
if (!$definition->isShared()) { if (!$definition->isShared()) {
$i = strpos($code, "\n\ninclude_once "); $i = strpos($code, "\n\ninclude_once ");
if (false !== $i && false !== $i = strpos($code, "\n\n", 2 + $i)) { if (false !== $i && false !== $i = strpos($code, "\n\n", 2 + $i)) {

View File

@ -86,4 +86,14 @@ class ProjectServiceContainer extends Container
'baz' => array('privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false), 'baz' => array('privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false),
)))->withContext('foo_service', $this)); )))->withContext('foo_service', $this));
} }
/**
* Gets the private 'Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition' shared service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition
*/
protected function getCustomDefinitionService()
{
return $this->privates['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition();
}
} }