8000 [HttpKernel] add "kernel.mode" for build-time env, keep "kernel.environment" for the runtime env by nicolas-grekas · Pull Request #37584 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] add "kernel.mode" for build-time env, keep "kernel.environment" for the runtime env #37584

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
[HttpKernel] add "kernel.mode" for build-time env, keep "kernel.envir…
…onment" for the runtime env
  • Loading branch information
nicolas-grekas committed Jul 15, 2020
commit 99388875d28d40ce9854de026ddc289c0a63af90
7 changes: 7 additions & 0 deletions UPGRADE-5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ DependencyInjection

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

HttpKernel
----------

* Deprecated the `Kernel::$environment` property, use `Kernel::$mode` instead
* Deprecated the `KernelInterface::getEnvironment()` method, use `KernelInterface::getMode()` instead
* Deprecated the `ConfigDataCollector::getEnv()` method, use `ConfigDataCollector::getMode()` instead

Mime
----

Expand Down
3 changes: 3 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ HttpKernel

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

Inflector
---------
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Bridge/Twig/AppVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AppVariable
private $tokenStorage;
private $requestStack;
private $environment;
private $mode;
private $debug;

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

public function setMode(string $mode): void
{
$this->mode = $mode;
}

public function setDebug(bool $debug)
{
$this->debug = $debug;
Expand Down Expand Up @@ -130,6 +136,18 @@ public function getEnvironment()
return $this->environment;
}

/**
* Returns the current app mode name (e.g 'dev').
*/
public function getMode(): string
{
if (null === $this->mode) {
throw new \RuntimeException('The "app.mode" variable is not available.');
}

return $this->mode;
}

/**
* Returns the current app debug mode.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public function testEnvironment()
$this->assertEquals('dev', $this->appVariable->getEnvironment());
}

public function testMode()
{
$this->appVariable->setMode('dev');

$this->assertEquals('dev', $this->appVariable->getMode());
}

/**
* @runInSeparateProcess
*/
Expand Down Expand Up @@ -120,6 +127,12 @@ public function testEnvironmentNotSet()
$this->appVariable->getEnvironment();
}

public function testModeNotSet()
{
$this->expectException('RuntimeException');
$this->appVariable->getMode();
}

public function testDebugNotSet()
{
$this->expectException('RuntimeException');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
['<info>Kernel</>'],
new TableSeparator(),
['Type', \get_class($kernel)],
['Environment', $kernel->getEnvironment()],
['Mode', $kernel->getMode()],
['Debug', $kernel->isDebug() ? 'true' : 'false'],
['Charset', $kernel->getCharset()],
['Cache directory', self::formatPath($kernel->getCacheDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getCacheDir()).'</>)'],
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ protected function configure()
])
->setDescription('Clears the cache')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command clears the application cache for a given environment
The <info>%command.name%</info> command clears the application cache for a given mode
and debug mode:

<info>php %command.full_name% --env=dev</info>
<info>php %command.full_name% --env=prod --no-debug</info>
<info>php %command.full_name% --mode=dev</info>
<info>php %command.full_name% --mode=prod --no-debug</info>
EOF
)
;
Expand All @@ -89,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realCacheDir));
}

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

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

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

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);

$kernel = $this->getApplication()->getKernel();
$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)));
$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)));

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

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

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function configure()

Display a specific environment variable by specifying its name with the <info>--env-var</info> option:

<info>php %command.full_name% --env-var=APP_ENV</info>
<info>php %command.full_name% --env-var=APP_MODE</info>

Use the --tags option to display tagged <comment>public</comment> services grouped by tag:

Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function __construct(KernelInterface $kernel)
parent::__construct('Symfony', Kernel::VERSION);

$inputDefinition = $this->getDefinition();
$inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getEnvironment()));
$inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getMode()));
$inputDefinition->addOption(new InputOption('--mode', null, InputOption::VALUE_REQUIRED, 'The Mode name.', $kernel->getMode()));
Copy link
Member

Choose a reason for hiding this comment

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

this effectively turns --mode into a reserve option name. So there is a BC impact for that change

$inputDefinition->addOption(new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'));
}

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

