From 95fd81b5d83da09695d66df850c547fda09a7afb Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 22 Mar 2018 12:53:13 +0100 Subject: [PATCH] [DI] deprecate TypedReference::canBeAutoregistered() and getRequiringClass() --- UPGRADE-4.1.md | 5 +++++ UPGRADE-5.0.md | 5 +++++ .../DependencyInjection/CHANGELOG.md | 1 + .../Compiler/AutowirePass.php | 2 +- .../RegisterServiceSubscribersPass.php | 3 +-- .../Tests/Compiler/AutowirePassTest.php | 18 +++++++-------- .../RegisterServiceSubscribersPassTest.php | 16 +++++++------- .../Fixtures/php/services_subscriber.php | 4 ++-- .../DependencyInjection/TypedReference.php | 22 +++++++++++++++---- ...RegisterControllerArgumentLocatorsPass.php | 2 +- ...sterControllerArgumentLocatorsPassTest.php | 6 ++--- .../Component/HttpKernel/composer.json | 4 ++-- 12 files changed, 56 insertions(+), 32 deletions(-) diff --git a/UPGRADE-4.1.md b/UPGRADE-4.1.md index 0a58560af919b..b0c41b891b85c 100644 --- a/UPGRADE-4.1.md +++ b/UPGRADE-4.1.md @@ -12,6 +12,11 @@ Console * Deprecated the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`. +DependencyInjection +------------------- + + * Deprecated the `TypedReference::canBeAutoregistered()` and `TypedReference::getRequiringClass()` methods. + EventDispatcher --------------- diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index 096fbd7583bb9..e994066f1e128 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -11,6 +11,11 @@ Console * Removed the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`. +DependencyInjection +------------------- + + * Removed the `TypedReference::canBeAutoregistered()` and `TypedReference::getRequiringClass()` methods. + EventDispatcher --------------- diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index d0f844df4fdc3..e3671ab9b53f3 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * added support for variadics in named arguments * added PSR-11 `ContainerBagInterface` and its `ContainerBag` implementation to access parameters as-a-service * added support for service's decorators autowiring + * deprecated the `TypedReference::canBeAutoregistered()` and `TypedReference::getRequiringClass()` methods 4.0.0 ----- diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index 1d2e37759f024..9d44bde35bd8c 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -208,7 +208,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a } $getValue = function () use ($type, $parameter, $class, $method) { - if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, !$parameter->isOptional() ? $class : ''))) { + if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type))) { $failureMessage = $this->createTypeNotFoundMessage($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method)); if ($parameter->isDefaultValueAvailable()) { diff --git a/src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php b/src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php index 572eabb7d99e7..87b4eac16ca22 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php @@ -65,7 +65,6 @@ protected function processValue($value, $isRoot = false) $class = $r->name; $subscriberMap = array(); - $declaringClass = (new \ReflectionMethod($class, 'getSubscribedServices'))->class; foreach ($class::getSubscribedServices() as $key => $type) { if (!is_string($type) || !preg_match('/^\??[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $type)) { @@ -85,7 +84,7 @@ protected function processValue($value, $isRoot = false) $serviceMap[$key] = new Reference($type); } - $subscriberMap[$key] = new TypedReference((string) $serviceMap[$key], $type, $declaringClass, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE); + $subscriberMap[$key] = new TypedReference((string) $serviceMap[$key], $type, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE); unset($serviceMap[$key]); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 10088a2aa935a..a78ea122c2c77 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -383,9 +383,9 @@ public function testSomeSpecificArgumentsAreSet() $definition = $container->getDefinition('multiple'); $this->assertEquals( array( - new TypedReference(A::class, A::class, MultipleArguments::class), + new TypedReference(A::class, A::class), new Reference('foo'), - new TypedReference(Dunglas::class, Dunglas::class, MultipleArguments::class), + new TypedReference(Dunglas::class, Dunglas::class), ), $definition->getArguments() ); @@ -438,7 +438,7 @@ public function testOptionalScalarArgsDontMessUpOrder() $definition = $container->getDefinition('with_optional_scalar'); $this->assertEquals( array( - new TypedReference(A::class, A::class, MultipleArgumentsOptionalScalar::class), + new TypedReference(A::class, A::class), // use the default value 'default_val', new TypedReference(Lille::class, Lille::class), @@ -462,8 +462,8 @@ public function testOptionalScalarArgsNotPassedIfLast() $definition = $container->getDefinition('with_optional_scalar_last'); $this->assertEquals( array( - new TypedReference(A::class, A::class, MultipleArgumentsOptionalScalarLast::class), - new TypedReference(Lille::class, Lille::class, MultipleArgumentsOptionalScalarLast::class), + new TypedReference(A::class, A::class), + new TypedReference(Lille::class, Lille::class), ), $definition->getArguments() ); @@ -519,7 +519,7 @@ public function testSetterInjection() ); // test setFoo args $this->assertEquals( - array(new TypedReference(Foo::class, Foo::class, SetterInjection::class)), + array(new TypedReference(Foo::class, Foo::class)), $methodCalls[1][1] ); } @@ -549,7 +549,7 @@ public function testExplicitMethodInjection() array_column($methodCalls, 0) ); $this->assertEquals( - array(new TypedReference(A::class, A::class, SetterInjection::class)), + array(new TypedReference(A::class, A::class)), $methodCalls[0][1] ); } @@ -647,7 +647,7 @@ public function testEmptyStringIsKept() (new ResolveClassPass())->process($container); (new AutowirePass())->process($container); - $this->assertEquals(array(new TypedReference(A::class, A::class, MultipleArgumentsOptionalScalar::class), '', new TypedReference(Lille::class, Lille::class)), $container->getDefinition('foo')->getArguments()); + $this->assertEquals(array(new TypedReference(A::class, A::class), '', new TypedReference(Lille::class, Lille::class)), $container->getDefinition('foo')->getArguments()); } public function testWithFactory() @@ -662,7 +662,7 @@ public function testWithFactory() (new ResolveClassPass())->process($container); (new AutowirePass())->process($container); - $this->assertEquals(array(new TypedReference(Foo::class, Foo::class, A::class)), $definition->getArguments()); + $this->assertEquals(array(new TypedReference(Foo::class, Foo::class)), $definition->getArguments()); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php index 42f994e821d97..f7314948aeef6 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php @@ -79,10 +79,10 @@ public function testNoAttributes() $this->assertSame(ServiceLocator::class, $locator->getClass()); $expected = array( - TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class, TestServiceSubscriber::class)), - CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), - 'bar' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class)), - 'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), + TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class)), + CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), + 'bar' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class)), + 'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), ); $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); @@ -109,10 +109,10 @@ public function testWithAttributes() $this->assertSame(ServiceLocator::class, $locator->getClass()); $expected = array( - TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class, TestServiceSubscriber::class)), - CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), - 'bar' => new ServiceClosureArgument(new TypedReference('bar', CustomDefinition::class, TestServiceSubscriber::class)), - 'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), + TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class)), + CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), + 'bar' => new ServiceClosureArgument(new TypedReference('bar', CustomDefinition::class)), + 'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), ); $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php index 11b935cc616ff..7ed23ebf7d5e3 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php @@ -57,8 +57,8 @@ public function getRemovedIds() 'Psr\\Container\\ContainerInterface' => true, 'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true, 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true, - 'service_locator.KT3jhJ7' => true, - 'service_locator.KT3jhJ7.foo_service' => true, + 'service_locator.ljJrY4L' => true, + 'service_locator.ljJrY4L.foo_service' => true, ); } diff --git a/src/Symfony/Component/DependencyInjection/TypedReference.php b/src/Symfony/Component/DependencyInjection/TypedReference.php index dc869ec9b101e..9d488ddbb478c 100644 --- a/src/Symfony/Component/DependencyInjection/TypedReference.php +++ b/src/Symfony/Component/DependencyInjection/TypedReference.php @@ -24,14 +24,18 @@ class TypedReference extends Reference /** * @param string $id The service identifier * @param string $type The PHP type of the identified service - * @param string $requiringClass The class of the service that requires the referenced type * @param int $invalidBehavior The behavior when the service does not exist */ - public function __construct(string $id, string $type, string $requiringClass = '', int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) + public function __construct(string $id, string $type, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { + if (\is_string($invalidBehavior) || 3 < \func_num_args()) { + @trigger_error(sprintf('The $requiringClass argument of "%s" is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED); + + $this->requiringClass = $invalidBehavior; + $invalidBehavior = 3 < \func_num_args() ? \func_get_arg(3) : ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; + } parent::__construct($id, $invalidBehavior); $this->type = $type; - $this->requiringClass = $requiringClass; } public function getType() @@ -39,13 +43,23 @@ public function getType() return $this->type; } + /** + * @deprecated since Symfony 4.1 + */ public function getRequiringClass() { - return $this->requiringClass; + @trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED); + + return $this->requiringClass ?? ''; } + /** + * @deprecated since Symfony 4.1 + */ public function canBeAutoregistered() { + @trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED); + return $this->requiringClass && (false !== $i = strpos($this->type, '\\')) && 0 === strncasecmp($this->type, $this->requiringClass, 1 + $i); } } diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php index afa480b31e530..e5cc3facf6759 100644 --- a/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php @@ -164,7 +164,7 @@ public function process(ContainerBuilder $container) throw new InvalidArgumentException($message); } - $args[$p->name] = $type ? new TypedReference($target, $type, $r->class, $invalidBehavior) : new Reference($target, $invalidBehavior); + $args[$p->name] = $type ? new TypedReference($target, $type, $invalidBehavior) : new Reference($target, $invalidBehavior); } // register the maps as a per-method service-locators if ($args) { diff --git a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php index 18bd03ddf872b..383a86a61f0da 100644 --- a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php @@ -148,7 +148,7 @@ public function testAllActions() $this->assertSame(ServiceLocator::class, $locator->getClass()); $this->assertFalse($locator->isPublic()); - $expected = array('bar' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, RegisterTestController::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); + $expected = array('bar' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); $this->assertEquals($expected, $locator->getArgument(0)); } @@ -168,7 +168,7 @@ public function testExplicitArgument() $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0); $locator = $container->getDefinition((string) $locator['foo::fooAction']->getValues()[0]); - $expected = array('bar' => new ServiceClosureArgument(new TypedReference('bar', ControllerDummy::class, RegisterTestController::class))); + $expected = array('bar' => new ServiceClosureArgument(new TypedReference('bar', ControllerDummy::class))); $this->assertEquals($expected, $locator->getArgument(0)); } @@ -187,7 +187,7 @@ public function testOptionalArgument() $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0); $locator = $container->getDefinition((string) $locator['foo::fooAction']->getValues()[0]); - $expected = array('bar' => new ServiceClosureArgument(new TypedReference('bar', ControllerDummy::class, RegisterTestController::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); + $expected = array('bar' => new ServiceClosureArgument(new TypedReference('bar', ControllerDummy::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE))); $this->assertEquals($expected, $locator->getArgument(0)); } diff --git a/src/Symfony/Component/HttpKernel/composer.json b/src/Symfony/Component/HttpKernel/composer.json index da049d8813b58..22eedffe8b59f 100644 --- a/src/Symfony/Component/HttpKernel/composer.json +++ b/src/Symfony/Component/HttpKernel/composer.json @@ -27,7 +27,7 @@ "symfony/config": "~3.4|~4.0", "symfony/console": "~3.4|~4.0", "symfony/css-selector": "~3.4|~4.0", - "symfony/dependency-injection": "^3.4.5|^4.0.5", + "symfony/dependency-injection": "^4.1", "symfony/dom-crawler": "~3.4|~4.0", "symfony/expression-language": "~3.4|~4.0", "symfony/finder": "~3.4|~4.0", @@ -44,7 +44,7 @@ }, "conflict": { "symfony/config": "<3.4", - "symfony/dependency-injection": "<3.4.5|<4.0.5,>=4", + "symfony/dependency-injection": "<4.1", "symfony/var-dumper": "<3.4", "twig/twig": "<1.34|<2.4,>=2" },