Skip to content
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 @@ -24,6 +24,7 @@
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Compiler\ResolveFactoryClassPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Expand All @@ -49,8 +50,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);
$errorIo = $io->getErrorStyle();

$resolveEnvVars = $input->getOption('resolve-env-vars');

try {
$container = $this->getContainerBuilder();
$container = $this->getContainerBuilder($resolveEnvVars);
} catch (RuntimeException $e) {
$errorIo->error($e->getMessage());

Expand All @@ -60,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$container->setParameter('container.build_time', time());

try {
$container->compile((bool) $input->getOption('resolve-env-vars'));
$container->compile($resolveEnvVars);
} catch (InvalidArgumentException $e) {
$errorIo->error($e->getMessage());

Expand All @@ -72,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

private function getContainerBuilder(): ContainerBuilder
private function getContainerBuilder(bool $resolveEnvVars): ContainerBuilder
{
if (isset($this->container)) {
return $this->container;
Expand All @@ -99,16 +102,22 @@ private function getContainerBuilder(): ContainerBuilder

(new XmlFileLoader($container = new ContainerBuilder($parameterBag = new EnvPlaceholderParameterBag()), new FileLocator()))->load($kernelContainer->getParameter('debug.container.dump'));

$refl = new \ReflectionProperty($parameterBag, 'resolved');
$refl->setValue($parameterBag, true);
if ($resolveEnvVars) {
$container->getCompilerPassConfig()->setOptimizationPasses([new ResolveParameterPlaceHoldersPass(), new ResolveFactoryClassPass()]);
} else {
$refl = new \ReflectionProperty($parameterBag, 'resolved');
$refl->setValue($parameterBag, true);

$container->getCompilerPassConfig()->setOptimizationPasses([new ResolveFactoryClassPass()]);
}

$container->getCompilerPassConfig()->setBeforeOptimizationPasses([]);
$container->getCompilerPassConfig()->setOptimizationPasses([new ResolveFactoryClassPass()]);
$container->getCompilerPassConfig()->setBeforeRemovingPasses([]);
}

$container->setParameter('container.build_hash', 'lint_container');
$container->setParameter('container.build_id', 'lint_container');
$container->setParameter('container.runtime_mode', 'web=0');

$container->addCompilerPass(new CheckAliasValidityPass(), PassConfig::TYPE_BEFORE_REMOVING, -100);
$container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,28 @@ class ContainerLintCommandTest extends AbstractWebTestCase
/**
* @dataProvider containerLintProvider
*/
public function testLintContainer(string $configFile, string $expectedOutput)
public function testLintContainer(string $configFile, bool $resolveEnvVars, int $expectedExitCode, string $expectedOutput)
{
$kernel = static::createKernel([
'test_case' => 'ContainerDebug',
'test_case' => 'ContainerLint',
'root_config' => $configFile,
'debug' => true,
]);
$this->application = new Application($kernel);

$tester = $this->createCommandTester();
$exitCode = $tester->execute([]);
$exitCode = $tester->execute(['--resolve-env-vars' => $resolveEnvVars]);

$this->assertSame(0, $exitCode);
$this->assertSame($expectedExitCode, $exitCode);
$this->assertStringContainsString($expectedOutput, $tester->getDisplay());
}

public static function containerLintProvider(): array
{
return [
'default container' => ['config.yml', 'The container was linted successfully'],
'missing dump file' => ['no_dump.yml', 'The container was linted successfully'],
['escaped_percent.yml', false, 0, 'The container was linted successfully'],
Copy link
Contributor Author

@MatTheCat MatTheCat Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot lint escaped_percent.yml with --resolve-env-vars yet.

['missing_env_var.yml', false, 0, 'The container was linted successfully'],
['missing_env_var.yml', true, 1, 'Environment variable not found: "BAR"'],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;

return [
new FrameworkBundle(),
new TestBundle(),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
imports:
- { resource: ../config/default.yml }

parameters:
percent: '%%foo%%'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
imports:
- { resource: ../config/default.yml }

parameters:
foo: '%env(BAR)%'
Loading