Skip to content

Commit b47cb73

Browse files
committed
feature #52369 [DependencyInjection] Add urlencode function to EnvVarProcessor (crtl)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [DependencyInjection] Add `urlencode` function to `EnvVarProcessor` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT This PR adds `urlencode` to the default `EnvVarProcessor` to allow encoding environment variables in config files. The comes handy when environment variables are provided by the OS and not through .env because those values are often not compatible with DSN syntax for doctrine or mailer components. In my case our app was deployed on an AWS stack and environment variables for SMPT and database where automatically provided in the deployment process and replaced in the respective .env files. Because those values where not encoded they would cause issues with DSN format requireing me to commit them in my `.env.prod` which circumvents the purpose of the whole system. Usage: ```yaml # config/packages/mailer.yaml framework: mailer: dsn: 'smtp://%env(urlencode:SMTP_USER)%:%env(urlencode:SMTP_PASSWORD)%@%env(urlencode:SMTP_HOST)%?encryption=%env(SMTP_ENCRYPTION)%&port=%env(SMTP_PORT)%&auth_mode=login' ``` Commits ------- 1439858fc8e [DependencyInjection] Add `urlencode` function to `EnvVarProcessor`
2 parents 71c053f + 9052626 commit b47cb73

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

CHANGELOG.md

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

2627
6.3
2728
---

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
}

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());

Tests/EnvVarProcessorTest.php

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

962+
public function testGetEnvUrlencode()
963+
{
964+
$processor = new EnvVarProcessor(new Container());
965+
966+
$result = $processor->getEnv('urlencode', 'URLENCODETEST', function () {
967+
return 'foo: Data123!@-_ + bar: Not the same content as Data123!@-_ +';
968+
});
969+
970+
$this->assertSame('foo%3A%20Data123%21%40-_%20%2B%20bar%3A%20Not%20the%20same%20content%20as%20Data123%21%40-_%20%2B', $result);
971+
}
972+
962973
public static function provideGetEnvDefined(): iterable
963974
{
964975
yield 'Defined' => [true, fn () => 'foo'];

0 commit comments

Comments
 (0)