Skip to content

Commit 648262d

Browse files
committed
[FrameworkBundle] Fix lint:container --resolve-env-vars
1 parent 4c63be4 commit 648262d

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

Command/ContainerLintCommand.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
2525
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2626
use Symfony\Component\DependencyInjection\Compiler\ResolveFactoryClassPass;
27+
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
2728
use Symfony\Component\DependencyInjection\Container;
2829
use Symfony\Component\DependencyInjection\ContainerBuilder;
2930
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -49,8 +50,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4950
$io = new SymfonyStyle($input, $output);
5051
$errorIo = $io->getErrorStyle();
5152

53+
$resolveEnvVars = $input->getOption('resolve-env-vars');
54+
5255
try {
53-
$container = $this->getContainerBuilder();
56+
$container = $this->getContainerBuilder($resolveEnvVars);
5457
} catch (RuntimeException $e) {
5558
$errorIo->error($e->getMessage());
5659

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

6265
try {
63-
$container->compile((bool) $input->getOption('resolve-env-vars'));
66+
$container->compile($resolveEnvVars);
6467
} catch (InvalidArgumentException $e) {
6568
$errorIo->error($e->getMessage());
6669

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

75-
private function getContainerBuilder(): ContainerBuilder
78+
private function getContainerBuilder(bool $resolveEnvVars): ContainerBuilder
7679
{
7780
if (isset($this->container)) {
7881
return $this->container;
@@ -99,16 +102,22 @@ private function getContainerBuilder(): ContainerBuilder
99102

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

102-
$refl = new \ReflectionProperty($parameterBag, 'resolved');
103-
$refl->setValue($parameterBag, true);
105+
if ($resolveEnvVars) {
106+
$container->getCompilerPassConfig()->setOptimizationPasses([new ResolveParameterPlaceHoldersPass(), new ResolveFactoryClassPass()]);
107+
} else {
108+
$refl = new \ReflectionProperty($parameterBag, 'resolved');
109+
$refl->setValue($parameterBag, true);
110+
111+
$container->getCompilerPassConfig()->setOptimizationPasses([new ResolveFactoryClassPass()]);
112+
}
104113

105114
$container->getCompilerPassConfig()->setBeforeOptimizationPasses([]);
106-
$container->getCompilerPassConfig()->setOptimizationPasses([new ResolveFactoryClassPass()]);
107115
$container->getCompilerPassConfig()->setBeforeRemovingPasses([]);
108116
}
109117

110118
$container->setParameter('container.build_hash', 'lint_container');
111119
$container->setParameter('container.build_id', 'lint_container');
120+
$container->setParameter('container.runtime_mode', 'web=0');
112121

113122
$container->addCompilerPass(new CheckAliasValidityPass(), PassConfig::TYPE_BEFORE_REMOVING, -100);
114123
$container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100);

Tests/Functional/ContainerLintCommandTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,28 @@ class ContainerLintCommandTest extends AbstractWebTestCase
2424
/**
2525
* @dataProvider containerLintProvider
2626
*/
27-
public function testLintContainer(string $configFile, string $expectedOutput)
27+
public function testLintContainer(string $configFile, bool $resolveEnvVars, int $expectedExitCode, string $expectedOutput)
2828
{
2929
$kernel = static::createKernel([
30-
'test_case' => 'ContainerDebug',
30+
'test_case' => 'ContainerLint',
3131
'root_config' => $configFile,
3232
'debug' => true,
3333
]);
3434
$this->application = new Application($kernel);
3535

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

39-
$this->assertSame(0, $exitCode);
39+
$this->assertSame($expectedExitCode, $exitCode);
4040
$this->assertStringContainsString($expectedOutput, $tester->getDisplay());
4141
}
4242

4343
public static function containerLintProvider(): array
4444
{
4545
return [
46-
'default container' => ['config.yml', 'The container was linted successfully'],
47-
'missing dump file' => ['no_dump.yml', 'The container was linted successfully'],
46+
['escaped_percent.yml', false, 0, 'The container was linted successfully'],
47+
['missing_env_var.yml', false, 0, 'The container was linted successfully'],
48+
['missing_env_var.yml', true, 1, 'Environment variable not found: "BAR"'],
4849
];
4950
}
5051

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
15+
return [
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
parameters:
5+
percent: '%%foo%%'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
parameters:
5+
foo: '%env(BAR)%'

0 commit comments

Comments
 (0)