Skip to content

Commit 95fd81b

Browse files
[DI] deprecate TypedReference::canBeAutoregistered() and getRequiringClass()
1 parent 9131bd1 commit 95fd81b

File tree

12 files changed

+56
-32
lines changed

12 files changed

+56
-32
lines changed

UPGRADE-4.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Console
1212

1313
* Deprecated the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`.
1414

15+
DependencyInjection
16+
-------------------
17+
18+
* Deprecated the `TypedReference::canBeAutoregistered()` and `TypedReference::getRequiringClass()` methods.
19+
1520
EventDispatcher
1621
---------------
1722

UPGRADE-5.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Console
1111

1212
* Removed the `setCrossingChar()` method in favor of the `setDefaultCrossingChar()` method in `TableStyle`.
1313

14+
DependencyInjection
15+
-------------------
16+
17+
* Removed the `TypedReference::canBeAutoregistered()` and `TypedReference::getRequiringClass()` methods.
18+
1419
EventDispatcher
1520
---------------
1621

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added support for variadics in named arguments
88
* added PSR-11 `ContainerBagInterface` and its `ContainerBag` implementation to access parameters as-a-service
99
* added support for service's decorators autowiring
10+
* deprecated the `TypedReference::canBeAutoregistered()` and `TypedReference::getRequiringClass()` methods
1011

1112
4.0.0
1213
-----

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
208208
}
209209

210210
$getValue = function () use ($type, $parameter, $class, $method) {
211-
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, !$parameter->isOptional() ? $class : ''))) {
211+
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type))) {
212212
$failureMessage = $this->createTypeNotFoundMessage($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
213213

214214
if ($parameter->isDefaultValueAvailable()) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ protected function processValue($value, $isRoot = false)
6565
$class = $r->name;
6666

6767
$subscriberMap = array();
68-
$declaringClass = (new \ReflectionMethod($class, 'getSubscribedServices'))->class;
6968

7069
foreach ($class::getSubscribedServices() as $key => $type) {
7170
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)
8584
$serviceMap[$key] = new Reference($type);
8685
}
8786

88-
$subscriberMap[$key] = new TypedReference((string) $serviceMap[$key], $type, $declaringClass, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
87+
$subscriberMap[$key] = new TypedReference((string) $serviceMap[$key], $type, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
8988
unset($serviceMap[$key]);
9089
}
9190

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ public function testSomeSpecificArgumentsAreSet()
383383
$definition = $container->getDefinition('multiple');
384384
$this->assertEquals(
385385
array(
386-
new TypedReference(A::class, A::class, MultipleArguments::class),
386+
new TypedReference(A::class, A::class),
387387
new Reference('foo'),
388-
new TypedReference(Dunglas::class, Dunglas::class, MultipleArguments::class),
388+
new TypedReference(Dunglas::class, Dunglas::class),
389389
),
390390
$definition->getArguments()
391391
);
@@ -438,7 +438,7 @@ public function testOptionalScalarArgsDontMessUpOrder()
438438
$definition = $container->getDefinition('with_optional_scalar');
439439
$this->assertEquals(
440440
array(
441-
new TypedReference(A::class, A::class, MultipleArgumentsOptionalScalar::class),
441+
new TypedReference(A::class, A::class),
442442
// use the default value
443443
'default_val',
444444
new TypedReference(Lille::class, Lille::class),
@@ -462,8 +462,8 @@ public function testOptionalScalarArgsNotPassedIfLast()
462462
$definition = $container->getDefinition('with_optional_scalar_last');
463463
$this->assertEquals(
464464
array(
465-
new TypedReference(A::class, A::class, MultipleArgumentsOptionalScalarLast::class),
466-
new TypedReference(Lille::class, Lille::class, MultipleArgumentsOptionalScalarLast::class),
465+
new TypedReference(A::class, A::class),
466+
new TypedReference(Lille::class, Lille::class),
467467
),
468468
$definition->getArguments()
469469
);
@@ -519,7 +519,7 @@ public function testSetterInjection()
519519
);
520520
// test setFoo args
521521
$this->assertEquals(
522-
array(new TypedReference(Foo::class, Foo::class, SetterInjection::class)),
522+
array(new TypedReference(Foo::class, Foo::class)),
523523
$methodCalls[1][1]
524524
);
525525
}
@@ -549,7 +549,7 @@ public function testExplicitMethodInjection()
549549
array_column($methodCalls, 0)
550550
);
551551
$this->assertEquals(
552-
array(new TypedReference(A::class, A::class, SetterInjection::class)),
552+
array(new TypedReference(A::class, A::class)),
553553
$methodCalls[0][1]
554554
);
555555
}
@@ -647,7 +647,7 @@ public function testEmptyStringIsKept()
647647
(new ResolveClassPass())->process($container);
648648
(new AutowirePass())->process($container);
649649

650-
$this->assertEquals(array(new TypedReference(A::class, A::class, MultipleArgumentsOptionalScalar::class), '', new TypedReference(Lille::class, Lille::class)), $container->getDefinition('foo')->getArguments());
650+
$this->assertEquals(array(new TypedReference(A::class, A::class), '', new TypedReference(Lille::class, Lille::class)), $container->getDefinition('foo')->getArguments());
651651
}
652652

653653
public function testWithFactory()
@@ -662,7 +662,7 @@ public function testWithFactory()
662662
(new ResolveClassPass())->process($container);
663663
(new AutowirePass())->process($container);
664664

665-
$this->assertEquals(array(new TypedReference(Foo::class, Foo::class, A::class)), $definition->getArguments());
665+
$this->assertEquals(array(new TypedReference(Foo::class, Foo::class)), $definition->getArguments());
666666
}
667667

668668
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public function testNoAttributes()
7979
$this->assertSame(ServiceLocator::class, $locator->getClass());
8080

8181
$expected = array(
82-
TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class, TestServiceSubscriber::class)),
83-
CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
84-
'bar' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class)),
85-
'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
82+
TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class)),
83+
CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
84+
'bar' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class)),
85+
'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
8686
);
8787

8888
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
@@ -109,10 +109,10 @@ public function testWithAttributes()
109109
$this->assertSame(ServiceLocator::class, $locator->getClass());
110110

111111
$expected = array(
112-
TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class, TestServiceSubscriber::class)),
113-
CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
114-
'bar' => new ServiceClosureArgument(new TypedReference('bar', CustomDefinition::class, TestServiceSubscriber::class)),
115-
'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
112+
TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class)),
113+
CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
114+
'bar' => new ServiceClosureArgument(new TypedReference('bar', CustomDefinition::class)),
115+
'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
116116
);
117117

118118
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function getRemovedIds()
5757
'Psr\\Container\\ContainerInterface' => true,
5858
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
5959
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true,
60-
'service_locator.KT3jhJ7' => true,
61-
'service_locator.KT3jhJ7.foo_service' => true,
60+
'service_locator.ljJrY4L' => true,
61+
'service_locator.ljJrY4L.foo_service' => true,
6262
);
6363
}
6464

src/Symfony/Component/DependencyInjection/TypedReference.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,42 @@ class TypedReference extends Reference
2424
/**
2525
* @param string $id The service identifier
2626
* @param string $type The PHP type of the identified service
27-
* @param string $requiringClass The class of the service that requires the referenced type
2827
* @param int $invalidBehavior The behavior when the service does not exist
2928
*/
30-
public function __construct(string $id, string $type, string $requiringClass = '', int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
29+
public function __construct(string $id, string $type, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
3130
{
31+
if (\is_string($invalidBehavior) || 3 < \func_num_args()) {
32+
@trigger_error(sprintf('The $requiringClass argument of "%s" is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED);
33+
34+
$this->requiringClass = $invalidBehavior;
35+
$invalidBehavior = 3 < \func_num_args() ? \func_get_arg(3) : ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
36+
}
3237
parent::__construct($id, $invalidBehavior);
3338
$this->type = $type;
34-
$this->requiringClass = $requiringClass;
3539
}
3640

3741
public function getType()
3842
{
3943
return $this->type;
4044
}
4145

46+
/**
47+
* @deprecated since Symfony 4.1
48+
*/
4249
public function getRequiringClass()
4350
{
44-
return $this->requiringClass;
51+
@trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED);
52+
53+
return $this->requiringClass ?? '';
4554
}
4655

56+
/**
57+
* @deprecated since Symfony 4.1
58+
*/
4759
public function canBeAutoregistered()
4860
{
61+
@trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED);
62+
4963
return $this->requiringClass && (false !== $i = strpos($this->type, '\\')) && 0 === strncasecmp($this->type, $this->requiringClass, 1 + $i);
5064
}
5165
}

src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function process(ContainerBuilder $container)
164164
throw new InvalidArgumentException($message);
165165
}
166166

167-
$args[$p->name] = $type ? new TypedReference($target, $type, $r->class, $invalidBehavior) : new Reference($target, $invalidBehavior);
167+
$args[$p->name] = $type ? new TypedReference($target, $type, $invalidBehavior) : new Reference($target, $invalidBehavior);
168168
}
169169
// register the maps as a per-method service-locators
170170
if ($args) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy