[DependencyInjection] Always autowire the constructor
This commit is contained in:
parent
839920828e
commit
be3d11faa9
@ -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));
|
||||
}
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user