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
-------

be3d11faa9 [DependencyInjection] Always autowire the constructor
This commit is contained in:
Fabien Potencier 2017-01-16 08:56:00 -08:00
commit f8b02ed038
2 changed files with 13 additions and 5 deletions

View File

@ -120,14 +120,22 @@ class AutowirePass implements CompilerPassInterface
*
* @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';
}
@ -147,7 +155,7 @@ class AutowirePass implements CompilerPassInterface
}
}
if ($notFound = array_diff($autowiredMethods, $found)) {
if ($notFound = array_diff($configuredAutowiredMethods, $found)) {
$compiler = $this->container->getCompiler();
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatUnusedAutowiringPatterns($this, $id, $notFound));
}

View File

@ -440,7 +440,7 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
// 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'))
;
@ -557,7 +557,7 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
$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);