8000 feature #21306 [DependencyInjection] Always autowire the constructor … · symfony/symfony@f8b02ed · GitHub
[go: up one dir, main page]

Skip to content

Commit f8b02ed

Browse files
committed
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
2 parents 8399208 + be3d11f commit f8b02ed

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,22 @@ private function completeDefinition($id, Definition $definition, array $autowire
120120
*
121121
* @param string $id
122122
* @param \ReflectionClass $reflectionClass
123-
* @param string[] $autowiredMethods
123+
* @param string[] $configuredAutowiredMethods
124124
*
125125
* @return \ReflectionMethod[]
126126
*/
127-
private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, array $autowiredMethods)
127+
private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, array $configuredAutowiredMethods)
128128
{
129129
$found = array();
130130
$regexList = array();
131+
132+
// Always try to autowire the constructor
133+
if (in_array('__construct', $configuredAutowiredMethods, true)) {
134+
$autowiredMethods = $configuredAutowiredMethods;
135+
} else {
136+
$autowiredMethods = array_merge(array('__construct'), $configuredAutowiredMethods);
137+
}
138+
131139
foreach ($autowiredMethods as $pattern) {
132140
$regexList[] = '/^'.str_replace('\*', '.*', preg_quote($pattern, '/')).'$/i';
133141
}
@@ -147,7 +155,7 @@ private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, ar
147155
}
148156
}
149157

150-
if ($notFound = array_diff($autowiredMethods, $found)) {
158+
if ($notFound = array_diff($configuredAutowiredMethods, $found)) {
151159
$compiler = $this->container->getCompiler();
152160
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatUnusedAutowiringPatterns($this, $id, $notFound));
153161
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public function testSetterInjection()
440440
// manually configure *one* call, to override autowiring
441441
$container
442442
->register('setter_injection', SetterInjection::class)
443-
->setAutowiredMethods(array('__construct', 'set*'))
443+
->setAutowiredMethods(array('set*'))
444444
->addMethodCall('setWithCallsConfigured', array('manual_arg1', 'manual_arg2'))
445445
;
446446

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

562562
$pass = new AutowirePass();
563563
$pass->process($container);

0 commit comments

Comments
 (0)
0