Skip to content

Commit 782d894

Browse files
committed
[DependencyInjection] Allow using enums in container configurator
1 parent 6166fc4 commit 782d894

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static function processValue($value, $allowServices = false)
101101
switch (true) {
102102
case null === $value:
103103
case \is_scalar($value):
104+
case $value instanceof \UnitEnum:
104105
return $value;
105106

106107
case $value instanceof ArgumentInterface:
Lines changed: 23 additions & 0 deletions
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 $container) {
10+
$container->parameters()
11+
->set('unit_enum', FooUnitEnum::BAR)
12+
->set('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);
13+
14+
$services = $container->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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
2323
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2424
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
25+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
26+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2527

2628
class PhpFileLoaderTest extends TestCase
2729
{
@@ -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
/**
169184
* @group legacy
170185
*/

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