8000 [HttpKernel] add "kernel.mode" for build-time env, keep "kernel.envir… · symfony/symfony@9938887 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9938887

Browse files
[HttpKernel] add "kernel.mode" for build-time env, keep "kernel.environment" for the runtime env
1 parent 367aa1d commit 9938887

36 files changed

+186
-68
lines changed

UPGRADE-5.2.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ DependencyInjection
66

77
* Deprecated `Definition::setPrivate()` and `Alias::setPrivate()`, use `setPublic()` instead
88

9+
HttpKernel
10+
---------- 10000
11+
12+
* Deprecated the `Kernel::$environment` property, use `Kernel::$mode` instead
13+
* Deprecated the `KernelInterface::getEnvironment()` method, use `KernelInterface::getMode()` instead
14+
* Deprecated the `ConfigDataCollector::getEnv()` method, use `ConfigDataCollector::getMode()` instead
15+
916
Mime
1017
----
1118

UPGRADE-6.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ HttpKernel
6868

6969
* Made `WarmableInterface::warmUp()` return a list of classes or files to preload on PHP 7.4+
7070
* Removed support for `service:action` syntax to reference controllers. Use `serviceOrFqcn::method` instead.
71+
* Removed the `Kernel::$environment` property, use `Kernel::$mode` instead
72+
* Removed the `KernelInterface::getEnvironment()` method, use `KernelInterface::getMode()` instead
73+
* Removed the `ConfigDataCollector::getEnv()` method, use `ConfigDataCollector::getMode()` instead
7174

7275
Inflector
7376
---------

src/Symfony/Bridge/Twig/AppVariable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AppVariable
2727
private $tokenStorage;
2828
private $requestStack;
2929
private $environment;
30+
private $mode;
3031
private $debug;
3132

3233
public function setTokenStorage(TokenStorageInterface $tokenStorage)
@@ -44,6 +45,11 @@ public function setEnvironment(string $environment)
4445
$this->environment = $environment;
4546
}
4647

48+
public function setMode(string $mode): void
49+
{
50+
$this->mode = $mode;
51+
}
52+
4753
public function setDebug(bool $debug)
4854
{
4955
$this->debug = $debug;
@@ -130,6 +136,18 @@ public function getEnvironment()
130136
return $this->environment;
131137
}
132138

139+
/**
140+
* Returns the current app mode name (e.g 'dev').
141+
*/
142+
public function getMode(): string
143+
{
144+
if (null === $this->mode) {
145+
throw new \RuntimeException('The "app.mode" variable is not available.');
146+
}
147+
148+
return $this->mode;
149+
}
150+
133151
/**
134152
* Returns the current app debug mode.
135153
*

src/Symfony/Bridge/Twig/Tests/AppVariableTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public function testEnvironment()
4545
$this->assertEquals('dev', $this->appVariable->getEnvironment());
4646
}
4747

48+
public function testMode()
49+
{
50+
$this->appVariable->setMode('dev');
51+
52+
$this->assertEquals('dev', $this->appVariable->getMode());
53+
}
54+
4855
/**
4956
* @runInSeparateProcess
5057
*/
@@ -120,6 +127,12 @@ public function testEnvironmentNotSet()
120127
$this->appVariable->getEnvironment();
121128
}
122129

130+
public function testModeNotSet()
131+
{
132+
$this->expectException('RuntimeException');
133+
$this->appVariable->getMode();
134+
}
135+
123136
public function testDebugNotSet()
124137
{
125138
$this->expectException('RuntimeException');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7272
['<info>Kernel</>'],
7373
new TableSeparator(),
7474
['Type', \get_class($kernel)],
75-
['Environment', $kernel->getEnvironment()],
75+
['Mode', $kernel->getMode()],
7676
['Debug', $kernel->isDebug() ? 'true' : 'false'],
7777
['Charset', $kernel->getCharset()],
7878
['Cache directory', self::formatPath($kernel->getCacheDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getCacheDir()).'</>)'],

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearC 10000 ommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ protected function configure()
6060
])
6161
->setDescription('Clears the cache')
6262
->setHelp(<<<'EOF'
63-
The <info>%command.name%</info> command clears the application cache for a given environment
63+
The <info>%command.name%</info> command clears the application cache for a given mode
6464
and debug mode:
6565
66-
<info>php %command.full_name% --env=dev</info>
67-
<info>php %command.full_name% --env=prod --no-debug</info>
66+
<info>php %command.full_name% --mode=dev</info>
67+
<info>php %command.full_name% --mode=prod --no-debug</info>
6868
EOF
6969
)
7070
;
@@ -89,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8989
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realCacheDir));
9090
}
9191

92-
$io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
92+
$io->comment(sprintf('Clearing the cache for the <info>%s</info> mode with debug <info>%s</info>', $kernel->getMode(), var_export($kernel->isDebug(), true)));
9393
$this->cacheClearer->clear($realCacheDir);
9494

