8000 feature #48686 [DependencyInjection] Deprecate integer keys in "servi… · symfony/symfony@ff6913e · GitHub
[go: up one dir, main page]

Skip to content

Commit ff6913e

Browse files
feature #48686 [DependencyInjection] Deprecate integer keys in "service_locator" config (upyx)
This PR was merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Deprecate integer keys in "service_locator" config | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | Deprecation befor #48653 | License | MIT | Doc PR | symfony/symfony-docs#17576 It deprecates undefined/wrong behaviour of https://symfony.com/doc/current/service_container/service_subscribers_locators.html#defining-a-service-locator Commits ------- 57c2365 [DependencyInjection] Deprecate integers keys in "service_locator" config
2 parents 8cc74f1 + 57c2365 commit ff6913e

11 files changed

+178
-1
lines changed

UPGRADE-6.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ DependencyInjection
55
-------------------
66

77
* Deprecate `PhpDumper` options `inline_factories_parameter` and `inline_class_loader_parameter`, use `inline_factories` and `inline_class_loader` instead
8+
* Deprecate undefined and numeric keys with `service_locator` config, use string aliases instead
89

910
HttpKernel
1011
----------

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Deprecate `PhpDumper` options `inline_factories_parameter` and `inline_class_loader_parameter`
99
* Add `RemoveBuildParametersPass`, which removes parameters starting with a dot during compilation
1010
* Add support for nesting autowiring-related attributes into `#[Autowire(...)]`
11+
* Deprecate undefined and numeric keys with `service_locator` config
1112

1213
6.2
1314
---

src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ function inline_service(string $class = null): InlineServiceConfigurator
123123
*/
124124
function service_locator(array $values): ServiceLocatorArgument
125125
{
126-
return new ServiceLocatorArgument(AbstractConfigurator::processValue($values, true));
126+
$values = AbstractConfigurator::processValue($values, true);
127+
128+
if (isset($values[0])) {
129+
trigger_deprecation('symfony/dependency-injection', '6.3', 'Using integers as keys in a "service_locator()" argument is deprecated. The keys will default to the IDs of the original services in 7.0.');
130+
}
131+
132+
return new ServiceLocatorArgument($values);
127133
}
128134

129135
/**

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
526526
break;
527527
case 'service_locator':
528528
$arg = $this->getArgumentsAsPhp($arg, $name, $file);
529+
530+
if (isset($arg[0])) {
531+
trigger_deprecation('symfony/dependency-injection', '6.3', 'Skipping "key" argument or using integers as values in a "service_locator" tag is deprecated. The keys will default to the IDs of the original services in 7.0.');
532+
}
533+
529534
$arguments[$key] = new ServiceLocatorArgument($arg);
530535
break;
531536
case 'tagged':

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,10 @@ private function resolveServices(mixed $value, string $file, bool $isParameter =
814814

815815
$argument = $this->resolveServices($argument, $file, $isParameter);
816816

817+
if (isset($argument[0])) {
818+
trigger_deprecation('symfony/dependency-injection', '6.3', 'Using integers as keys in a "!service_locator" tag is deprecated. The keys will default to the IDs of the original services in 7.0.');
819+
}
820+
817821
return new ServiceLocatorArgument($argument);
818822
}
819823
if (\in_array($value->getTag(), ['tagged', 'tagged_iterator', 'tagged_locator'], true)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
4+
5+
return function (ContainerConfigurator $configurator) {
6+
$services = $configurator->services()->defaults()->public();
7+
8+
$services->set('foo_service', \stdClass::class);
9+
10+
$services->set('bar_service', \stdClass::class);
11+
12+
$services->set('locator_dependent_service_indexed', \ArrayObject::class)
13+
->args([service_locator([
14+
'foo' => service('foo_service'),
15+
'bar' => service('bar_service'),
16+
])]);
17+
18+
$services->set('locator_dependent_service_not_indexed', \ArrayObject::class)
19+
->args([service_locator([
20+
service('foo_service'),
21+
service('bar_service'),
22+
])]);
23+
24+
$services->set('locator_dependent_service_mixed', \ArrayObject::class)
25+
->args([service_locator([
26+
'foo' => service('foo_service'),
27+
service('bar_service'),
28+
])]);
29+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="foo_service" class="stdClass"/>
5+
6+
<service id="bar_service" class="stdClass"/>
7+
8+
<service id="locator_dependent_service_indexed" class="ArrayObject">
9+
<argument type="service_locator">
10+
<argument key="foo" type="service" id="foo_service"/>
11+
<argument key="bar" type="service" id="bar_service"/>
12+
</argument>
13+
</service>
14+
15+
<service id="locator_dependent_service_not_indexed" class="ArrayObject">
16+
<argument type="service_locator">
17+
<argument type="service" id="foo_service"/>
18+
<argument type="service" id="bar_service"/>
19+
</argument>
20+
</service>
21+
22+
<service id="locator_dependent_service_mixed" class="ArrayObject">
23+
<argument type="service_locator">
24+
<argument key="foo" type="service" id="foo_service"/>
25+
<argument type="service" id="bar_service"/>
26+
</argument>
27+
</service>
28+
</services>
29+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
services:
3+
foo_service:
4+
class: stdClass
5+
6+
bar_service:
7+
class: stdClass
8+
9+
locator_dependent_service_indexed:
10+
class: ArrayObject
11+
arguments:
12+
- !service_locator
13+
'foo': '@foo_service'
14+
'bar': '@bar_service'
15+
16+
locator_dependent_service_not_indexed:
17+
class: ArrayObject
18+
arguments:
19+
- !service_locator
20+
- '@foo_service'
21+
- '@bar_service'
22+
23+
locator_dependent_service_mixed:
24+
class: ArrayObject
25+
arguments:
26+
- !service_locator
27+
'foo': '@foo_service'
28+
'0': '@bar_service'

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@
1414
require_once __DIR__.'/../Fixtures/includes/AcmeExtension.php';
1515

1616
use PHPUnit\Framework\TestCase;
17+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1718
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
1819
use Symfony\Component\Config\FileLocator;
20+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1921
use Symfony\Component\DependencyInjection\ContainerBuilder;
2022
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
2123
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
2224
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2325
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
26+
use Symfony\Component\DependencyInjection\Reference;
2427
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
2528
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2629

2730
class PhpFileLoaderTest extends TestCase
2831
{
32+
use ExpectDeprecationTrait;
33+
2934
public function testSupports()
3035
{
3136
$loader = new PhpFileLoader(new ContainerBuilder(), new FileLocator());
@@ -200,4 +205,25 @@ public function testWhenEnv()
200205

201206
$loader->load($fixtures.'/config/when_env.php');
202207
}
208+
209+
/**
210+
* @group legacy
211+
*/
212+
public function testServiceWithServiceLocatorArgument()
213+
{
214+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: Using integers as keys in a "service_locator()" argument is deprecated. The keys will default to the IDs of the original services in 7.0.');
215+
216+
$fixtures = realpath(__DIR__.'/../Fixtures');
217+
$loader = new PhpFileLoader($container = new ContainerBuilder(), new FileLocator());
218+
$loader->load($fixtures.'/config/services_with_service_locator_argument.php');
219+
220+
$values = ['foo' => new Reference('foo_service'), 'bar' => new Reference('bar_service')];
221+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_indexed')->getArguments());
222+
223+
$values = [new Reference('foo_service'), new Reference('bar_service')];
224+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_not_indexed')->getArguments());
225+
226+
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
227+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
228+
}
203229
}

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
1617
use Symfony\Component\Config\Exception\LoaderLoadException;
1718
use Symfony\Component\Config\FileLocator;
@@ -47,6 +48,8 @@
4748

