diff --git a/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php b/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php index 83ba7e7076fc1..0a706d90ce636 100644 --- a/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php +++ b/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php @@ -100,6 +100,10 @@ public function get($name) throw new ParameterNotFoundException($name, null, null, null, $alternatives, $nonNestedAlternative); } + if (isset($_SERVER[$sfEnvVar = 'SYMFONY__'.strtoupper(str_replace('.', '__', $name))])) { + @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); + } + return $this->parameters[$name]; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ParameterBagTest.php b/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ParameterBagTest.php index 391e0eb3ff7de..267f5ba619bb8 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ParameterBagTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ParameterBagTest.php @@ -225,6 +225,16 @@ public function testEscapeValue() $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it'); } + /** + * @group legacy + * @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. + */ + public function testGetSymfonyEnvParameter() + { + $_SERVER['SYMFONY__FOO__BAR'] = 'foobar'; + $this->assertSame('foobar', (new ParameterBag(array('foo.bar' => 'foobar')))->get('foo.bar')); + } + /** * @dataProvider stringsWithSpacesProvider */ diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index b8c4e177ee3b9..cff39963d6f66 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -560,7 +560,7 @@ protected function getKernelParameters() 'kernel.charset' => $this->getCharset(), 'kernel.container_class' => $this->getContainerClass(), ), - $this->getEnvParameters() + $this->getEnvParameters(false) ); } @@ -570,9 +570,15 @@ protected function getKernelParameters() * Only the parameters starting with "SYMFONY__" are considered. * * @return array An array of parameters + * + * @deprecated since version 3.3, to be removed in 4.0 */ protected function getEnvParameters() { + if (0 === func_num_args() || func_get_arg(0)) { + @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); + } + $parameters = array(); foreach ($_SERVER as $key => $value) { if (0 === strpos($key, 'SYMFONY__')) { diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 7e77903c5de4c..316721d1ef99b 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -734,6 +734,22 @@ public function testKernelWithoutBundles() $this->assertTrue($kernel->getContainer()->getParameter('test_executed')); } + /** + * @group legacy + * @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. + */ + public function testGetEnvParameters() + { + $_SERVER['SYMFONY__FOO__BAR'] = 'baz'; + + $kernel = $this->getKernel(); + $method = new \ReflectionMethod($kernel, 'getEnvParameters'); + $method->setAccessible(true); + + $envParameters = $method->invoke($kernel); + $this->assertSame('baz', $envParameters['foo.bar']); + } + /** * Returns a mock for the BundleInterface. *