Skip to content

Commit 6c5faef

Browse files
bug #30930 [FrameworkBundle] Fixed issue when a parameter contains a '%' (lyrixx)
This PR was merged into the 4.3 branch. Discussion ---------- [FrameworkBundle] Fixed issue when a parameter contains a '%' | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #31431 | License | MIT | Doc PR | --- On my computer: ``` dump(get_cfg_var('xdebug.file_link_format')); "subl://%f:%l" ``` When I ran `bin/console debug:config framework` I got this exception: ``` In ParameterBag.php line 100: [Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException] The parameter "templating.helper.code.file_link_format" has a dependency on a non-existent parameter "f:". Exception trace: () at /home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php:100 ... ``` This issue was introduced [here](https://github.com/symfony/symfony/pull/27684/files#diff-b3847149480405e1de881530b4c75ab5L212) / cc @ro0NL This PR does not really fix the issue: I'm able to debug the config, The the `debug:container --env-vars` does not work anymore. How could we fix both issue? cc @nicolas-grekas Commits ------- 7bcd714 [FrameworkBundle] Fixed issue when a parameter container a '%'
2 parents 7ef80ff + 7bcd714 commit 6c5faef

File tree

4 files changed

+56
-38
lines changed

4 files changed

+56
-38
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,12 @@ protected function getContainerBuilder()
224224
if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
225225
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel));
226226
$container = $buildContainer();
227+
$container->getCompilerPassConfig()->setRemovingPasses([]);
228+
$container->compile();
227229
} else {
228230
(new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
229-
$container->setParameter('container.build_hash', $hash = ContainerBuilder::hash(__METHOD__));
230-
$container->setParameter('container.build_id', hash('crc32', $hash.time()));
231231
}
232232

233-
$container->getCompilerPassConfig()->setRemovingPasses([]);
234-
$container->compile();
235-
236233
return $this->containerBuilder = $container;
237234
}
238235

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,41 @@ public static function getClassDescription(string $class, string &$resolvedClass
322322

323323
private function getContainerEnvVars(ContainerBuilder $container): array
324324
{
325+
if (!$container->hasParameter('debug.container.dump')) {
326+
return [];
327+
}
328+
329+
if (!is_file($container->getParameter('debug.container.dump'))) {
330+
return [];
331+
}
332+
333+
$file = file_get_contents($container->getParameter('debug.container.dump'));
334+
preg_match_all('{%env\(((?:\w++:)*+\w++)\)%}', $file, $envVars);
335+
$envVars = array_unique($envVars[1]);
336+
337+
$bag = $container->getParameterBag();
338+
$getDefaultParameter = function (string $name) {
339+
return parent::get($name);
340+
};
341+
$getDefaultParameter = $getDefaultParameter->bindTo($bag, \get_class($bag));
342+
325343
$getEnvReflection = new \ReflectionMethod($container, 'getEnv');
326344
$getEnvReflection->setAccessible(true);
345+
327346
$envs = [];
328-
foreach (array_keys($container->getEnvCounters()) as $env) {
347+
348+
foreach ($envVars as $env) {
329349
$processor = 'string';
330350
if (false !== $i = strrpos($name = $env, ':')) {
331351
$name = substr($env, $i + 1);
332352
$processor = substr($env, 0, $i);
333353
}
334-
$defaultValue = ($hasDefault = $container->hasParameter("env($name)")) ? $container->getParameter("env($name)") : null;
354+
$defaultValue = ($hasDefault = $container->hasParameter("env($name)")) ? $getDefaultParameter("env($name)") : null;
335355
if (false === ($runtimeValue = $_ENV[$name] ?? $_SERVER[$name] ?? getenv($name))) {
336356
$runtimeValue = null;
337357
}
338358
$processedValue = ($hasRuntime = null !== $runtimeValue) || $hasDefault ? $getEnvReflection->invoke($container, $env) : null;
339-
$envs[$name.$processor] = [
359+
$envs["$name$processor"] = [
340360
'name' => $name,
341361
'processor' => $processor,
342362
'default_available' => $hasDefault,

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ContainerDebugCommandTest.php

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ public function testIgnoreBackslashWhenFindingService(string $validServiceId)
8383
public function testDescribeEnvVars()
8484
{
8585
putenv('REAL=value');
86-
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
86+
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
8787

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

91+
@unlink(static::$container->getParameter('debug.container.dump'));
92+
9193
$tester = new ApplicationTester($application);
9294
$tester->run(['command' => 'debug:container', '--env-vars' => true], ['decorated' => false]);
9395

@@ -96,13 +98,13 @@ public function testDescribeEnvVars()
9698
Symfony Container Environment Variables
9799
=======================================
98100
99-
--------- ----------------- ------------
100-
Name Default value Real value
101-
--------- ----------------- ------------
102-
JSON "[1, "2.5", 3]" n/a
103-
REAL n/a "value"
104-
UNKNOWN n/a n/a
105-
--------- ----------------- ------------
101+
--------- ----------------- ------------%w
102+
Name Default value Real value%w
103+
--------- ----------------- ------------%w
104+
JSON "[1, "2.5", 3]" n/a%w
105+
REAL n/a "value"%w
106+
UNKNOWN n/a n/a%w
107+
--------- ----------------- ------------%w
106108
107109
// Note real values might be different between web and CLI.%w
108110
@@ -118,35 +120,17 @@ public function testDescribeEnvVars()
118120

119121
public function testDescribeEnvVar()
120122
{
121-
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
123+
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
122124

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

128+
@unlink(static::$container->getParameter('debug.container.dump'));
129+
126130
$tester = new ApplicationTester($application);
127131
$tester->run(['command' => 'debug:container', '--env-var' => 'js'], ['decorated' => false]);
128132

129-
$this->assertContains(<<<'TXT'
130-
%env(float:key:2:json:JSON)%
131-
----------------------------
132-
133-
----------------- -----------------
134-
Default value "[1, "2.5", 3]"
135-
Real value n/a
136-
Processed value 3.0
137-
----------------- -----------------
138-
139-
%env(int:key:2:json:JSON)%
140-
--------------------------
141-
142-
----------------- -----------------
143-
Default value "[1, "2.5", 3]"
144-
Real value n/a
145-
Processed value 3
146-
----------------- -----------------
147-
148-
TXT
149-
, $tester->getDisplay(true));
133+
$this->assertContains(file_get_contents(__DIR__.'/Fixtures/describe_env_vars.txt'), $tester->getDisplay(true));
150134
}
151135

152136
public function provideIgnoreBackslashWhenFindingService()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%env(float:key:2:json:JSON)%
2+
----------------------------
3+
4+
----------------- -----------------
5+
Default value "[1, "2.5", 3]"
6+
Real value n/a
7+
Processed value 3.0
8+
----------------- -----------------
9+
10+
%env(int:key:2:json:JSON)%
11+
--------------------------
12+
13+
----------------- -----------------
14+
Default value "[1, "2.5", 3]"
15+
Real value n/a
16+
Processed value 3
17+
----------------- -----------------

0 commit comments

Comments
 (0)