Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e226492

Browse files
committed
[DI] Add trim env processor
1 parent 44aa362 commit e226492

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

src/Symfony/Component/DependencyInjection/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

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

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

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

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

src/Symfony/Component/DependencyInjection/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());

src/Symfony/Component/DependencyInjection/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)