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

Skip to content

Commit a921ed4

Browse files
committed
[DependencyInjection] Add shuffle env processor
1 parent 25c2bb1 commit a921ed4

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add argument `&$asGhostObject` to LazyProxy's `DumperInterface` to allow using ghost objects for lazy loading services
88
* Add `enum` env var processor
9+
* Add `shuffle` env var processor
910

1011
6.1
1112
---

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static function getProvidedTypes(): array
5858
'trim' => 'string',
5959
'require' => 'bool|int|float|string|array',
6060
'enum' => \BackedEnum::class,
61+
'shuffle' => 'array',
6162
];
6263
}
6364

@@ -206,6 +207,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
206207
return null;
207208
}
208209

210+
if ('shuffle' === $prefix) {
211+
\is_array($env) ? shuffle($env) : throw new RuntimeException(sprintf('Env var "%s" cannot be shuffled because its value "%s" is not an array.', $name, $env));
212+
213+
return $env;
214+
}
215+
209216
if (!\is_scalar($env)) {
210217
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to "%s".', $name, $prefix));
211218
}

src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,23 @@ public function testGetEnvCsv($value, $processed)
708708
$this->assertSame($processed, $result);
709709
}
710710

711+
public function testGetEnvShuffle()
712+
{
713+
mt_srand(2);
714+
715+
$this->assertSame(
716+
['bar', 'foo'],
717+
(new EnvVarProcessor(new Container()))->getEnv('shuffle', '', fn () => ['foo', 'bar']),
718+
);
719+
}
720+
721+
public function testGetEnvShuffleInvalid()
722+
{
723+
$this->expectException(RuntimeException::class);
724+
$this->expectExceptionMessage('Env var "foo" cannot be shuffled because its value "bar" is not an array');
725+
(new EnvVarProcessor(new Container()))->getEnv('shuffle', 'foo', fn () => 'bar');
726+
}
727+
711728
public function validCsv()
712729
{
713730
$complex = <<<'CSV'

0 commit comments

Comments
 (0)