Skip to content

Commit 397c19e

Browse files
ro0NLnicolas-grekas
authored andcommitted
[DI] Deprecated using env vars with cannotBeEmpty()
1 parent 91f6e69 commit 397c19e

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

UPGRADE-4.2.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Config
1616

1717
* Deprecated constructing a `TreeBuilder` without passing root node information.
1818
* Deprecated `FileLoaderLoadException`, use `LoaderLoadException` instead.
19+
* Deprecated using environment variables with `cannotBeEmpty()` if the value is validated with `validate()`
1920

2021
Console
2122
-------

UPGRADE-5.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Config
1818
* Added the `getChildNodeDefinitions()` method to `ParentNodeDefinitionInterface`.
1919
* The `Processor` class has been made final
2020
* Removed `FileLoaderLoadException`, use `LoaderLoadException` instead.
21+
* Using environment variables with `cannotBeEmpty()` if the value is validated with `validate()` will throw an exception.
2122

2223
Console
2324
-------

src/Symfony/Component/Config/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* deprecated constructing a `TreeBuilder` without passing root node information
88
* renamed `FileLoaderLoadException` to `LoaderLoadException`
9+
* deprecated using environment variables with `cannotBeEmpty()` if the value is validated with `validate()`
910

1011
4.1.0
1112
-----

src/Symfony/Component/Config/Definition/ScalarNode.php

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ protected function validateType($value)
4848
*/
4949
protected function isValueEmpty($value)
5050
{
51+
// assume environment variables are never empty (which in practice is likely to be true during runtime)
52+
// not doing so breaks many configs that are valid today
5153
if ($this->isHandlingPlaceholder()) {
5254
return false;
5355
}

src/Symfony/Component/Config/Definition/VariableNode.php

+15
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ protected function validateType($value)
8181
*/
8282
protected function finalizeValue($value)
8383
{
84+
// deny environment variables only when using custom validators
85+
// this avoids ever passing an empty value to final validation closures
86+
if (!$this->allowEmptyValue && $this->isHandlingPlaceholder() && $this->finalValidationClosures) {
87+
@trigger_error(sprintf('Setting path "%s" to an environment variable is deprecated since Symfony 4.2. Remove "cannotBeEmpty()", "validate()" or include a prefix/suffix value instead.', $this->getPath()), E_USER_DEPRECATED);
88+
// $e = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an environment variable when empty values are not allowed by definition and are validated.', $this->getPath(), json_encode($value)));
89+
// if ($hint = $this->getInfo()) {
90+
// $e->addHint($hint);
91+
// }
92+
// $e->setPath($this->getPath());
93+
//
94+
// throw $e;
95+
}
96+
8497
if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
8598
$ex = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an empty value, but got %s.', $this->getPath(), json_encode($value)));
8699
if ($hint = $this->getInfo()) {
@@ -120,6 +133,8 @@ protected function mergeValues($leftSide, $rightSide)
120133
* @param mixed $value
121134
*
122135
* @return bool
136+
*
137+
* @see finalizeValue()
123138
*/
124139
protected function isValueEmpty($value)
125140
{

src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,38 @@ public function testEmptyEnvWhichCannotBeEmptyForScalarNode(): void
210210
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
211211
}
212212

213+
/**
214+
* NOT LEGACY (test exception in 5.0).
215+
*
216+
* @group legacy
217+
* @expectedDeprecation Setting path "env_extension.scalar_node_not_empty_validated" to an environment variable is deprecated since Symfony 4.2. Remove "cannotBeEmpty()", "validate()" or include a prefix/suffix value instead.
218+
*/
219+
public function testEmptyEnvWhichCannotBeEmptyForScalarNodeWithValidation(): void
220+
{
221+
$container = new ContainerBuilder();
222+
$container->registerExtension($ext = new EnvExtension());
223+
$container->prependExtensionConfig('env_extension', $expected = array(
224+
'scalar_node_not_empty_validated' => '%env(SOME)%',
225+
));
226+
227+
$this->doProcess($container);
228+
229+
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
230+
}
231+
232+
public function testPartialEnvWhichCannotBeEmptyForScalarNode(): void
233+
{
234+
$container = new ContainerBuilder();
235+
$container->registerExtension($ext = new EnvExtension());
236+
$container->prependExtensionConfig('env_extension', $expected = array(
237+
'scalar_node_not_empty_validated' => 'foo %env(SOME)% bar',
238+
));
239+
240+
$this->doProcess($container);
241+
242+
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
243+
}
244+
213245
public function testEnvWithVariableNode(): void
214246
{
215247
$container = new ContainerBuilder();
@@ -281,6 +313,14 @@ public function getConfigTreeBuilder()
281313
->children()
282314
->scalarNode('scalar_node')->end()
283315
->scalarNode('scalar_node_not_empty')->cannotBeEmpty()->end()
316+
->scalarNode('scalar_node_not_empty_validated')
317+
->cannotBeEmpty()
318+
->validate()
319+
->always(function ($value) {
320+
return $value;
321+
})
322+
->end()
323+
->end()
284324
->integerNode('int_node')->end()
285325
->floatNode('float_node')->end()
286326
->booleanNode('bool_node')->end()

0 commit comments

Comments
 (0)