Skip to content

[FrameworkBundle] Add inlined env vars into debug:container output #49336

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 @@ -308,6 +308,21 @@ private function getContainerEnvVars(ContainerBuilder $container): array

$envs = [];

if ($container->hasParameter('.debug.container.build_time_env_vars')) {
foreach ($container->getParameter('.debug.container.build_time_env_vars') as $name => $value) {
$envs["{$name}inline"] = [
'name' => $name,
'inlined' => true,
'processor' => 'string',
'default_available' => false,
'default_value' => null,
'runtime_available' => true,
'runtime_value' => $value,
'processed_value' => $value, // Env var processors are not supported at the moment
];
}
}

foreach ($envVars as $env) {
$processor = 'string';
if (false !== $i = strrpos($name = $env, ':')) {
Expand All @@ -321,6 +336,7 @@ private function getContainerEnvVars(ContainerBuilder $container): array
$processedValue = ($hasRuntime = null !== $runtimeValue) || $hasDefault ? $getEnvReflection->invoke($container, $env) : null;
$envs["$name$processor"] = [
'name' => $name,
'inlined' => false,
'processor' => $processor,
'default_available' => $hasDefault,
'default_value' => $defaultValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,17 @@ protected function describeContainerEnvVars(array $envs, array $options = []): v
if ($name === $env['name'] || false !== stripos($env['name'], $name)) {
$matches = true;
$options['output']->section('%env('.$env['processor'].':'.$env['name'].')%');
$options['output']->table([], [
['<info>Default value</>', $env['default_available'] ? $dump($env['default_value']) : 'n/a'],
['<info>Real value</>', $env['runtime_available'] ? $dump($env['runtime_value']) : 'n/a'],
['<info>Processed value</>', $env['default_available'] || $env['runtime_available'] ? $dump($env['processed_value']) : 'n/a'],
]);
if (!$env['inlined']) {
$options['output']->table([], [
['<info>Default value</>', $env['default_available'] ? $dump($env['default_value']) : 'n/a'],
['<info>Real value</>', $env['runtime_available'] ? $dump($env['runtime_value']) : 'n/a'],
['<info>Processed value</>', $env['default_available'] || $env['runtime_available'] ? $dump($env['processed_value']) : 'n/a'],
]);
} else {
$options['output']->table([], [
['<info>Inlined value</>', $dump($env['processed_value'])],
]);
}
}
}

Expand All @@ -454,12 +460,13 @@ protected function describeContainerEnvVars(array $envs, array $options = []): v
$rows = [];
$missing = [];
foreach ($envs as $env) {
if (isset($rows[$env['name']])) {
$name = !$env['inlined'] ? $env['name'] : "{$env['name']} (inlined)";
if (isset($rows[$name])) {
continue;
}

$rows[$env['name']] = [
$env['name'],
$rows[$name] = [
$name,
$env['default_available'] ? $dump($env['default_value']) : 'n/a',
$env['runtime_available'] ? $dump($env['runtime_value']) : 'n/a',
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Stores environment variables used during build, for debugging purposes
*
* @author Valtteri Rauhala <valtzu@gmail.com>
*/
class StoreInlinedEnvVarsCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$buildTimeEnvVars = [];
foreach ($container->getEnvCounters() as $env => $count) {
if ($count > 0) {
$buildTimeEnvVars[$env] = $container->resolveEnvPlaceholders("%env($env)%", true);
}
}

$container->setParameter('.debug.container.build_time_env_vars', $buildTimeEnvVars);
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RemoveUnusedSessionMarshallingHandlerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\StoreInlinedEnvVarsCompilerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\WorkflowGuardListenerPass;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
Expand Down Expand Up @@ -175,6 +176,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new EnableLoggerDebugModePass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -33);
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new StoreInlinedEnvVarsCompilerPass(), PassConfig::TYPE_BEFORE_REMOVING, -254);
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
$container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
}
Expand Down