Skip to content

Commit d04cb7b

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

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
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: 7 additions & 0 deletions
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, expected array, got "%s".', $name, get_debug_type($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: 17 additions & 0 deletions
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, expected array, got "string".');
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)
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