Skip to content

Commit

Permalink
minor #27481 [DI] Don't generate factories for errored services (nico…
Browse files Browse the repository at this point in the history
…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 4cd6477
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
29 changes: 28 additions & 1 deletion src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -70,6 +70,7 @@ class PhpDumper extends Dumper
private $inlinedRequires = array();
private $circularReferences = array();
private $singleUsePrivateIds = array();
private $addThrow = false;

/**
* @var ProxyDumper
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down
Expand Up @@ -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.'));
}

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -500,4 +490,9 @@ protected function getDefaultParameters()
'foo' => 'bar',
);
}

protected function throw($message)
{
throw new RuntimeException($message);
}
}
Expand Up @@ -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.'));
}

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -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.