[DI] Add trim env processor

This commit is contained in:
Maxime Steinhausser 2019-01-04 18:50:27 +01:00 committed by Maxime Steinhausser
parent 44aa362419
commit e226492db7
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

@ -45,6 +45,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
'resolve' => 'string',
'default' => 'bool|int|float|string|array',
'string' => 'string',
'trim' => 'string',
);
}
@ -195,6 +196,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
*/