8000 [DI] remove support for non-string default env() parameters by ro0NL · Pull Request #31731 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] remove support for non-string default env() parameters #31731

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
May 30, 2019
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 src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* removed support for auto-discovered extension configuration class which does not implement `ConfigurationInterface`
* removed support for non-string default env() parameters

4.4.0
-----
Expand Down
10000
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,8 @@ public function get($name)
if (!preg_match('/^(?:\w*+:)*+\w++$/', $env)) {
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
}

if ($this->has($name)) {
$defaultValue = parent::get($name);

if (null !== $defaultValue && !is_scalar($defaultValue)) { // !is_string in 5.0
//throw new RuntimeException(sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
} elseif (is_scalar($defaultValue) && !\is_string($defaultValue)) {
@trigger_error(sprintf('A non-string default value of an env() parameter is deprecated since 4.3, cast "%s" to string instead.', $name), E_USER_DEPRECATED);
}
if ($this->has($name) && null !== ($defaultValue = parent::get($name)) && !\is_string($defaultValue)) {
throw new RuntimeException(sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
}

$uniqueName = md5($name.uniqid(mt_rand(), true));
Expand Down Expand Up @@ -146,19 +138,8 @@ public function resolve()
parent::resolve();

foreach ($this->envPlaceholders as $env => $placeholders) {
if (!$this->has($name = "env($env)")) {
continue;
}
if (is_numeric($default = $this->parameters[$name])) {
if (!\is_string($default)) {
@trigger_error(sprintf('A non-string default value of env parameter "%s" is deprecated since 4.3, cast it to string instead.', $env), E_USER_DEPRECATED);
}
$this->parameters[$name] = (string) $default;
} elseif (null !== $default && !is_scalar($default)) { // !is_string in 5.0
//throw new RuntimeException(sprintf('The default value of env parameter "%s" must be a string or null, %s given.', $env, \gettype($default)));
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, %s given.', $env, \gettype($default)));
} elseif (is_scalar($default) && !\is_string($default)) {
@trigger_error(sprintf('A non-string default value of env parameter "%s" is deprecated since 4.3, cast it to string instead.', $env), E_USER_DEPRECATED);
if ($this->has($name = "env($env)") && null !== ($default = $this->parameters[$name]) && !\is_string($default)) {
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be a string or null, %s given.', $env, \gettype($default)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function testDefaultEnvIsValidatedInConfig()
}

/**
* @group legacy
* @expectedDeprecation A non-string default value of an env() parameter is deprecated since 4.3, cast "env(FLOATISH)" to string instead.
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The default value of an env() parameter must be a string or null, but "double" given to "env(FLOATISH)".
*/
public function testDefaultEnvWithoutPrefixIsValidatedInConfig()
{
Expand All @@ -72,8 +72,6 @@ public function testDefaultEnvWithoutPrefixIsValidatedInConfig()
]);

$this->doProcess($container);

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,26 @@ public function testMergeWithDifferentIdentifiersForPlaceholders()
}

/**
* @group legacy
* @expectedDeprecation A non-string default value of env parameter "INT_VAR" is deprecated since 4.3, cast it to string instead.
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The default value of env parameter "INT_VAR" must be a string or null, integer given.
*/
public function testResolveEnvCastsIntToString()
public function testResolveEnvRequiresStrings()
{
$bag = new EnvPlaceholderParameterBag();
$bag->get('env(INT_VAR)');
$bag->set('env(INT_VAR)', 2);
$bag->resolve();
$this->assertSame('2', $bag->all()['env(INT_VAR)']);
}

/**
* @group legacy
* @expectedDeprecation A non-string default value of an env() parameter is deprecated since 4.3, cast "env(INT_VAR)" to string instead.
* @expectedDeprecation A non-string default value of env parameter "INT_VAR" is deprecated since 4.3, cast it to string instead.
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The default value of an env() parameter must be a string or null, but "integer" given to "env(INT_VAR)".
*/
public function testGetDefaultScalarEnv()
{
$bag = new EnvPlaceholderParameterBag();
$bag->set('env(INT_VAR)', 2);
$this->assertStringMatchesFormat('env_%s_INT_VAR_%s', $bag->get('env(INT_VAR)'));
$this->assertSame(2, $bag->all()['env(INT_VAR)']);
$bag->resolve();
$this->assertStringMatchesFormat('env_%s_INT_VAR_%s', $bag->get('env(INT_VAR)'));
$this->assertSame('2', $bag->all()['env(INT_VAR)']);
$bag->get('env(INT_VAR)');
}

public function testGetDefaultEnv()
Expand All @@ -163,7 +157,7 @@ public function testResolveEnvAllowsNull()

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be scalar or null, array given.
* @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be a string or null, array given.
*/
public function testResolveThrowsOnBadDefaultValue()
{
Expand All @@ -185,7 +179,7 @@ public function testGetEnvAllowsNull()

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The default value of an env() parameter must be scalar or null, but "array" given to "env(ARRAY_VAR)".
* @expectedExceptionMessage The default value of an env() parameter must be a string or null, but "array" given to "env(ARRAY_VAR)".
*/
public function testGetThrowsOnBadDefaultValue()
{
Expand Down
0