9595
// The current event dispatcher is stale, let's not use it anymore
@@ -179,7 +179,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
179179
$io->comment('Finished');
180180
}
181181

182-
$io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully cleared.', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
182+
$io->success(sprintf('Cache for the "%s" mode (debug=%s) was successfully cleared.', $kernel->getMode(), var_export($kernel->isDebug(), true)));
183183

184184
return 0;
185185
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7272
$io = new SymfonyStyle($input, $output);
7373

7474
$kernel = $this->getApplication()->getKernel();
75-
$io->comment(sprintf('Warming up the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
75+
$io->comment(sprintf('Warming up the cache for the <info>%s</info> mode with debug <info>%s</info>', $kernel->getMode(), var_export($kernel->isDebug(), true)));
7676

7777
if (!$input->getOption('no-optional-warmers')) {
7878
$this->cacheWarmer->enableOptionalWarmers();
@@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8484
Preloader::append($preloadFile, $preload);
8585
}
8686

87-
$io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully warmed.', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
87+
$io->success(sprintf('Cache for the "%s" mode (debug=%s) was successfully warmed.', $kernel->getMode(), var_export($kernel->isDebug(), true)));
8888

8989
return 0;
9090
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function configure()
8585
8686
Display a specific environment variable by specifying its name with the <info>--env-var</info> option:
8787
88-
<info>php %command.full_name% --env-var=APP_ENV</info>
88+
<info>php %command.full_name% --env-var=APP_MODE</info>
8989
9090
Use the --tags option to display tagged <comment>public</comment> services grouped by tag:
9191

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function __construct(KernelInterface $kernel)
4040
parent::__construct('Symfony', Kernel::VERSION);
4141

4242
$inputDefinition = $this->getDefinition();
43-
$inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getEnvironment()));
43+
$inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getMode()));
44+
$inputDefinition->addOption(new InputOption('--mode', null, InputOption::VALUE_REQUIRED, 'The Mode name.', $kernel->getMode()));
4445
$inputDefinition->addOption(new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'));
4546
}
4647

@@ -147,7 +148,7 @@ public function all($namespace = null)
147148
*/
148149
public function getLongVersion()
149150
{
150-
return parent::getLongVersion().sprintf(' (env: <comment>%s</>, debug: <comment>%s</>)', $this->kernel->getEnvironment(), $this->kernel->isDebug() ? 'true' : 'false');
151+
return parent::getLongVersion().sprintf(' (mode: <comment>%s</>, debug: <comment>%s</>)', $this->kernel->getMode(), $this->kernel->isDebug() ? 'true' : 'false');
151152
}
152153

