Permalink
Browse files

minor #27481 [DI] Don't generate factories for errored services (nico…

…las-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
  • Loading branch information...
fabpot committed Jun 5, 2018
2 parents 4f197a5 + a6b6206 commit 4cd64771c93ac7b2125b1f1033e0be4ff5eb4586
@@ -70,6 +70,7 @@ class PhpDumper extends Dumper
private $inlinedRequires = array();
private $circularReferences = array();
private $singleUsePrivateIds = array();
private $addThrow = false;
/**
* @var ProxyDumper
@@ -125,6 +126,7 @@ public function dump(array $options = array())
'build_time' => time(),
), $options);
$this->addThrow = false;
$this->namespace = $options['namespace'];
$this->asFiles = $options['as_files'];
$this->hotPathTag = $options['hot_path_tag'];
@@ -556,10 +558,13 @@ private function addServiceInstance(string $id, Definition $definition, string $
private function isTrivialInstance(Definition $definition): bool
{
if ($definition->getErrors()) {
return true;
}
if ($definition->isSynthetic() || $definition->getFile() || $definition->getMethodCalls() || $definition->getProperties() || $definition->getConfigurator()) {
return false;
}
if ($definition->isDeprecated() || $definition->isLazy() || $definition->getFactory() || 3 < count($definition->getArguments()) || $definition->getErrors()) {
if ($definition->isDeprecated() || $definition->isLazy() || $definition->getFactory() || 3 < count($definition->getArguments())) {
return false;
}
@@ -1318,6 +1323,18 @@ private function exportParameters(array $parameters, string $path = '', int $ind
private function endClass(): string
{
if ($this->addThrow) {
return <<<'EOF'
protected function throw($message)
{
throw new RuntimeException($message);
}
}
EOF;
}
return <<<'EOF'
}
@@ -1525,6 +1542,11 @@ private function dumpValue($value, bool $interpolate = true): string
list($this->definitionVariables, $this->referenceVariables, $this->variableCount) = $scope;
}
} elseif ($value instanceof Definition) {
if ($e = $value->getErrors()) {
$this->addThrow = true;
return sprintf('$this->throw(%s)', $this->export(reset($e)));
}
if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
return $this->dumpValue($this->definitionVariables[$value], $interpolate);
}
@@ -1670,6 +1692,11 @@ private function getServiceCall(string $id, Reference $reference = null): string
return $code;
}
} elseif ($this->isTrivialInstance($definition)) {
if ($e = $definition->getErrors()) {
$this->addThrow = true;
return sprintf('$this->throw(%s)', $this->export(reset($e)));
}
$code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) {
$code = sprintf('$this->%s[\'%s\'] = %s', $definition->isPublic() ? 'services' : 'privates', $id, $code);
@@ -127,16 +127,6 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
return $this->services['deprecated_service'] = new \stdClass();
[Container%s/getErroredDefinitionService.php] => <?php
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the private 'errored_definition' shared service.
throw new RuntimeException('Service "errored_definition" is broken.');
[Container%s/getFactoryServiceService.php] => <?php
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
@@ -326,7 +316,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the public 'runtime_error' shared service.
return $this->services['runtime_error'] = new \stdClass($this->load('getErroredDefinitionService.php'));
return $this->services['runtime_error'] = new \stdClass($this->throw('Service "errored_definition" is broken.'));
[Container%s/getServiceFromStaticMethodService.php] => <?php
@@ -381,7 +381,7 @@ protected function getNewFactoryServiceService()
*/
protected function getRuntimeErrorService()
{
return $this->services['runtime_error'] = new \stdClass($this->getErroredDefinitionService());
return $this->services['runtime_error'] = new \stdClass($this->throw('Service "errored_definition" is broken.'));
}
/**
@@ -407,16 +407,6 @@ protected function getTaggedIteratorService()
}, 2));
}
/**
* Gets the private 'errored_definition' shared service.
*
* @return \stdClass
*/
protected function getErroredDefinitionService()
{
throw new RuntimeException('Service "errored_definition" is broken.');
}
/**
* Gets the private 'factory_simple' shared service.
*
@@ -500,4 +490,9 @@ protected function getDefaultParameters()
'foo' => 'bar',
);
}
protected function throw($message)
{
throw new RuntimeException($message);
}
}
@@ -381,7 +381,7 @@ protected function getNewFactoryServiceService()
*/
protected function getRuntimeErrorService()
{
return $this->services['runtime_error'] = new \stdClass($this->getErroredDefinitionService());
return $this->services['runtime_error'] = new \stdClass($this->throw('Service "errored_definition" is broken.'));
}
/**
@@ -407,16 +407,6 @@ protected function getTaggedIteratorService()
}, 2));
}
/**
* Gets the private 'errored_definition' shared service.
*
* @return \stdClass
*/
protected function getErroredDefinitionService()
{
throw new RuntimeException('Service "errored_definition" is broken.');
}
/**
* Gets the private 'factory_simple' shared service.
*
@@ -501,4 +491,9 @@ protected function getDefaultParameters()
'foo_bar' => 'foo_bar',
);
}
protected function throw($message)
{
throw new RuntimeException($message);
}
}

0 comments on commit 4cd6477

Please sign in to comment.