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

Skip to content

Commit d18d383

Browse files
committed
[DependencyInjection] Add shuffle env processor
1 parent d1d6126 commit d18d383

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 8 additions & 1 deletion
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

@@ -82,7 +83,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
8283
}
8384

8485
if (!isset($array[$key]) && !\array_key_exists($key, $array)) {
85-
throw new EnvNotFoundException(sprintf('Key "%s" not found in %s (resolved from "%s").', $key, json_encode($array), $next));
86+
throw new EnvNotFoundException(sprintf('Key "%s" not found in "%s" (resolved from "%s").', $key, json_encode($array), $next));
8687
}
8788

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

210+
if ('shuffle' === $prefix) {
211+
shuffle($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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,16 @@ 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+
711721
public function validCsv()
712722
{
713723
$complex = <<<'CSV'

0 commit comments

Comments
 (0)