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
This commit is contained in:
Fabien Potencier 2019-01-06 17:27:17 +01:00
commit 0083ba1d88
4 changed files with 20 additions and 0 deletions

View File

@ -4,6 +4,7 @@ CHANGELOG
4.3.0
-----
* added `%env(trim:...)%` processor to trim a string value
* added `%env(default:...)%` processor to fallback to a default value
4.2.0

View File

@ -44,6 +44,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
'resolve' => 'string',
'default' => 'bool|int|float|string|array',
'string' => 'string',
'trim' => 'string',
);
}
@ -194,6 +195,10 @@ class EnvVarProcessor implements EnvVarProcessorInterface
return str_getcsv($env);
}
if ('trim' === $prefix) {
return trim($env);
}
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
}
}

View File

@ -42,6 +42,7 @@ class RegisterEnvVarProcessorsPassTest extends TestCase
'resolve' => array('string'),
'default' => array('bool', 'int', 'float', 'string', 'array'),
'string' => array('string'),
'trim' => array('string'),
);
$this->assertSame($expected, $container->getParameterBag()->getProvidedTypes());

View File

@ -233,6 +233,19 @@ class EnvVarProcessorTest extends TestCase
$this->assertSame('hello', $result);
}
public function testGetEnvTrim()
{
$processor = new EnvVarProcessor(new Container());
$result = $processor->getEnv('trim', 'foo', function ($name) {
$this->assertSame('foo', $name);
return " hello\n";
});
$this->assertSame('hello', $result);
}
/**
* @dataProvider validJson
*/