153154
public function add(Command $command)

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ trait MicroKernelTrait
6969
public function getCacheDir(): string
7070
{
7171
if (isset($_SERVER['APP_CACHE_DIR'])) {
72-
return $_SERVER['APP_CACHE_DIR'].'/'.$this->environment;
72+
return $_SERVER['APP_CACHE_DIR'].'/'.$this->mode;
7373
}
7474

7575
return parent::getCacheDir();
@@ -90,7 +90,7 @@ public function registerBundles(): iterable
9090
{
9191
$contents = require $this->getProjectDir().'/config/bundles.php';
9292
foreach ($contents as $class => $envs) {
93-
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
93+
if ($envs[$this->mode] ?? $envs['all'] ?? false) {
9494
yield new $class();
9595
}
9696
}

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ protected static function createKernel(array $options = [])
104104

105105
if (isset($options['environment'])) {
106106
$env = $options['environment'];
107+
} elseif (isset($_ENV['APP_MODE'])) {
108+
$env = $_ENV['APP_MODE'];
109+
} elseif (isset($_SERVER['APP_MODE'])) {
110+
$env = $_SERVER['APP_MODE'];
107111
} elseif (isset($_ENV['APP_ENV'])) {
108112
$env = $_ENV['APP_ENV'];
109113
} elseif (isset($_SERVER['APP_ENV'])) {

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolDeleteCommandTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1919
use Symfony\Component\Console\Tester\CommandTester;
2020
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
21-
use Symfony\Component\HttpKernel\KernelInterface;
2221

2322
class CachePoolDeleteCommandTest extends TestCase
2423
{

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePruneCommandTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\Cache\PruneableInterface;
1919
use Symfony\Component\Console\Tester\CommandTester;
2020
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
21-
use Symfony\Component\HttpKernel\KernelInterface;
2221

2322
class CachePruneCommandTest extends TestCase
2423
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
13+
14+
use Symfony\Component\HttpKernel\KernelInterface as BaseKernelInterface;
15+
16+
interface KernelInterface extends BaseKernelInterface
17+
{
18+
public function getMode(): string;
19+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterMatchCommandTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand;
1717
use Symfony\Bundle\FrameworkBundle\Console\Application;
1818
use Symfony\Component\Console\Tester\CommandTester;
19-
use Symfony\Component\HttpKernel\KernelInterface;
2019
use Symfony\Component\Routing\RequestContext;
2120
use Symfony\Component\Routing\Route;
2221
use Symfony\Component\Routing\RouteCollection;

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testDebugCustomDirectory()
9292
{
9393
$this->fs->mkdir($this->translationDir.'/customDir/translations');
9494
$this->fs->mkdir($this->translationDir.'/customDir/templates');
95-
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
95+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
9696
$kernel->expects($this->once())
9797
->method('getBundle')
9898
->with($this->equalTo($this->translationDir.'/customDir'))
@@ -111,7 +111,7 @@ public function testDebugCustomDirectory()
111111
public function testDebugInvalidDirectory()
112112
{
113113
$this->expectException('InvalidArgumentException');
114-
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
114+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
115115
$kernel->expects($this->once())
116116
->method('getBundle')
117117
->with($this->equalTo('dir'))
@@ -170,7 +170,7 @@ function ($path, $catalogue) use ($loadedMessages) {
170170
['foo', $this->getBundle($this->translationDir)],
171171
['test', $this->getBundle('test')],
172172
];
173-
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
173+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
174174
$kernel
175175
->expects($this->any())
176176
->method('getBundle')

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\Console\Tester\CommandTester;
1818
use Symfony\Component\DependencyInjection\Container;
1919
use Symfony\Component\Filesystem\Filesystem;
20-
use Symfony\Component\HttpKernel;
2120

2221
class TranslationUpdateCommandTest extends TestCase
2322
{
@@ -135,7 +134,7 @@ protected function tearDown(): void
135134
/**
136135
* @return CommandTester
137136
*/
138-
private function createCommandTester($extractedMessages = [], $loadedMessages = [], HttpKernel\KernelInterface $kernel = null, array $transPaths = [], array $viewsPaths = [])
137+
private function createCommandTester($extractedMessages = [], $loadedMessages = [], KernelInterface $kernel = null, array $transPaths = [], array $viewsPaths = [])
139138
{
140139
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
141140
->disableOriginalConstructor()
@@ -181,7 +180,7 @@ function ($path, $catalogue) use ($loadedMessages) {
181180
['foo', $this->getBundle($this->translationDir)],
182181
['test', $this->getBundle('test')],
183182
];
184-
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
183+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
185184
$kernel
186185
->expects($this->any())
187186
->method('getBundle')

src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\Console\Input\InputDefinition;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Tester\CommandTester;
22-
use Symfony\Component\HttpKernel\KernelInterface;
2322

2423
/**
2524
* Tests the part of the XliffLintCommand managed by the FrameworkBundle. The

src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\Console\Input\InputDefinition;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Tester\CommandTester;
22-
use Symfony\Component\HttpKernel\KernelInterface;
2322

2423
/**
2524
* Tests the YamlLintCommand.

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Console\Application;
1515
use Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber;
16+
use Symfony\Bundle\FrameworkBundle\Tests\Command\KernelInterface;
1617
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1718
use Symfony\Component\Console\Command\Command;
1819
use Symfony\Component\Console\Event\ConsoleErrorEvent;
@@ -24,7 +25,6 @@
2425
use Symfony\Component\Console\Tester\ApplicationTester;
2526
use Symfony\Component\DependencyInjection\ContainerBuilder;
2627
use Symfony\Component\EventDispatcher\EventDispatcher;
27-
use Symfony\Component\HttpKernel\KernelInterface;
2828

2929
class ApplicationTest extends TestCase
3030
{
@@ -264,7 +264,7 @@ private function getKernel(array $bundles, $useDispatcher = false)
264264
->willReturnOnConsecutiveCalls([], [])
265265
;
266266

267-
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
267+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
268268
$kernel->expects($this->once())->method('boot');
269269
$kernel
270270
->expects($this->any())

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,7 @@ protected function createContainer(array $data = [])
14771477
'kernel.project_dir' => __DIR__,
14781478
'kernel.debug' => false,
14791479
'kernel.environment' => 'test',
1480+
'kernel.mode' => 'test',
14801481
'kernel.name' => 'kernel',
14811482
'kernel.container_class' => 'testContainer',
14821483
'container.build_hash' => 'Abc1234',

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/AppKernel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getProjectDir(): string
6464

6565
public function getCacheDir(): string
6666
{
67-
return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment;
67+
return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->mode;
6868
}
6969

7070
public function getLogDir(): string
@@ -84,12 +84,12 @@ protected function build(ContainerBuilder $container)
8484

8585
public function __sleep(): array
8686
{
87-
return ['varDir', 'testCase', 'rootConfig', 'environment', 'debug'];
87+
return ['varDir', 'testCase', 'rootConfig', 'mode', 'debug'];
8888
}
8989

9090
public function __wakeup()
9191
{
92-
$this->__construct($this->varDir, $this->testCase, $this->rootConfig, $this->environment, $this->debug);
92+
$this->__construct($this->varDir, $this->testCase, $this->rootConfig, $this->mode, $this->debug);
9393
}
9494

9595
protected function getKernelParameters(): array

0 commit comments

Comments
 (0)
0