Skip to content

Commit 4cd6477

Browse files
committed
minor symfony#27481 [DI] Don't generate factories for errored services (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [DI] Don't generate factories for errored services | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Spotted while comparing the dumped container of Blackfire with Symfony 4.0 vs 4.1. Commits ------- a6b6206 [DI] Don't generate factories for errored services
2 parents 4f197a5 + a6b6206 commit 4cd6477

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class PhpDumper extends Dumper
7070
private $inlinedRequires = array();
7171
private $circularReferences = array();
7272
private $singleUsePrivateIds = array();
73+
private $addThrow = false;
7374

7475
/**
7576
* @var ProxyDumper
@@ -125,6 +126,7 @@ public function dump(array $options = array())
125126
'build_time' => time(),
126127
), $options);
127128

129+
$this->addThrow = false;
128130
$this->namespace = $options['namespace'];
129131
$this->asFiles = $options['as_files'];
130132
$this->hotPathTag = $options['hot_path_tag'];
@@ -556,10 +558,13 @@ private function addServiceInstance(string $id, Definition $definition, string $
556558

557559
private function isTrivialInstance(Definition $definition): bool
558560
{
561+
if ($definition->getErrors()) {
562+
return true;
563+
}
559564
if ($definition->isSynthetic() || $definition->getFile() || $definition->getMethodCalls() || $definition->getProperties() || $definition->getConfigurator()) {
560565
return false;
561566
}
562-
if ($definition->isDeprecated() || $definition->isLazy() || $definition->getFactory() || 3 < count($definition->getArguments()) || $definition->getErrors()) {
567+
if ($definition->isDeprecated() || $definition->isLazy() || $definition->getFactory() || 3 < count($definition->getArguments())) {
563568
return false;
564569
}
565570

@@ -1318,6 +1323,18 @@ private function exportParameters(array $parameters, string $path = '', int $ind
13181323

13191324
private function endClass(): string
13201325
{
1326+
if ($this->addThrow) {
1327+
return <<<'EOF'
1328+
1329+
protected function throw($message)
1330+
{
1331+
throw new RuntimeException($message);
1332+
}
1333+
}
1334+
1335+
EOF;
1336+
}
1337+
13211338
return <<<'EOF'
13221339
}
13231340

@@ -1525,6 +1542,11 @@ private function dumpValue($value, bool $interpolate = true): string
15251542
list($this->definitionVariables, $this->referenceVariables, $this->variableCount) = $scope;
15261543
}
15271544
} elseif ($value instanceof Definition) {
1545+
if ($e = $value->getErrors()) {
1546+
$this->addThrow = true;
1547+
1548+
return sprintf('$this->throw(%s)', $this->export(reset($e)));
1549+
}
15281550
if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
15291551
return $this->dumpValue($this->definitionVariables[$value], $interpolate);
15301552
}
@@ -1670,6 +1692,11 @@ private function getServiceCall(string $id, Reference $reference = null): string
16701692
return $code;
16711693
}
16721694
} elseif ($this->isTrivialInstance($definition)) {
1695+
if ($e = $definition->getErrors()) {
1696+
$this->addThrow = true;
1697+
1698+
return sprintf('$this->throw(%s)', $this->export(reset($e)));
1699+
}
16731700
$code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
16741701
if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) {
16751702
$code = sprintf('$this->%s[\'%s\'] = %s', $definition->isPublic() ? 'services' : 'privates', $id, $code);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,6 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
127127

128128
return $this->services['deprecated_service'] = new \stdClass();
129129

130-
[Container%s/getErroredDefinitionService.php] => <?php
131-
132-
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
133-
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
134-
135-
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
136-
// Returns the private 'errored_definition' shared service.
137-
138-
throw new RuntimeException('Service "errored_definition" is broken.');
139-
140130
[Container%s/getFactoryServiceService.php] => <?php
141131

142132
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
@@ -326,7 +316,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
326316
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
327317
// Returns the public 'runtime_error' shared service.
328318

329-
return $this->services['runtime_error'] = new \stdClass($this->load('getErroredDefinitionService.php'));
319+
return $this->services['runtime_error'] = new \stdClass($this->throw('Service "errored_definition" is broken.'));
330320

331321
[Container%s/getServiceFromStaticMethodService.php] => <?php
332322

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ protected function getNewFactoryServiceService()
381381
*/
382382
protected function getRuntimeErrorService()
383383
{
384-
return $this->services['runtime_error'] = new \stdClass($this->getErroredDefinitionService());
384+
return $this->services['runtime_error'] = new \stdClass($this->throw('Service "errored_definition" is broken.'));
385385
}
386386

387387
/**
@@ -407,16 +407,6 @@ protected function getTaggedIteratorService()
407407
}, 2));
408408
}
409409

410-
/**
411-
* Gets the private 'errored_definition' shared service.
412-
*
413-
* @return \stdClass
414-
*/
415-
protected function getErroredDefinitionService()
416-
{
417-
throw new RuntimeException('Service "errored_definition" is broken.');
418-
}
419-
420410
/**
421411
* Gets the private 'factory_simple' shared service.
422412
*
@@ -500,4 +490,9 @@ protected function getDefaultParameters()
500490
'foo' => 'bar',
501491
);
502492
}
493+
494+
protected function throw($message)
495+
{
496+
throw new RuntimeException($message);
497+
}
503498
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ protected function getNewFactoryServiceService()
381381
*/
382382
protected function getRuntimeErrorService()
383383
{
384-
return $this->services['runtime_error'] = new \stdClass($this->getErroredDefinitionService());
384+
return $this->services['runtime_error'] = new \stdClass($this->throw('Service "errored_definition" is broken.'));
385385
}
386386

387387
/**
@@ -407,16 +407,6 @@ protected function getTaggedIteratorService()
407407
}, 2));
408408
}
409409

410-
/**
411-
* Gets the private 'errored_definition' shared service.
412-
*
413-
* @return \stdClass
414-
*/
415-
protected function getErroredDefinitionService()
416-
{
417-
throw new RuntimeException('Service "errored_definition" is broken.');
418-
}
419-
420410
/**
421411
* Gets the private 'factory_simple' shared service.
422412
*
@@ -501,4 +491,9 @@ protected function getDefaultParameters()
501491
'foo_bar' => 'foo_bar',
502492
);
503493
}
494+
495+
protected function throw($message)
496+
{
497+
throw new RuntimeException($message);
498+
}
504499
}

0 commit comments

Comments
 (0)