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

Skip to content

Commit 6888c01

Browse files
[HttpKernel] add "kernel.mode" for build-time env, keep "kernel.environment" for the runtime env
1 parent ba4d57b commit 6888c01

File tree

27 files changed

+158
-53
lines changed

27 files changed

+158
-53
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+
----------
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/CacheClearCommand.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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function __construct(KernelInterface $kernel)
4141

4242
$inputDefinition = $this->getDefinition();
4343
$inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getEnvironment()));
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/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: 2 additions & 2 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
@@ -89,7 +89,7 @@ public function __sleep(): array
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

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
*/
2323
class AppKernel extends Kernel
2424
{
25+
protected $mode;
26+
2527
private $varDir;
2628
private $testCase;
2729
private $rootConfig;
2830
private $authenticatorManagerEnabled;
2931

30-
public function __construct($varDir, $testCase, $rootConfig, $environment, $debug, $authenticatorManagerEnabled = false)
32+
public function __construct($varDir, $testCase, $rootConfig, $mode, $debug, $authenticatorManagerEnabled = false)
3133
{
3234
if (!is_dir(__DIR__.'/'.$testCase)) {
3335
throw new \InvalidArgumentException(sprintf('The test case "%s" does not exist.', $testCase));
@@ -42,7 +44,8 @@ public function __construct($varDir, $testCase, $rootConfig, $environment, $debu
4244
$this->rootConfig = $rootConfig;
4345
$this->authenticatorManagerEnabled = $authenticatorManagerEnabled;
4446

45-
parent::__construct($environment, $debug);
47+
$this->mode = $mode;
48+
parent::__construct($mode, $debug);
4649
}
4750

4851
/**
@@ -69,7 +72,7 @@ public function getProjectDir(): string
6972

7073
public function getCacheDir(): string
7174
{
72-
return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment;
75+
return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->mode;
7376
}
7477

7578
public function getLogDir(): string
@@ -92,7 +95,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
9295

9396
public function serialize()
9497
{
95-
return serialize([$this->varDir, $this->testCase, $this->rootConfig, $this->getEnvironment(), $this->isDebug()]);
98+
return serialize([$this->varDir, $this->testCase, $this->rootConfig, $this->mode, $this->isDebug()]);
9699
}
97100

98101
public function unserialize($str)

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
5.2.0
55
-----
66

7+
* the `app.env` variable is now runtime-dependent when the `APP_RUNTIME_ENV` env var is defined
8+
* added the `app.mode` variable: it's the build-time equivalent of `app.env`
79
* deprecated the public `twig` service to private
810

911
5.0.0

src/Symfony/Bundle/TwigBundle/Resources/config/twig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070

7171
->set('twig.app_variable', AppVariable::class)
7272
->call('setEnvironment', [param('kernel.environment')])
73+
->call('setMode', [param('kernel.mode')])
7374
->call('setDebug', [param('kernel.debug')])
7475
->call('setTokenStorage', [service('security.token_storage')->ignoreOnInvalid()])
7576
->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()])

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ public function testRuntimeLoader()
246246
$container->registerExtension(new TwigExtension());
247247
$container->loadFromExtension('twig');
248248
$container->setParameter('kernel.environment', 'test');
249+
$container->setParameter('kernel.mode', 'test');
249250
$container->setParameter('debug.file_link_format', 'test');
250251
$container->setParameter('foo', 'FooClass');
251252
$container->register('http_kernel', 'FooClass');

src/Symfony/Bundle/TwigBundle/Tests/Functional/EmptyAppTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
6969

7070
public function getCacheDir(): string
7171
{
72-
return sys_get_temp_dir().'/'.Kernel::VERSION.'/EmptyAppKernel/cache/'.$this->environment;
72+
return sys_get_temp_dir().'/'.Kernel::VERSION.'/EmptyAppKernel/cache/'.$this->mode;
7373
}
7474

7575
public function getLogDir(): string

src/Symfony/Bundle/TwigBundle/Tests/Functional/NoTemplatingEntryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
7777

7878
public function getCacheDir(): string
7979
{
80-
return sys_get_temp_dir().'/'.Kernel::VERSION.'/NoTemplatingEntryKernel/cache/'.$this->environment;
80+
return sys_get_temp_dir().'/'.Kernel::VERSION.'/NoTemplatingEntryKernel/cache/'.$this->mode;
8181
}
8282

8383
public function getLogDir(): string

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/config": "^4.4|^5.0",
2121
"symfony/twig-bridge": "^5.0",
2222
"symfony/http-foundation": "^4.4|^5.0",
23-
"symfony/http-kernel": "^5.0",
23+
"symfony/http-kernel": "^5.2",
2424
"symfony/polyfill-ctype": "~1.8",
2525
"twig/twig": "^2.10|^3.0"
2626
},

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/config.html.twig

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@
3838
</span>
3939
</div>
4040

41-
{% if 'n/a' is not same as(collector.env) %}
41+
{% if collector.mode is defined %}
42+
{% if 'n/a' is not same as(collector.mode) %}
43+
<div class="sf-toolbar-info-piece">
44+
<b>Mode</b>
45+
<span>{{ collector.mode }}</span>
46+
</div>
47+
{% endif %}
48+
{% elseif 'n/a' is not same as(collector.env) %}
4249
<div class="sf-toolbar-info-piece">
4350
<b>Environment</b>
4451
<span>{{ collector.env }}</span>
@@ -116,7 +123,14 @@
116123
<span class="label">Symfony version</span>
117124
</div>
118125

119-
{% if 'n/a' != collector.env %}
126+
{% if collector.mode is defined %}
127+
{% if 'n/a' is not same as(collector.mode) %}
128+
<div class="metric">
129+
<span class="value">{{ collector.mode }}</span>
130+
<span class="label">Mode</span>
131+
</div>
132+
{% endif %}
133+
{% elseif 'n/a' != collector.env %}
120134
<div class="metric">
121135
<span class="value">{{ collector.env }}</span>
122136
<span class="label">Environment</span>

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ CHANGELOG
88
* made the public `http_cache` service handle requests when available
99
* allowed enabling trusted hosts and proxies using new `kernel.trusted_hosts`,
1010
`kernel.trusted_proxies` and `kernel.trusted_headers` parameters
11+
* made parameter `kernel.environment` runtime-dependent when the `APP_RUNTIME_ENV` env var is set
12+
* added parameter `kernel.mode`: it's the build-time equivalent of `kernel.environment`
13+
* deprecated the `Kernel::$environment` property, use `Kernel::$mode` instead
14+
* deprecated the `KernelInterface::getEnvironment()` method, use `KernelInterface::getMode()` instead
15+
* deprecated the `ConfigDataCollector::getEnv()` method, use `ConfigDataCollector::getMode()` instead
1116

1217
5.1.0
1318
-----

0 commit comments

Comments
 (0)
0