Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #21306 [DependencyInjection] Always autowire the constructor …
…(dunglas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DependencyInjection] Always autowire the constructor

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no (because method autowiring has been introduced in 3.3)
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Always try to autowire the constructor even if it has not been configured explicitly. It doesn't make sense to autowire some methods but not the constructor. It will also allow to write shorter definitions when using method autowiring:

```yaml
services:
    Foo\Bar: { autowire: ['set*'] }
```

instead of

```yaml
services:
    Foo\Bar: { autowire: ['__construct', 'set*'] }
```

Commits
-------

be3d11f [DependencyInjection] Always autowire the constructor
  • Loading branch information
fabpot committed Jan 16, 2017
2 parents 8399208 + be3d11f commit f8b02ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Expand Up @@ -120,14 +120,22 @@ private function completeDefinition($id, Definition $definition, array $autowire
*
* @param string $id
* @param \ReflectionClass $reflectionClass
* @param string[] $autowiredMethods
* @param string[] $configuredAutowiredMethods
*
* @return \ReflectionMethod[]
*/
private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, array $autowiredMethods)
private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, array $configuredAutowiredMethods)
{
$found = array();
$regexList = array();

// Always try to autowire the constructor
if (in_array('__construct', $configuredAutowiredMethods, true)) {
$autowiredMethods = $configuredAutowiredMethods;
} else {
$autowiredMethods = array_merge(array('__construct'), $configuredAutowiredMethods);
}

foreach ($autowiredMethods as $pattern) {
$regexList[] = '/^'.str_replace('\*', '.*', preg_quote($pattern, '/')).'$/i';
}
Expand All @@ -147,7 +155,7 @@ private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, ar
}
}

if ($notFound = array_diff($autowiredMethods, $found)) {
if ($notFound = array_diff($configuredAutowiredMethods, $found)) {
$compiler = $this->container->getCompiler();
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatUnusedAutowiringPatterns($this, $id, $notFound));
}
Expand Down
Expand Up @@ -440,7 +440,7 @@ public function testSetterInjection()
// manually configure *one* call, to override autowiring
$container
->register('setter_injection', SetterInjection::class)
->setAutowiredMethods(array('__construct', 'set*'))
->setAutowiredMethods(array('set*'))
->addMethodCall('setWithCallsConfigured', array('manual_arg1', 'manual_arg2'))
;

Expand Down Expand Up @@ -557,7 +557,7 @@ public function testSetterInjectionCollisionThrowsException()
$container->register('c1', CollisionA::class);
$container->register('c2', CollisionB::class);
$aDefinition = $container->register('setter_injection_collision', SetterInjectionCollision::class);
$aDefinition->setAutowiredMethods(array('__construct', 'set*'));
$aDefinition->setAutowiredMethods(array('set*'));

$pass = new AutowirePass();
$pass->process($container);
Expand Down

0 comments on commit f8b02ed

Please sign in to comment.