Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f83ce7

Browse files
committed
feature #48045 [DependencyInjection] Allow enum as service parameter in php config files (alexndlm)
This PR was merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Allow enum as service parameter in php config files | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | Commits ------- c37c67c -allow enum as service parameter in php config files
2 parents fa3d2d5 + c37c67c commit 1f83ce7

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __wakeup()
5656
/**
5757
* Checks that a value is valid, optionally replacing Definition and Reference configurators by their configure value.
5858
*
59-
* @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars and arrays are
59+
* @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars, arrays and enum are
6060
*
6161
* @return mixed the value, optionally cast to a Definition/Reference
6262
*/
@@ -98,6 +98,7 @@ public static function processValue(mixed $value, bool $allowServices = false):
9898
switch (true) {
9999
case null === $value:
100100
case \is_scalar($value):
101+
case $value instanceof \UnitEnum:
101102
return $value;
102103

103104
case $value instanceof ArgumentInterface:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
4+
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
7+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
8+
9+
return function (ContainerConfigurator $containerConfigurator) {
10+
$containerConfigurator->parameters()
11+
->set('unit_enum', FooUnitEnum::BAR)
12+
->set('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
13+
14+
$services = $containerConfigurator->services();
15+
16+
$services->defaults()->public();
17+
18+
$services->set('service_container', ContainerInterface::class)
19+
->synthetic();
20+
21+
$services->set(FooClassWithEnumAttribute::class)
22+
->args([FooUnitEnum::BAR]);
23+
};

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

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
2222
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2323
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
24+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
25+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2426

2527
class PhpFileLoaderTest extends TestCase
2628
{
@@ -165,6 +167,19 @@ public function testEnvConfigurator()
165167
$this->assertSame('%env(int:CCC)%', $container->getDefinition('foo')->getArgument(0));
166168
}
167169

170+
public function testEnumeration()
171+
{
172+
$fixtures = realpath(__DIR__.'/../Fixtures');
173+
$container = new ContainerBuilder();
174+
$loader = new PhpFileLoader($container, new FileLocator($fixtures.'/config'));
175+
$loader->load('services_with_enumeration.php');
176+
177+
$container->compile();
178+
179+
$definition = $container->getDefinition(FooClassWithEnumAttribute::class);
180+
$this->assertSame([FooUnitEnum::BAR], $definition->getArguments());
181+
}
182+
168183
public function testNestedBundleConfigNotAllowed()
169184
{
170185
$fixtures = realpath(__DIR__.'/../Fixtures');

0 commit comments

Comments
 (0)