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

Commit bc18e39

Browse filesBrowse files
committed
feature #27808 [DI] Deprecate non-string default envs (ro0NL)
This PR was merged into the 4.3-dev branch. Discussion ---------- [DI] Deprecate non-string default envs | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes-ish | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | yes | Tests pass? | no <!-- please add some, will be required by reviewers --> | Fixed tickets | #27680, #27470 (comment) | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> This is a failing test to further clarify the issues raised in #27680 So given #27680 (comment) > We should be sure this solves a real-world issue. I think it solves a real bug :) Commits ------- 2311437 [DI] Deprecate non-string default envs
2 parents c949f9a + 2311437 commit bc18e39
Copy full SHA for bc18e39

File tree

6 files changed

+102
-6
lines changed
Filter options

6 files changed

+102
-6
lines changed

‎UPGRADE-4.3.md

Copy file name to clipboardExpand all lines: UPGRADE-4.3.md
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ Config
2121

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

24+
DependencyInjection
25+
-------------------
26+
27+
* Deprecated support for non-string default env() parameters
28+
29+
Before:
30+
```yaml
31+
parameters:
32+
env(NAME): 1.5
33+
```
34+
35+
After:
36+
```yaml
37+
parameters:
38+
env(NAME): '1.5'
39+
```
40+
2441
EventDispatcher
2542
---------------
2643

‎UPGRADE-5.0.md

Copy file name to clipboardExpand all lines: UPGRADE-5.0.md
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ EventDispatcher
7272
* The `TraceableEventDispatcherInterface` has been removed.
7373
* The signature of the `EventDispatcherInterface::dispatch()` method has been updated to `dispatch($event, string $eventName = null)`
7474

75+
DependencyInjection
76+
-------------------
77+
78+
* Removed support for non-string default env() parameters
79+
80+
Before:
81+
```yaml
82+
parameters:
83+
env(NAME): 1.5
84+
```
85+
86+
After:
87+
```yaml
88+
parameters:
89+
env(NAME): '1.5'
90+
```
91+
7592
Filesystem
7693
----------
7794

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* added `ReverseContainer`: a container that turns services back to their ids
1414
* added ability to define an index for a tagged collection
1515
* added ability to define an index for services in an injected service locator argument
16+
* deprecated support for non-string default env() parameters
1617

1718
4.2.0
1819
-----

‎src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ public function get($name)
4949
if ($this->has($name)) {
5050
$defaultValue = parent::get($name);
5151

52-
if (null !== $defaultValue && !is_scalar($defaultValue)) {
52+
if (null !== $defaultValue && !is_scalar($defaultValue)) { // !is_string in 5.0
53+
//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));
5354
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
55+
} elseif (is_scalar($defaultValue) && !\is_string($defaultValue)) {
56+
@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);
5457
}
5558
}
5659

