8000 Deprecate defining parameters from environment variables using SYMFON… · symfony/symfony@83b63c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 83b63c0

Browse files
committed
Deprecate defining parameters from environment variables using SYMFONY__ as prefix
1 parent 4484278 commit 83b63c0

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public function get($name)
100100
throw new ParameterNotFoundException($name, null, null, null, $alternatives, $nonNestedAlternative);
101101
}
102102

103+
if (isset($_SERVER[$sfEnvVar = 'SYMFONY__'.strtoupper(str_replace('.', '__', $name))])) {
104+
@trigger_error(sprintf('Defining parameters using special environment variables that start with "SYMFONY__" (such as "%s") is deprecated as of 3.3 and won\'t be supported in 4.0. Use the %%env()%% syntax to get the value of any environment variable from configuration files instead.', $sfEnvVar), E_USER_DEPRECATED);
105+
}
106+
103107
return $this->parameters[$name];
104108
}
105109

src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ParameterBagTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ public function testEscapeValue()
225225
$this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it');
226226
}
227227

228+
/**
229+
* @group legacy
230+
* @expectedDeprecation Defining parameters using special environment variables that start with "SYMFONY__" (such as "SYMFONY__FOO__BAR") is deprecated as of 3.3 and won't be supported in 4.0. Use the %s syntax to get the value of any environment variable from configuration files instead.
231+
*/
232+
public function testGetSymfonyEnvParameter()
233+
{
234+
$_SERVER['SYMFONY__FOO__BAR'] = 'foobar';
235+
$this->assertSame('foobar', (new ParameterBag(array('foo.bar' => 'foobar')))->get('foo.bar'));
236+
}
237+
228238
/**
229239
* @dataProvider stringsWithSpacesProvider
230240
*/

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ protected function getKernelParameters()
560560
'kernel.charset' => $this->getCharset(),
561561
'kernel.container_class' => $this->getContainerClass(),
562562
),
563-
$this->getEnvParameters()
563+
$this->getEnvParameters(false)
564564
);
565565
}
566566

@@ -570,9 +570,15 @@ protected function getKernelParameters()
570570
* Only the parameters starting with "SYMFONY__" are considered.
571571
*
572572
* @return array An array of parameters
573+
*
574+
* @deprecated since version 3.3, to be removed in 4.0
573575
*/
574576
protected function getEnvParameters()
575577
{
578+
if (0 === func_num_args() || func_get_arg(0)) {
579+
@trigger_error(sprintf('The %s() method is deprecated as of 3.3 and will be removed in 4.0. Use the %%env()%% syntax to get the value of any environment variable from configuration files instead.', __METHOD__), E_USER_DEPRECATED);
580+
}
581+
576582
$parameters = array();
577583
foreach ($_SERVER as $key => $value) {
578584
if (0 === strpos($key, 'SYMFONY__')) {

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,22 @@ public function testKernelWithoutBundles()
734734
$this->assertTrue($kernel->getContainer()->getParameter('test_executed'));
735735
}
736736

737+
/**
738+
* @group legacy
739+
* @expectedDeprecation The Symfony\Component\HttpKernel\Kernel::getEnvParameters() method is deprecated as of 3.3 and will be removed in 4.0. Use the %s syntax to get the value of any environment variable from configuration files instead.
740+
*/
741+
public function testGetEnvParameters()
742+
{
743+
$_SERVER['SYMFONY__FOO__BAR'] = 'baz';
744+
745+
$kernel = $this->getKernel();
746+
$method = new \ReflectionMethod($kernel, 'getEnvParameters');
747+
$method->setAccessible(true);
748+
749+
$envParameters = $method->invoke($kernel);
750+
$this->assertSame('baz', $envParameters['foo.bar']);
751+
}
752+
737753
/**
738754
* Returns a mock for the BundleInterface.
739755
*

0 commit comments

Comments
 (0)
0