Skip to content

Commit f61bc24

Browse files
bug symfony#28182 [DI] Fix dumping service locators (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [DI] Fix dumping service locators | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes (master only) | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#28180 | License | MIT | Doc PR | - Services need to be visited to populate `$this->loadedIds` before using the property decide what needs to be dumped or not. Commits ------- e17f650 [DI] Fix dumping service locators
2 parents 00e8b49 + e17f650 commit f61bc24

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function dump(array $options = array())
207207

208208
$code =
209209
$this->startClass($options['class'], $baseClass, $baseClassWithNamespace).
210-
$this->addServices().
210+
$this->addServices($services).
211211
$this->addDefaultParametersMethod()
212212
;
213213

@@ -238,7 +238,7 @@ public function dump(array $options = array())
238238
$files['removed-ids.php'] = $c .= ");\n";
239239
}
240240

241-
foreach ($this->generateServiceFiles() as $file => $c) {
241+
foreach ($this->generateServiceFiles($services) as $file => $c) {
242242
$files[$file] = $fileStart.$c;
243243
}
244244
foreach ($this->generateProxyClasses() as $file => $c) {
@@ -708,7 +708,7 @@ private function addServiceConfigurator(Definition $definition, string $variable
708708
return sprintf(" %s(\$%s);\n", $callable, $variableName);
709709
}
710710

711-
private function addService(string $id, Definition $definition, string &$file = null): string
711+
private function addService(string $id, Definition $definition): array
712712
{
713713
$this->definitionVariables = new \SplObjectStorage();
714714
$this->referenceVariables = array();
@@ -759,6 +759,7 @@ private function addService(string $id, Definition $definition, string &$file =
759759
$file = $methodName.'.php';
760760
$code = " // Returns the $public '$id'$shared$autowired service.\n\n";
761761
} else {
762+
$file = null;
762763
$code = <<<EOF
763764
764765
/*{$this->docStar}
@@ -825,36 +826,38 @@ protected function {$methodName}($lazyInitialization)
825826
$this->definitionVariables = null;
826827
$this->referenceVariables = null;
827828

828-
return $code;
829+
return array($file, $code);
829830
}
830831

831-
private function addServices(): string
832+
private function addServices(array &$services = null): string
832833
{
833834
$publicServices = $privateServices = '';
834835
$definitions = $this->container->getDefinitions();
835836
ksort($definitions);
836837
foreach ($definitions as $id => $definition) {
837-
if ($definition->isSynthetic() || ($this->asFiles && !$this->isHotPath($definition))) {
838+
$services[$id] = $definition->isSynthetic() ? null : $this->addService($id, $definition);
839+
}
840+
841+
foreach ($definitions as $id => $definition) {
842+
if (!(list($file, $code) = $services[$id]) || null !== $file) {
838843
continue;
839844
}
840845
if ($definition->isPublic()) {
841-
$publicServices .= $this->addService($id, $definition);
846+
$publicServices .= $code;
842847
} elseif (!$this->isTrivialInstance($definition) || isset($this->locatedIds[$id])) {
843-
$privateServices .= $this->addService($id, $definition);
848+
$privateServices .= $code;
844849
}
845850
}
846851

847852
return $publicServices.$privateServices;
848853
}
849854

850-
private function generateServiceFiles()
855+
private function generateServiceFiles(array $services)
851856
{
852857
$definitions = $this->container->getDefinitions();
853858
ksort($definitions);
854859
foreach ($definitions as $id => $definition) {
855-
if (!$definition->isSynthetic() && !$this->isHotPath($definition) && ($definition->isPublic() || !$this->isTrivialInstance($definition) || isset($this->locatedIds[$id]))) {
856-
$code = $this->addService($id, $definition, $file);
857-
860+
if ((list($file, $code) = $services[$id]) && null !== $file && ($definition->isPublic() || !$this->isTrivialInstance($definition) || isset($this->locatedIds[$id]))) {
858861
if (!$definition->isShared()) {
859862
$i = strpos($code, "\n\ninclude_once ");
860863
if (false !== $i && false !== $i = strpos($code, "\n\n", 2 + $i)) {

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,14 @@ protected function getFooServiceService()
8686
'baz' => array('privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false),
8787
)))->withContext('foo_service', $this));
8888
}
89+
90+
/**
91+
* Gets the private 'Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition' shared service.
92+
*
93+
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition
94+
*/
95+
protected function getCustomDefinitionService()
96+
{
97+
return $this->privates['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition();
98+
}
8999
}

0 commit comments

Comments
 (0)