From bd865f7f68701dba091ecada3ec2cea51139aa6e Mon Sep 17 00:00:00 2001 From: "jack.shpartko" Date: Tue, 20 Sep 2022 23:14:33 +0200 Subject: [PATCH] Bugfix: add \UnitEnum as a result of get() method --- .../ParameterBag/EnvPlaceholderParameterBag.php | 2 +- .../Tests/Fixtures/StringBackedEnum.php | 17 +++++++++++++++++ .../EnvPlaceholderParameterBagTest.php | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/StringBackedEnum.php diff --git a/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php b/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php index 0b6f082aaeb94..7466506d5ea65 100644 --- a/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php +++ b/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php @@ -29,7 +29,7 @@ class EnvPlaceholderParameterBag extends ParameterBag /** * {@inheritdoc} */ - public function get(string $name): array|bool|string|int|float|null + public function get(string $name): array|bool|string|int|float|\UnitEnum|null { if (str_starts_with($name, 'env(') && str_ends_with($name, ')') && 'env()' !== $name) { $env = substr($name, 4, -1); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/StringBackedEnum.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/StringBackedEnum.php new file mode 100644 index 0000000000000..b118cc7550eb8 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/StringBackedEnum.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures; + +enum StringBackedEnum: string +{ + case Bar = 'bar'; +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php b/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php index 9134f1f6c0186..e529dd9e566ed 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php @@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; +use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum; class EnvPlaceholderParameterBagTest extends TestCase { @@ -196,4 +197,12 @@ public function testExtraCharsInProcessor() $bag->resolve(); $this->assertStringMatchesFormat('env_%s_key_a_b_c_FOO_%s', $bag->get('env(key:a.b-c:FOO)')); } + + public function testGetEnum() + { + $bag = new EnvPlaceholderParameterBag(); + $bag->set('ENUM_VAR', StringBackedEnum::Bar); + $this->assertInstanceOf(StringBackedEnum::class, $bag->get('ENUM_VAR')); + $this->assertEquals(StringBackedEnum::Bar, $bag->get('ENUM_VAR')); + } }