Skip to content

Commit b6b3341

Browse files
committed
feature #29781 [DI] Add trim env processor (ogizanagi)
This PR was merged into the 4.3-dev branch. Discussion ---------- [DI] Add trim env processor | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #26708 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | Todo Which is especially useful in combination with the `file` env processor. Commits ------- e226492db7 [DI] Add trim env processor
2 parents d4c8c3b + 478c502 commit b6b3341

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.3.0
55
-----
66

7+
* added `%env(trim:...)%` processor to trim a string value
78
* added `%env(default:...)%` processor to fallback to a default value
89

910
4.2.0

EnvVarProcessor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static function getProvidedTypes()
4444
'resolve' => 'string',
4545
'default' => 'bool|int|float|string|array',
4646
'string' => 'string',
47+
'trim' => 'string',
4748
);
4849
}
4950

@@ -194,6 +195,10 @@ public function getEnv($prefix, $name, \Closure $getEnv)
194195
return str_getcsv($env);
195196
}
196197

198+
if ('trim' === $prefix) {
199+
return trim($env);
200+
}
201+
197202
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
198203
}
199204
}

Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function testSimpleProcessor()
4242
'resolve' => array('string'),
4343
'default' => array('bool', 'int', 'float', 'string', 'array'),
4444
'string' => array('string'),
45+
'trim' => array('string'),
4546
);
4647

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

Tests/EnvVarProcessorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ public function testGetEnvBase64()
233233
$this->assertSame('hello', $result);
234234
}
235235

236+
public function testGetEnvTrim()
237+
{
238+
$processor = new EnvVarProcessor(new Container());
239+
240+
$result = $processor->getEnv('trim', 'foo', function ($name) {
241+
$this->assertSame('foo', $name);
242+
243+
return " hello\n";
244+
});
245+
246+
$this->assertSame('hello', $result);
247+
}
248+
236249
/**
237250
* @dataProvider validJson
238251
*/

0 commit comments

Comments
 (0)