Skip to content

Commit

Permalink
feature #24671 [DI] Handle container.autowiring.strict_mode to opt-ou…
Browse files Browse the repository at this point in the history
…t from legacy autowiring (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Handle container.autowiring.strict_mode to opt-out from legacy autowiring

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

To preserve BC, autowiring still wires things in hybrid 2.8/3.3 modes.
But 2.8 mode is really a foot gun.
I propose to add a new parameter in SF3.4, to opt-out of this 2.8 mode, and enable this strict mode for all new projects.
WDYT?
(see symfony/recipes#221 for corresponding change on Flex recipe)

Commits
-------

a4a0ae2 [DI] Handle container.autowiring.strict_mode to opt-out from legacy autowiring
  • Loading branch information
nicolas-grekas committed Oct 24, 2017
2 parents 7d97133 + a4a0ae2 commit 2a5759d
Showing 1 changed file with 6 additions and 2 deletions.
Expand Up @@ -34,6 +34,7 @@ class AutowirePass extends AbstractRecursivePass
private $lastFailure;
private $throwOnAutowiringException;
private $autowiringExceptions = array();
private $strictMode;

/**
* @param bool $throwOnAutowireException Errors can be retrieved via Definition::getErrors()
Expand Down Expand Up @@ -62,6 +63,7 @@ public function process(ContainerBuilder $container)
{
// clear out any possibly stored exceptions from before
$this->autowiringExceptions = array();
$this->strictMode = $container->hasParameter('container.autowiring.strict_mode') && $container->getParameter('container.autowiring.strict_mode');

try {
parent::process($container);
Expand Down Expand Up @@ -290,7 +292,7 @@ private function getAutowiredReference(TypedReference $reference, $deprecationMe
return new TypedReference($this->types[$type], $type);
}

if (isset($this->types[$type])) {
if (!$this->strictMode && isset($this->types[$type])) {
$message = 'Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won\'t be supported in version 4.0.';
if ($aliasSuggestion = $this->getAliasesSuggestionForType($type = $reference->getType(), $deprecationMessage)) {
$message .= ' '.$aliasSuggestion;
Expand All @@ -311,7 +313,9 @@ private function getAutowiredReference(TypedReference $reference, $deprecationMe
return $this->autowired[$type] ? new TypedReference($this->autowired[$type], $type) : null;
}

return $this->createAutowiredDefinition($type);
if (!$this->strictMode) {
return $this->createAutowiredDefinition($type);
}
}

/**
Expand Down

0 comments on commit 2a5759d

Please sign in to comment.