Skip to content

[DependencyInjection] Add urlencode function to EnvVarProcessor #52369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Deprecate `ContainerAwareInterface` and `ContainerAwareTrait`, use dependency injection instead
* Add `defined` env var processor that returns `true` for defined and neither null nor empty env vars
* Add `#[AutowireLocator]` and `#[AutowireIterator]` attributes
* Add `urlencode` env var processor that url encodes a string value

6.3
---
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static function getProvidedTypes(): array
'enum' => \BackedEnum::class,
'shuffle' => 'array',
'defined' => 'bool',
'urlencode' => 'string',
];
}

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

if ('urlencode' === $prefix) {
return rawurlencode($env);
}

throw new RuntimeException(sprintf('Unsupported env var prefix "%s" for env name "%s".', $prefix, $name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function testSimpleProcessor()
'enum' => [\BackedEnum::class],
'shuffle' => ['array'],
'defined' => ['bool'],
'urlencode' => ['string'],
];

$this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,17 @@ public function testGetEnvDefined(bool $expected, callable $callback)
$this->assertSame($expected, (new EnvVarProcessor(new Container()))->getEnv('defined', 'NO_SOMETHING', $callback));
}

public function testGetEnvUrlencode()
{
$processor = new EnvVarProcessor(new Container());

$result = $processor->getEnv('urlencode', 'URLENCODETEST', function () {
return 'foo: Data123!@-_ + bar: Not the same content as Data123!@-_ +';
});

$this->assertSame('foo%3A%20Data123%21%40-_%20%2B%20bar%3A%20Not%20the%20same%20content%20as%20Data123%21%40-_%20%2B', $result);
}

public static function provideGetEnvDefined(): iterable
{
yield 'Defined' => [true, fn () => 'foo'];
Expand Down