Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Deprecate non-string default envs #27808

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
Mar 27, 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
17 changes: 17 additions & 0 deletions 17 UPGRADE-4.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ Config

* Deprecated using environment variables with `cannotBeEmpty()` if the value is validated with `validate()`

DependencyInjection
-------------------

* Deprecated support for non-string default env() parameters

Before:
```yaml
parameters:
env(NAME): 1.5
```

After:
```yaml
parameters:
env(NAME): '1.5'
```

EventDispatcher
---------------

Expand Down
17 changes: 17 additions & 0 deletions 17 UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ EventDispatcher
* The `TraceableEventDispatcherInterface` has been removed.
* The signature of the `EventDispatcherInterface::dispatch()` method has been updated to `dispatch($event, string $eventName = null)`

DependencyInjection
-------------------

* Removed support for non-string default env() parameters

Before:
```yaml
parameters:
env(NAME): 1.5
```

After:
```yaml
parameters:
env(NAME): '1.5'
```

Filesystem
----------

Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
* added `ReverseContainer`: a container that turns services back to their ids
* added ability to define an index for a tagged collection
* added ability to define an index for services in an injected service locator argument
* deprecated support for non-string default env() parameters

4.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public function get($name)
if ($this->has($name)) {
$defaultValue = parent::get($name);

if (null !== $defaultValue && !is_scalar($defaultValue)) {
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);
}
}

Expand Down Expand Up @@ -147,9 +150,15 @@ public function resolve()
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)) {
} 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,48 @@ public function testEnvsAreValidatedInConfig()
{
$container = new ContainerBuilder();
$container->setParameter('env(NULLED)', null);
$container->setParameter('env(FLOATISH)', 3.2);
$container->setParameter('env(FLOATISH)', '3.2');
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = [
'scalar_node' => '%env(NULLED)%',
'scalar_node_not_empty' => '%env(FLOATISH)%',
'int_node' => '%env(int:FOO)%',
'float_node' => '%env(float:BAR)%',
'string_node' => '%env(UNDEFINED)%',
]);

$this->doProcess($container);

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

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage Invalid configuration for path "env_extension.string_node": "fail" is not a valid string
*/
public function testDefaultEnvIsValidatedInConfig()
{
$container = new ContainerBuilder();
$container->setParameter('env(STRING)', 'fail');
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = [
'string_node' => '%env(STRING)%',
]);

$this->doProcess($container);
}

/**
* @group legacy
* @expectedDeprecation A non-string default value of an env() parameter is deprecated since 4.3, cast "env(FLOATISH)" to string instead.
*/
public function testDefaultEnvWithoutPrefixIsValidatedInConfig()
{
$container = new ContainerBuilder();
$container->setParameter('env(FLOATISH)', 3.2);
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = [
'float_node' => '%env(FLOATISH)%',
'string_node' => '%env(UNDEFINED)%',
]);

$this->doProcess($container);
Expand Down Expand Up @@ -357,9 +377,9 @@ public function getConfigTreeBuilder()
->scalarNode('string_node')
->validate()
->ifTrue(function ($value) {
return !\is_string($value);
return !\is_string($value) || 'fail' === $value;
})
->thenInvalid('%s is not a string')
->thenInvalid('%s is not a valid string')
->end()
->end()
->end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public function testMergeWithDifferentIdentifiersForPlaceholders()
$this->assertCount(2, $merged[$envName]);
}

/**
* @group legacy
* @expectedDeprecation A non-string default value of env parameter "INT_VAR" is deprecated since 4.3, cast it to string instead.
*/
public function testResolveEnvCastsIntToString()
{
$bag = new EnvPlaceholderParameterBag();
Expand All @@ -120,6 +124,34 @@ public function testResolveEnvCastsIntToString()
$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.
*/
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)']);
}

public function testGetDefaultEnv()
{
$bag = new EnvPlaceholderParameterBag();
$this->assertStringMatchesFormat('env_%s_INT_VAR_%s', $bag->get('env(INT_VAR)'));
$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)']);
}

public function testResolveEnvAllowsNull()
{
$bag = new EnvPlaceholderParameterBag();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.