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

Skip to content

[DependencyInjection] Allow enum as service parameter in php config files #48045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __wakeup()
/**
* Checks that a value is valid, optionally replacing Definition and Reference configurators by their configure value.
*
* @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars and arrays are
* @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars, arrays and enum are
*
* @return mixed the value, optionally cast to a Definition/Reference
*/
Expand Down Expand Up @@ -98,6 +98,7 @@ public static function processValue(mixed $value, bool $allowServices = false):
switch (true) {
case null === $value:
case \is_scalar($value):
case $value instanceof \UnitEnum:
return $value;

case $value instanceof ArgumentInterface:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;

return function (ContainerConfigurator $containerConfigurator) {
$containerConfigurator->parameters()
->set('unit_enum', FooUnitEnum::BAR)
->set('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]);

$services = $containerConfigurator->services();

$services->defaults()->public();

$services->set('service_container', ContainerInterface::class)
->synthetic();

$services->set(FooClassWithEnumAttribute::class)
->args([FooUnitEnum::BAR]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;

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

public function testEnumeration()
{
$fixtures = realpath(__DIR__.'/../Fixtures');
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator($fixtures.'/config'));
$loader->load('services_with_enumeration.php');

$container->compile();

$definition = $container->getDefinition(FooClassWithEnumAttribute::class);
$this->assertSame([FooUnitEnum::BAR], $definition->getArguments());
}

public function testNestedBundleConfigNotAllowed()
{
$fixtures = realpath(__DIR__.'/../Fixtures');
Expand Down