4849
class XmlFileLoaderTest extends TestCase
4950
{
51+
use ExpectDeprecationTrait;
52+
5053
protected static $fixturesPath;
5154

5255
public static function setUpBeforeClass(): void
@@ -421,6 +424,27 @@ public function testParseTaggedArgumentsWithIndexBy()
421424
$this->assertEquals(new ServiceLocatorArgument($taggedIterator3), $container->getDefinition('foo3_tagged_locator')->getArgument(0));
422425
}
423426

427+
/**
428+
* @group legacy
429+
*/
430+
public function testServiceWithServiceLocatorArgument()
431+
{
432+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: Skipping "key" argument or using integers as values in a "service_locator" tag is deprecated. The keys will default to the IDs of the original services in 7.0.');
433+
434+
$container = new ContainerBuilder();
435+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
436+
$loader->load('services_with_service_locator_argument.xml');
437+
438+
$values = ['foo' => new Reference('foo_service'), 'bar' => new Reference('bar_service')];
439+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_indexed')->getArguments());
440+
441+
$values = [new Reference('foo_service'), new Reference('bar_service')];
442+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_not_indexed')->getArguments());
443+
444+
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
445+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
446+
}
447+
424448
public function testParseServiceClosure()
425449
{
426450
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
1617
use Symfony\Component\Config\Exception\LoaderLoadException;
1718
use Symfony\Component\Config\FileLocator;
@@ -45,6 +46,8 @@
4546

4647
class YamlFileLoaderTest extends TestCase
4748
{
49+
use ExpectDeprecationTrait;
50+
4851
protected static $fixturesPath;
4952

5053
public static function setUpBeforeClass(): void
@@ -408,6 +411,27 @@ public function testTaggedArgumentsWithIndex()
408411
$this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('bar_service_tagged_locator')->getArgument(0));
409412
}
410413

414+
/**
415+
* @group legacy
416+
*/
417+
public function testServiceWithServiceLocatorArgument()
418+
{
419+
$this->expectDeprecation('Since symfony/dependency-injection 6.3: Using integers as keys in a "!service_locator" tag is deprecated. The keys will default to the IDs of the original services in 7.0.');
420+
421+
$container = new ContainerBuilder();
422+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
423+
$loader->load('services_with_service_locator_argument.yml');
424+
425+
$values = ['foo' => new Reference('foo_service'), 'bar' => new Reference('bar_service')];
426+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_indexed')->getArguments());
427+
428+
$values = [new Reference('foo_service'), new Reference('bar_service')];
429+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_not_indexed')->getArguments());
430+
431+
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
432+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
433+
}
434+
411435
public function testParseServiceClosure()
412436
{
413437
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)
0