8000 [DependencyInjection] Add `urlencode` function to `EnvVarProcessor` · symfony/symfony@1439858 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 1439858

Browse files
crtlfabpot
authored andcommitted
[DependencyInjection] Add urlencode function to EnvVarProcessor
1 parent 61023a7 commit 1439858

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Deprecate `ContainerAwareInterface` and `ContainerAwareTrait`, use dependency injection instead
99
* Add `defined` env var processor that returns `true` for defined and neither null nor empty env vars
1010
* Add `#[AutowireLocator]` and `#[AutowireIterator]` attributes
11+
* Add `urlencode` env var processor that url encodes a string value
1112

1213
6.3
1314
---

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static function getProvidedTypes(): array
5757
'enum' => \BackedEnum::class,
5858
'shuffle' => 'array',
5959
'defined' => 'bool',
60+
'urlencode' => 'string',
6061
];
6162
}
6263

@@ -344,6 +345,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
344345
return trim($env);
345346
}
346347

348+
if ('urlencode' === $prefix) {
349+
return rawurlencode($env);
350+
}
351+
347352
throw new RuntimeException(sprintf('Unsupported env var prefix "%s" for env name "%s".', $prefix, $name));
348353
}
349354
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function testSimpleProcessor()
5151
'enum' => [\BackedEnum::class],
5252
'shuffle' => ['array'],
5353
'defined' => ['bool'],
54+
'urlencode' => ['string'],
5455
];
5556

5657
$this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,17 @@ public function testGetEnvDefined(bool $expected, callable $callback)
948948
$this->assertSame($expected, (new EnvVarProcessor(new Container()))->getEnv('defined', 'NO_SOMETHING', $callback));
949949
}
950950

951+
public function testGetEnvUrlencode()
952+
{
953+
$processor = new EnvVarProcessor(new Container());
954+
955+
$result = $processor->getEnv('urlencode', 'URLENCODETEST', function () {
956+
return 'foo: Data123!@-_ + bar: Not the same content as Data123!@-_ +';
957+
});
958+
959+
$this->assertSame('foo%3A%20Data123%21%40-_%20%2B%20bar%3A%20Not%20the%20same%20content%20as%20Data123%21%40-_%20%2B', $result);
960+
}
961+
951962
public static function provideGetEnvDefined(): iterable
952963
{
953964
yield 'Defined' => [true, fn () => 'foo'];

0 commit comments

Comments
 (0)
0