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

Skip to content

Commit e695449

Browse files
feature #28858 [DI] Deprecated using env vars with cannotBeEmpty() (ro0NL)
This PR was squashed before being merged into the 4.3-dev branch (closes #28858). Discussion ---------- [DI] Deprecated using env vars with cannotBeEmpty() | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes-ish | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | yes | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #28827 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Continuation of #28838 for 4.2 Using environment variables for nodes marked `cannotBeEmpty()` is semantically not possible, we'll never know the value is empty yes/no during compile time. Neither we should assume one or another. Commits ------- 397c19e [DI] Deprecated using env vars with cannotBeEmpty()
2 parents 67be665 + 397c19e commit e695449

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

UPGRADE-4.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Config
2929
```
3030

3131
* Deprecated `FileLoaderLoadException`, use `LoaderLoadException` instead.
32+
* Deprecated using environment variables with `cannotBeEmpty()` if the value is validated with `validate()`
3233

3334
Console
3435
-------

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,38 @@ public function testEmptyEnvWhichCannotBeEmptyForScalarNode(): void
211211
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
212212
}
213213

214+
/**
215+
* NOT LEGACY (test exception in 5.0).
216+
*
217+
* @group legacy
218+
* @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.
219+
*/
220+
public function testEmptyEnvWhichCannotBeEmptyForScalarNodeWithValidation(): void
221+
{
222+
$container = new ContainerBuilder();
223+
$container->registerExtension($ext = new EnvExtension());
224+
$container->prependExtensionConfig('env_extension', $expected = array(
225+
'scalar_node_not_empty_validated' => '%env(SOME)%',
226+
));
227+
228+
$this->doProcess($container);
229+
230+
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
231+
}
232+
233+
public function testPartialEnvWhichCannotBeEmptyForScalarNode(): void
234+
{
235+
$container = new ContainerBuilder();
236+
$container->registerExtension($ext = new EnvExtension());
237+
$container->prependExtensionConfig('env_extension', $expected = array(
238+
'scalar_node_not_empty_validated' => 'foo %env(SOME)% bar',
239+
));
240+
241+
$this->doProcess($container);
242+
243+
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
244+
}
245+
214246
public function testEnvWithVariableNode(): void
215247
{
216248
$container = new ContainerBuilder();
@@ -282,6 +314,14 @@ public function getConfigTreeBuilder()
282314
->children()
283315
->scalarNode('scalar_node')->end()
284316
->scalarNode('scalar_node_not_empty')->cannotBeEmpty()->end()
317+
->scalarNode('scalar_node_not_empty_validated')
318+
->cannotBeEmpty()
319+
->validate()
320+
->always(function ($value) {
321+
return $value;
322+
})
323+
->end()
324+
->end()
285325
->integerNode('int_node')->end()
286326
->floatNode('float_node')->end()
287327
->booleanNode('bool_node')->end()

0 commit comments

Comments
 (0)