8000 [FrameworkBundle] Fixed issue when a parameter contains a '%' by lyrixx · Pull Request #30930 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Fixed issue when a parameter contains a '%' #30930

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
May 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,12 @@ protected function getContainerBuilder()
if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel));
$container = $buildContainer();
$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->compile();
} else {
(new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
$container->setParameter('container.build_hash', $hash = ContainerBuilder::hash(__METHOD__));
$container->setParameter('container.build_id', hash('crc32', $hash.time()));
}

$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->compile();

return $this->containerBuilder = $container;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,41 @@ public static function getClassDescription(string $class, string &$resolvedClass

private function getContainerEnvVars(ContainerBuilder $container): array
{
if (!$container->hasParameter('debug.container.dump')) {
return [];
}

if (!is_file($container->getParameter('debug.container.dump'))) {
return [];
}

$file = file_get_contents($container->getParameter('debug.container.dump'));
preg_match_all('{%env\(((?:\w++:)*+\w++)\)%}', $file, $envVars);
$envVars = array_unique($envVars[1]);

$bag = $container->getParameterBag();
$getDefaultParameter = function (string $name) {
return parent::get($name);
};
$getDefaultParameter = $getDefaultParameter->bindTo($bag, \get_class($bag));

$getEnvReflection = new \ReflectionMethod($container, 'getEnv');
$getEnvReflection->setAccessible(true);

$envs = [];
foreach (array_keys($container->getEnvCounters()) as $env) {

foreach ($envVars as $env) {
$processor = 'string';
if (false !== $i = strrpos($name = $env, ':')) {
$name = substr($env, $i + 1);
$processor = substr($env, 0, $i);
}
$defaultValue = ($hasDefault = $container->hasParameter("env($name)")) ? $container->getParameter("env($name)") : null;
$defaultValue = ($hasDefault = $container->hasParameter("env($name)")) ? $getDefaultParameter("env($name)") : null;
if (false === ($runtimeValue = $_ENV[$name] ?? $_SERVER[$name] ?? getenv($name))) {
$runtimeValue = null;
}
$processedValue = ($hasRuntime = null !== $runtimeValue) || $hasDefault ? $getEnvReflection->invoke($container, $env) : null;
$envs[$name.$processor] = [
$envs["$name$processor"] = [
'name' => $name,
'processor' => $processor,
'default_available' => $hasDefault,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ public function testIgnoreBackslashWhenFindingService(string $validServiceId)
public function testDescribeEnvVars()
{
putenv('REAL=value');
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);

$application = new Application(static::$kernel);
$application->setAutoExit(false);

@unlink(static::$container->getParameter('debug.container.dump'));

$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--env-vars' => true], ['decorated' => false]);

Expand All @@ -96,13 +98,13 @@ public function testDescribeEnvVars()
Symfony Container Environment Variables
=======================================

--------- ----------------- ------------
Name Default value Real value
--------- ----------------- ------------
JSON "[1, "2.5", 3]" n/a
REAL n/a "value"
UNKNOWN n/a n/a
--------- ----------------- ------------
--------- ----------------- ------------%w
Name Default value Real value%w
--------- ----------------- ------------%w
JSON "[1, "2.5", 3]" n/a%w
REAL n/a "value"%w
UNKNOWN n/a n/a%w
--------- ----------------- ------------%w

// Note real values might be different between web and CLI.%w

Expand All @@ -118,35 +120,17 @@ public function testDescribeEnvVars()

public function testDescribeEnvVar()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);

$application = new Application(static::$kernel);
$application->setAutoExit(false);

@unlink(static::$container->getParameter('debug.container.dump'));

$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--env-var' => 'js'], ['decorated' => false]);

$this->assertContains(<<<'TXT'
%env(float:key:2:json:JSON)%
----------------------------

----------------- -----------------
Default value "[1, "2.5", 3]"
Real value n/a
Processed value 3.0
----------------- -----------------

%env(int:key:2:json:JSON)%
--------------------------

----------------- -----------------
Default value "[1, "2.5", 3]"
Real value n/a
Processed value 3
----------------- -----------------

TXT
, $tester->getDisplay(true));
$this->assertContains(file_get_contents(__DIR__.'/Fixtures/describe_env_vars.txt'), $tester->getDisplay(true));
}

public function provideIgnoreBackslashWhenFindingService()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%env(float:key:2:json:JSON)%
----------------------------

----------------- -----------------
Default value "[1, "2.5", 3]"
Real value n/a
Processed value 3.0
----------------- -----------------

%env(int:key:2:json:JSON)%
--------------------------

----------------- -----------------
Default value "[1, "2.5", 3]"
Real value n/a
Processed value 3
----------------- -----------------
0