public function add(Command $command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ trait MicroKernelTrait
public function getCacheDir(): string
{
if (isset($_SERVER['APP_CACHE_DIR'])) {
return $_SERVER['APP_CACHE_DIR'].'/'.$this->environment;
return $_SERVER['APP_CACHE_DIR'].'/'.$this->mode;
}

return parent::getCacheDir();
Expand All @@ -90,7 +90,7 @@ public function registerBundles(): iterable
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
if ($envs[$this->mode] ?? $envs['all'] ?? false) {
yield new $class();
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ protected static function createKernel(array $options = [])

if (isset($options['environment'])) {
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we rename the option to mode as well ?

$env = $options['environment'];
} elseif (isset($_ENV['APP_MODE'])) {
$env = $_ENV['APP_MODE'];
} elseif (isset($_SERVER['APP_MODE'])) {
$env = $_SERVER['APP_MODE'];
} elseif (isset($_ENV['APP_ENV'])) {
$env = $_ENV['APP_ENV'];
} elseif (isset($_SERVER['APP_ENV'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
use Symfony\Component\HttpKernel\KernelInterface;

class CachePoolDeleteCommandTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\HttpKernel\KernelInterface;

class CachePruneCommandTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Command;

use Symfony\Component\HttpKernel\KernelInterface as BaseKernelInterface;

interface KernelInterface extends BaseKernelInterface
{
public function getMode(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testDebugCustomDirectory()
{
$this->fs->mkdir($this->translationDir.'/customDir/translations');
$this->fs->mkdir($this->translationDir.'/customDir/templates');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
$kernel->expects($this->once())
->method('getBundle')
->with($this->equalTo($this->translationDir.'/customDir'))
Expand All @@ -111,7 +111,7 @@ public function testDebugCustomDirectory()
public function testDebugInvalidDirectory()
{
$this->expectException('InvalidArgumentException');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
$kernel->expects($this->once())
->method('getBundle')
->with($this->equalTo('dir'))
Expand Down Expand Up @@ -170,7 +170,7 @@ function ($path, $catalogue) use ($loadedMessages) {
['foo', $this->getBundle($this->translationDir)],
['test', $this->getBundle('test')],
];
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
$kernel
->expects($this->any())
->method('getBundle')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel;

class TranslationUpdateCommandTest extends TestCase
{
Expand Down Expand Up @@ -135,7 +134,7 @@ protected function tearDown(): void
/**
* @return CommandTester
*/
private function createCommandTester($extractedMessages = [], $loadedMessages = [], HttpKernel\KernelInterface $kernel = null, array $transPaths = [], array $viewsPaths = [])
private function createCommandTester($extractedMessages = [], $loadedMessages = [], KernelInterface $kernel = null, array $transPaths = [], array $viewsPaths = [])
{
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
->disableOriginalConstructor()
Expand Down Expand Up @@ -181,7 +180,7 @@ function ($path, $catalogue) use ($loadedMessages) {
['foo', $this->getBundle($this->translationDir)],
['test', $this->getBundle('test')],
];
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
$kernel
->expects($this->any())
->method('getBundle')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Tests the part of the XliffLintCommand managed by the FrameworkBundle. The
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Tests the YamlLintCommand.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber;
use Symfony\Bundle\FrameworkBundle\Tests\Command\KernelInterface;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
Expand All @@ -24,7 +25,6 @@
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\KernelInterface;

class ApplicationTest extends TestCase
{
Expand Down Expand Up @@ -264,7 +264,7 @@ private function getKernel(array $bundles, $useDispatcher = false)
->willReturnOnConsecutiveCalls([], [])
;

$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
$kernel->expects($this->once())->method('boot');
$kernel
->expects($this->any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,7 @@ protected function createContainer(array $data = [])
'kernel.project_dir' => __DIR__,
'kernel.debug' => false,
'kernel.environment' => 'test',
'kernel.mode' => 'test',
'kernel.name' => 'kernel',
'kernel.container_class' => 'testContainer',
'container.build_hash' => 'Abc1234',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getProjectDir(): string

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

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

public function __sleep(): array
{
return ['varDir', 'testCase', 'rootConfig', 'environment', 'debug'];
return ['varDir', 'testCase', 'rootConfig', 'mode', 'debug'];
}

public function __wakeup()
{
$this->__construct($this->varDir, $this->testCase, $this->rootConfig, $this->environment, $this->debug);
$this->__construct($this->varDir, $this->testCase, $this->rootConfig, $this->mode, $this->debug);
}

protected function getKernelParameters(): array
Expand Down
Loading
0