Skip to content

Commit 09212b5

Browse files
committed
Show inlined env vars in debug:container output
1 parent e16aea4 commit 09212b5

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,21 @@ private function getContainerEnvVars(ContainerBuilder $container): array
308308

309309
$envs = [];
310310

311+
if ($container->hasParameter('.debug.container.build_time_env_vars')) {
312+
foreach ($container->getParameter('.debug.container.build_time_env_vars') as $name => $value) {
313+
$envs["{$name}inline"] = [
314+
'name' => $name,
315+
'inlined' => true,
316+
'processor' => 'string',
317+
'default_available' => false,
318+
'default_value' => null,
319+
'runtime_available' => true,
320+
'runtime_value' => $value,
321+
'processed_value' => $value, // Env var processors are not supported at the moment
322+
];
323+
}
324+
}
325+
311326
foreach ($envVars as $env) {
312327
$processor = 'string';
313328
if (false !== $i = strrpos($name = $env, ':')) {
@@ -321,6 +336,7 @@ private function getContainerEnvVars(ContainerBuilder $container): array
321336
$processedValue = ($hasRuntime = null !== $runtimeValue) || $hasDefault ? $getEnvReflection->invoke($container, $env) : null;
322337
$envs["$name$processor"] = [
323338
'name' => $name,
339+
'inlined' => false,
324340
'processor' => $processor,
325341
'default_available' => $hasDefault,
326342
'default_value' => $defaultValue,

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,17 @@ protected function describeContainerEnvVars(array $envs, array $options = []): v
428428
if ($name === $env['name'] || false !== stripos($env['name'], $name)) {
429429
$matches = true;
430430
$options['output']->section('%env('.$env['processor'].':'.$env['name'].')%');
431-
$options['output']->table([], [
432-
['<info>Default value</>', $env['default_available'] ? $dump($env['default_value']) : 'n/a'],
433-
['<info>Real value</>', $env['runtime_available'] ? $dump($env['runtime_value']) : 'n/a'],
434-
['<info>Processed value</>', $env['default_available'] || $env['runtime_available'] ? $dump($env['processed_value']) : 'n/a'],
435-
]);
431+
if (!$env['inlined']) {
432+
$options['output']->table([], [
433+
['<info>Default value</>', $env['default_available'] ? $dump($env['default_value']) : 'n/a'],
434+
['<info>Real value</>', $env['runtime_available'] ? $dump($env['runtime_value']) : 'n/a'],
435+
['<info>Processed value</>', $env['default_available'] || $env['runtime_available'] ? $dump($env['processed_value']) : 'n/a'],
436+
]);
437+
} else {
438+
$options['output']->table([], [
439+
['<info>Inlined value</>', $dump($env['processed_value'])],
440+
]);
441+
}
436442
}
437443
}
438444

@@ -454,12 +460,13 @@ protected function describeContainerEnvVars(array $envs, array $options = []): v
454460
$rows = [];
455461
$missing = [];
456462
foreach ($envs as $env) {
457-
if (isset($rows[$env['name']])) {
463+
$name = !$env['inlined'] ? $env['name'] : "{$env['name']} (inlined)";
464+
if (isset($rows[$name])) {
458465
continue;
459466
}
460467

461-
$rows[$env['name']] = [
462-
$env['name'],
468+
$rows[$name] = [
469+
$name,
463470
$env['default_available'] ? $dump($env['default_value']) : 'n/a',
464471
$env['runtime_available'] ? $dump($env['runtime_value']) : 'n/a',
465472
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
/**
9+
* Stores environment variables used during build, for debugging purposes
10+
*
11+
* @author Valtteri Rauhala <valtzu@gmail.com>
12+
*/
13+
class StoreInlinedEnvVarsCompilerPass implements CompilerPassInterface
14+
{
15+
public function process(ContainerBuilder $container)
16+
{
17+
$buildTimeEnvVars = [];
18+
foreach ($container->getEnvCounters() as $env => $count) {
19+
if ($count > 0) {
20+
$buildTimeEnvVars[$env] = $container->resolveEnvPlaceholders("%env($env)%", true);
21+
}
22+
}
23+
24+
$container->setParameter('.debug.container.build_time_env_vars', $buildTimeEnvVars);
25+
}
26+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RemoveUnusedSessionMarshallingHandlerPass;
2424
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
2525
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass;
26+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\StoreInlinedEnvVarsCompilerPass;
2627
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
2728
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\WorkflowGuardListenerPass;
2829
use Symfony\Component\Cache\Adapter\ApcuAdapter;
@@ -175,6 +176,7 @@ public function build(ContainerBuilder $container)
175176
$container->addCompilerPass(new EnableLoggerDebugModePass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -33);
176177
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);
177178
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
179+
$container->addCompilerPass(new StoreInlinedEnvVarsCompilerPass(), PassConfig::TYPE_BEFORE_REMOVING, -254);
178180
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
179181
$container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
180182
}

0 commit comments

Comments
 (0)