Skip to content

Commit 6dcfbb9

Browse files
committed
Allow passing an inline_service to a service_locator
Something, you want to include an inline service to a service locator. This works fine. Except that the PHPDoc doesn't allow it causing PHPStan to fail.
1 parent 2bbbe70 commit 6dcfbb9

8 files changed

+43
-1
lines changed

Loader/Configurator/ContainerConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function inline_service(string $class = null): InlineServiceConfigurator
119119
/**
120120
* Creates a service locator.
121121
*
122-
* @param ReferenceConfigurator[] $values
122+
* @param array<ReferenceConfigurator|InlineServiceConfigurator> $values
123123
*/
124124
function service_locator(array $values): ServiceLocatorArgument
125125
{

Tests/Compiler/ServiceLocatorTagPassTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function testProcessValue()
7070
new Reference('bar'),
7171
new Reference('baz'),
7272
'some.service' => new Reference('bar'),
73+
'inlines.service' => new Definition(CustomDefinition::class),
7374
]])
7475
->addTag('container.service_locator')
7576
;
@@ -82,6 +83,7 @@ public function testProcessValue()
8283
$this->assertSame(CustomDefinition::class, $locator('bar')::class);
8384
$this->assertSame(CustomDefinition::class, $locator('baz')::class);
8485
$this->assertSame(CustomDefinition::class, $locator('some.service')::class);
86+
$this->assertSame(CustomDefinition::class, \get_class($locator('inlines.service')));
8587
}
8688

8789
public function testServiceWithKeyOverwritesPreviousInheritedKey()

Tests/Fixtures/config/services_with_service_locator_argument.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@
2626
'foo' => service('foo_service'),
2727
service('bar_service'),
2828
])]);
29+
30+
$services->set('locator_dependent_inline_service', \ArrayObject::class)
31+
->args([service_locator([
32+
'foo' => inline_service(\stdClass::class),
33+
'bar' => inline_service(\stdClass::class),
34+
])]);
2935
};

Tests/Fixtures/xml/services_with_service_locator_argument.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,16 @@
2525
<argument type="service" id="bar_service"/>
2626
</argument>
2727
</service>
28+
29+
<service id="locator_dependent_inline_service" class="ArrayObject">
30+
<argument type="service_locator">
31+
<argument key="foo" type="service">
32+
<service class="stdClass"/>
33+
</argument>
34+
<argument key="bar" type="service">
35+
<service class="stdClass"/>
36+
</argument>
37+
</argument>
38+
</service>
2839
</services>
2940
</container>

Tests/Fixtures/yaml/services_with_service_locator_argument.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ services:
2626
- !service_locator
2727
'foo': '@foo_service'
2828
'0': '@bar_service'
29+
30+
locator_dependent_inline_service:
31+
class: ArrayObject
32+
arguments:
33+
- !service_locator
34+
'foo':
35+
- !service
36+
class: stdClass
37+
'bar':
38+
- !service
39+
class: stdClass

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Config\FileLocator;
2020
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2121
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use Symfony\Component\DependencyInjection\Definition;
2223
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
2324
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
2425
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -231,5 +232,8 @@ public function testServiceWithServiceLocatorArgument()
231232

232233
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
233234
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
235+
236+
$values = ['foo' => new Definition(\stdClass::class), 'bar' => new Definition(\stdClass::class)];
237+
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_inline_service')->getArguments());
234238
}
235239
}

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ public function testServiceWithServiceLocatorArgument()
447447

448448
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
449449
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
450+
451+
$inlinedServiceArguments = $container->getDefinition('locator_dependent_inline_service')->getArguments();
452+
$this->assertEquals((new Definition(\stdClass::class))->setPublic(false), $container->getDefinition((string) $inlinedServiceArguments[0]->getValues()['foo']));
453+
$this->assertEquals((new Definition(\stdClass::class))->setPublic(false), $container->getDefinition((string) $inlinedServiceArguments[0]->getValues()['bar']));
450454
}
451455

452456
public function testParseServiceClosure()

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ public function testServiceWithServiceLocatorArgument()
441441

442442
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
443443
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
444+
445+
$inlinedServiceArguments = $container->getDefinition('locator_dependent_inline_service')->getArguments();
446+
$this->assertEquals(new Definition(\stdClass::class), $container->getDefinition((string) $inlinedServiceArguments[0]->getValues()['foo'][0]));
447+
$this->assertEquals(new Definition(\stdClass::class), $container->getDefinition((string) $inlinedServiceArguments[0]->getValues()['bar'][0]));
444448
}
445449

446450
public function testParseServiceClosure()

0 commit comments

Comments
 (0)