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

Skip to content

[DI] Deprecated using env vars with cannotBeEmpty() #28858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-4.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Config

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

Console
-------
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Config
* Added the `getChildNodeDefinitions()` method to `ParentNodeDefinitionInterface`.
* The `Processor` class has been made final
* Removed `FileLoaderLoadException`, use `LoaderLoadException` instead.
* Using environment variables with `cannotBeEmpty()` if the value is validated with `validate()` will throw an exception.

Console
-------
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

4.1.0
-----
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Config/Definition/ScalarNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ protected function validateType($value)
*/
protected function isValueEmpty($value)
{
// assume environment variables are never empty (which in practice is likely to be true during runtime)
// not doing so breaks many configs that are valid today
if ($this->isHandlingPlaceholder()) {
return false;
}
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Config/Definition/VariableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ protected function validateType($value)
*/
protected function finalizeValue($value)
{
// deny environment variables only when using custom validators
// this avoids ever passing an empty value to final validation closures
if (!$this->allowEmptyValue && $this->isHandlingPlaceholder() && $this->finalValidationClosures) {
@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);
// $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)));
// if ($hint = $this->getInfo()) {
// $e->addHint($hint);
// }
// $e->setPath($this->getPath());
//
// throw $e;
}

if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
$ex = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an empty value, but got %s.', $this->getPath(), json_encode($value)));
if ($hint = $this->getInfo()) {
Expand Down Expand Up @@ -120,6 +133,8 @@ protected function mergeValues($leftSide, $rightSide)
* @param mixed $value
*
* @return bool
*
* @see finalizeValue()
*/
protected function isValueEmpty($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,38 @@ public function testEmptyEnvWhichCannotBeEmptyForScalarNode(): void
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
}

/**
* NOT LEGACY (test exception in 5.0).
*
* @group legacy
* @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.
*/
public function testEmptyEnvWhichCannotBeEmptyForScalarNodeWithValidation(): void
{
$container = new ContainerBuilder();
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = array(
'scalar_node_not_empty_validated' => '%env(SOME)%',
));

$this->doProcess($container);

$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
}

public function testPartialEnvWhichCannotBeEmptyForScalarNode(): void
{
$container = new ContainerBuilder();
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = array(
'scalar_node_not_empty_validated' => 'foo %env(SOME)% bar',
));

$this->doProcess($container);

$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
}

public function testEnvWithVariableNode(): void
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -281,6 +313,14 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('scalar_node')->end()
->scalarNode('scalar_node_not_empty')->cannotBeEmpty()->end()
->scalarNode('scalar_node_not_empty_validated')
->cannotBeEmpty()
->validate()
->always(function ($value) {
return $value;
})
->end()
->end()
->integerNode('int_node')->end()
->floatNode('float_node')->end()
->booleanNode('bool_node')->end()
Expand Down