8000 Deprecate accessing environment variables prefixed by SYMFONY__ as parameters by chalasr · Pull Request #21997 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Deprecate accessing environment variables prefixed by SYMFONY__ as parameters #21997

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ protected function getKernelParameters()
'kernel.charset' => $this->getCharset(),
'kernel.container_class' => $this->getContainerClass(),
),
$this->getEnvParameters()
$this->getEnvParameters(false)
);
}

Expand All @@ -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__')) {
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
0