@@ -147,9 +150,15 @@ public function resolve()
147150
continue;
148151
}
149152
if (is_numeric($default = $this->parameters[$name])) {
153+
if (!\is_string($default)) {
154+
@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);
155+
}
150156
$this->parameters[$name] = (string) $default;
151-
} elseif (null !== $default && !is_scalar($default)) {
157+
} elseif (null !== $default && !is_scalar($default)) { // !is_string in 5.0
158+
//throw new RuntimeException(sprintf('The default value of env parameter "%s" must be a string or null, %s given.', $env, \gettype($default)));
152159
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, %s given.', $env, \gettype($default)));
160+
} elseif (is_scalar($default) && !\is_string($default)) {
161+
@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);
153162
}
154163
}
155164
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php
+24-4Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,48 @@ public function testEnvsAreValidatedInConfig()
2727
{
2828
$container = new ContainerBuilder();
2929
$container->setParameter('env(NULLED)', null);
30-
$container->setParameter('env(FLOATISH)', 3.2);
30+
$container->setParameter('env(FLOATISH)', '3.2');
3131
$container->registerExtension($ext = new EnvExtension());
3232
$container->prependExtensionConfig('env_extension', $expected = [
3333
'scalar_node' => '%env(NULLED)%',
3434
'scalar_node_not_empty' => '%env(FLOATISH)%',
3535
'int_node' => '%env(int:FOO)%',
3636
'float_node' => '%env(float:BAR)%',
37+
'string_node' => '%env(UNDEFINED)%',
3738
]);
3839

3940
$this->doProcess($container);
4041

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

45+
/**
46+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
47+
* @expectedExceptionMessage Invalid configuration for path "env_extension.string_node": "fail" is not a valid string
48+
*/
49+
public function testDefaultEnvIsValidatedInConfig()
50+
{
51+
$container = new ContainerBuilder();
52+
$container->setParameter('env(STRING)', 'fail');
53+
$container->registerExtension($ext = new EnvExtension());
54+
$container->prependExtensionConfig('env_extension', $expected = [
55+
'string_node' => '%env(STRING)%',
56+
]);
57+
58+
$this->doProcess($container);
59+
}
60+
61+
/**
62+
* @group legacy
63+
* @expectedDeprecation A non-string default value of an env() parameter is deprecated since 4.3, cast "env(FLOATISH)" to string instead.
64+
*/
4465
public function testDefaultEnvWithoutPrefixIsValidatedInConfig()
4566
{
4667
$container = new ContainerBuilder();
4768
$container->setParameter('env(FLOATISH)', 3.2);
4869
$container->registerExtension($ext = new EnvExtension());
4970
$container->prependExtensionConfig('env_extension', $expected = [
5071
'float_node' => '%env(FLOATISH)%',
51-
'string_node' => '%env(UNDEFINED)%',
5272
]);
5373

5474
$this->doProcess($container);
@@ -357,9 +377,9 @@ public function getConfigTreeBuilder()
357377
->scalarNode('string_node')
358378
->validate()
359379
->ifTrue(function ($value) {
360-
return !\is_string($value);
380+
return !\is_string($value) || 'fail' === $value;
361381
})
362-
->thenInvalid('%s is not a string')
382+
->thenInvalid('%s is not a valid string')
363383
->end()
364384
->end()
365385
->end();

‎src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ public function testMergeWithDifferentIdentifiersForPlaceholders()
111111
$this->assertCount(2, $merged[$envName]);
112112
}
113113

114+
/**
115+
* @group legacy
116+
* @expectedDeprecation A non-string default value of env parameter "INT_VAR" is deprecated since 4.3, cast it to string instead.
117+
*/
114118
public function testResolveEnvCastsIntToString()
115119
{
116120
$bag = new EnvPlaceholderParameterBag();
@@ -120,6 +124,34 @@ public function testResolveEnvCastsIntToString()
120124
$this->assertSame('2', $bag->all()['env(INT_VAR)']);
121125
}
122126

127+
/**
128+
* @group legacy
129+
* @expectedDeprecation A non-string default value of an env() parameter is deprecated since 4.3, cast "env(INT_VAR)" to string instead.
130+
* @expectedDeprecation A non-string default value of env parameter "INT_VAR" is deprecated since 4.3, cast it to string instead.
131+
*/
132+
public function testGetDefaultScalarEnv()
133+
{
134+
$bag = new EnvPlaceholderParameterBag();
135+
$bag->set('env(INT_VAR)', 2);
136+
$this->assertStringMatchesFormat('env_%s_INT_VAR_%s', $bag->get('env(INT_VAR)'));
137+
$this->assertSame(2, $bag->all()['env(INT_VAR)']);
138+
$bag->resolve();
139+
$this->assertStringMatchesFormat('env_%s_INT_VAR_%s', $bag->get('env(INT_VAR)'));
140+
$this->assertSame('2', $bag->all()['env(INT_VAR)']);
141+
}
142+
143+
public function testGetDefaultEnv()
144+
{
145+
$bag = new EnvPlaceholderParameterBag();
146+
$this->assertStringMatchesFormat('env_%s_INT_VAR_%s', $bag->get('env(INT_VAR)'));
147+
$bag->set('env(INT_VAR)', '2');
148+
$this->assertStringMatchesFormat('env_%s_INT_VAR_%s', $bag->get('env(INT_VAR)'));
149+
$this->assertSame('2', $bag->all()['env(INT_VAR)']);
150+
$bag->resolve();
151+
$this->assertStringMatchesFormat('env_%s_INT_VAR_%s', $bag->get('env(INT_VAR)'));
152+
$this->assertSame('2', $bag->all()['env(INT_VAR)']);
153+
}
154+
123155
public function testResolveEnvAllowsNull()
124156
{
125157
$bag = new EnvPlaceholderParameterBag();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.