Skip to content

[DI] deprecate TypedReference::canBeAutoregistered() and getRequiringClass() #26636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------

Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down Expand Up @@ -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),
Expand All @@ -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()
);
Expand Down Expand Up @@ -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]
);
}
Expand Down Expand Up @@ -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]
);
}
Expand Down Expand Up @@ -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()
Expand All @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down
22 changes: 18 additions & 4 deletions src/Symfony/Component/DependencyInjection/TypedReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,42 @@ 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()
{
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down