Skip to content

Commit

Permalink
feature #28809 [HttpKernel] Deprecate the Kernel name (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.2-dev branch.

Discussion
----------

[HttpKernel] Deprecate the Kernel name

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | yes <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #26904
| License       | MIT
| Doc PR        | n/a

<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
 - Bug fixes must be submitted against the lowest branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

Commits
-------

98ff750 [HttpKernel] deprecated the Kernel name
  • Loading branch information
fabpot committed Oct 15, 2018
2 parents d813148 + 98ff750 commit 5fda7e2
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 40 deletions.
Expand Up @@ -367,7 +367,7 @@ protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheD
} else {
$seed = '_'.$container->getParameter('kernel.root_dir');
}
$seed .= '.'.$container->getParameter('kernel.name').'.'.$container->getParameter('kernel.environment').'.'.$container->getParameter('kernel.debug');
$seed .= '.'.$container->getParameter('kernel.container_class').'.'.$container->getParameter('kernel.environment').'.'.$container->getParameter('kernel.debug');
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.ContainerBuilder::hash($seed);

$cacheDriver['namespace'] = $namespace;
Expand Down
Expand Up @@ -271,7 +271,7 @@ protected function createContainer(array $data = array())
'kernel.cache_dir' => __DIR__,
'kernel.debug' => false,
'kernel.environment' => 'test',
'kernel.name' => 'kernel',
'kernel.container_class' => 'kernel',
'kernel.root_dir' => __DIR__,
), $data)));
}
Expand Down
Expand Up @@ -71,7 +71,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
array('<info>Kernel</>'),
new TableSeparator(),
array('Type', \get_class($kernel)),
array('Name', $kernel->getName()),
array('Environment', $kernel->getEnvironment()),
array('Debug', $kernel->isDebug() ? 'true' : 'false'),
array('Charset', $kernel->getCharset()),
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Expand Up @@ -148,7 +148,7 @@ public function all($namespace = null)
*/
public function getLongVersion()
{
return parent::getLongVersion().sprintf(' (kernel: <comment>%s</>, env: <comment>%s</>, debug: <comment>%s</>)', $this->kernel->getName(), $this->kernel->getEnvironment(), $this->kernel->isDebug() ? 'true' : 'false');
return parent::getLongVersion().sprintf(' (env: <comment>%s</>, debug: <comment>%s</>)', $this->kernel->getEnvironment(), $this->kernel->isDebug() ? 'true' : 'false');
}

public function add(Command $command)
Expand Down
Expand Up @@ -30,7 +30,7 @@ public function testPoolRefsAreWeak()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');

Expand Down
Expand Up @@ -35,7 +35,7 @@ public function testNamespaceArgumentIsReplaced()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$adapter = new Definition();
Expand All @@ -57,7 +57,7 @@ public function testNamespaceArgumentIsNotReplacedIfArrayAdapterIsUsed()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.root_dir', 'foo');

$container->register('cache.adapter.array', ArrayAdapter::class)->addArgument(0);
Expand All @@ -75,7 +75,7 @@ public function testArgsAreReplaced()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('cache.prefix.seed', 'foo');
$cachePool = new Definition();
Expand All @@ -100,7 +100,7 @@ public function testWithNameAttribute()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('cache.prefix.seed', 'foo');
$cachePool = new Definition();
Expand All @@ -126,7 +126,7 @@ public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$adapter = new Definition();
Expand Down
Expand Up @@ -46,13 +46,9 @@ public function __construct($varDir, $testCase, $rootConfig, $environment, $debu
/**
* {@inheritdoc}
*/
public function getName()
public function getContainerClass()
{
if (null === $this->name) {
$this->name = parent::getName().substr(md5($this->rootConfig), -16);
}

return $this->name;
return parent::getContainerClass().substr(md5($this->rootConfig), -16);
}

public function registerBundles()
Expand Down
Expand Up @@ -50,13 +50,6 @@
</span>
</div>

{% if 'n/a' is not same as(collector.appname) %}
<div class="sf-toolbar-info-piece">
<b>Kernel name</b>
<span>{{ collector.appname }}</span>
</div>
{% endif %}

{% if 'n/a' is not same as(collector.env) %}
<div class="sf-toolbar-info-piece">
<b>Environment</b>
Expand Down
Expand Up @@ -52,7 +52,7 @@ public function process(ContainerBuilder $container)
} else {
$seed = '_'.$container->getParameter('kernel.root_dir');
}
$seed .= '.'.$container->getParameter('kernel.name').'.'.$container->getParameter('kernel.environment');
$seed .= '.'.$container->getParameter('kernel.container_class').'.'.$container->getParameter('kernel.environment');

$pools = array();
$clearers = array();
Expand Down
Expand Up @@ -27,7 +27,7 @@ public function testPoolRefsAreWeak()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');

Expand Down
Expand Up @@ -32,7 +32,7 @@ public function testNamespaceArgumentIsReplaced()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$adapter = new Definition();
Expand All @@ -54,7 +54,7 @@ public function testNamespaceArgumentIsNotReplacedIfArrayAdapterIsUsed()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.root_dir', 'foo');

$container->register('cache.adapter.array', ArrayAdapter::class)->addArgument(0);
Expand All @@ -72,7 +72,7 @@ public function testArgsAreReplaced()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('cache.prefix.seed', 'foo');
$cachePool = new Definition();
Expand All @@ -97,7 +97,7 @@ public function testWithNameAttribute()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('cache.prefix.seed', 'foo');
$cachePool = new Definition();
Expand All @@ -123,7 +123,7 @@ public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$adapter = new Definition();
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* deprecated `KernelInterface::getName()` and the `kernel.name` parameter

4.1.0
-----

Expand Down
Expand Up @@ -60,7 +60,7 @@ public function collect(Request $request, Response $response, \Exception $except
'token' => $response->headers->get('X-Debug-Token'),
'symfony_version' => Kernel::VERSION,
'symfony_state' => 'unknown',
'name' => isset($this->kernel) ? $this->kernel->getName() : 'n/a',
'name' => 'n/a',
'env' => isset($this->kernel) ? $this->kernel->getEnvironment() : 'n/a',
'debug' => isset($this->kernel) ? $this->kernel->isDebug() : 'n/a',
'php_version' => PHP_VERSION,
Expand Down Expand Up @@ -227,6 +227,8 @@ public function getPhpTimezone()
* Gets the application name.
*
* @return string The application name
*
* @deprecated since Symfony 4.2
*/
public function getAppName()
{
Expand Down
16 changes: 13 additions & 3 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -58,6 +58,9 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
protected $environment;
protected $debug;
protected $booted = false;
/**
* @deprecated since Symfony 4.2
*/
protected $name;
protected $startTime;

Expand All @@ -81,7 +84,7 @@ public function __construct(string $environment, bool $debug)
$this->environment = $environment;
$this->debug = $debug;
$this->rootDir = $this->getRootDir();
$this->name = $this->getName();
$this->name = $this->getName(false);
}

public function __clone()
Expand Down Expand Up @@ -271,8 +274,12 @@ public function locateResource($name, $dir = null, $first = true)
/**
* {@inheritdoc}
*/
public function getName()
public function getName(/* $triggerDeprecation = true */)
{
if (0 === \func_num_args() || func_get_arg(0)) {
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
}

if (null === $this->name) {
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
if (ctype_digit($this->name[0])) {
Expand Down Expand Up @@ -424,7 +431,7 @@ protected function build(ContainerBuilder $container)
*/
protected function getContainerClass()
{
return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
return str_replace('\\', '_', \get_class($this)).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
}

/**
Expand Down Expand Up @@ -579,6 +586,9 @@ protected function getKernelParameters()
'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(),
'kernel.environment' => $this->environment,
'kernel.debug' => $this->debug,
/*
* @deprecated since Symfony 4.2
*/
'kernel.name' => $this->name,
'kernel.cache_dir' => realpath($cacheDir = $this->warmupDir ?: $this->getCacheDir()) ?: $cacheDir,
'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpKernel/KernelInterface.php
Expand Up @@ -100,6 +100,8 @@ public function locateResource($name, $dir = null, $first = true);
* Gets the name of the kernel.
*
* @return string The kernel name
*
* @deprecated since Symfony 4.2
*/
public function getName();

Expand Down
Expand Up @@ -30,7 +30,7 @@ public function testCollect()
$this->assertSame('test', $c->getEnv());
$this->assertTrue($c->isDebug());
$this->assertSame('config', $c->getName());
$this->assertSame('testkernel', $c->getAppName());
$this->assertSame('n/a', $c->getAppName());
$this->assertRegExp('~^'.preg_quote($c->getPhpVersion(), '~').'~', PHP_VERSION);
$this->assertRegExp('~'.preg_quote((string) $c->getPhpVersionExtra(), '~').'$~', PHP_VERSION);
$this->assertSame(PHP_INT_SIZE * 8, $c->getPhpArchitecture());
Expand Down
24 changes: 19 additions & 5 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Expand Up @@ -75,7 +75,7 @@ public function testInitializeContainerClearsOldContainers()
$kernel->boot();

$containerDir = __DIR__.'/Fixtures/cache/custom/'.substr(\get_class($kernel->getContainer()), 0, 16);
$this->assertTrue(unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta'));
$this->assertTrue(unlink(__DIR__.'/Fixtures/cache/custom/FixturesSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta'));
$this->assertFileExists($containerDir);
$this->assertFileNotExists($containerDir.'.legacy');

Expand Down Expand Up @@ -302,13 +302,19 @@ public function testGetRootDir()
$this->assertEquals(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures', realpath($kernel->getRootDir()));
}

/**
* @group legacy
*/
public function testGetName()
{
$kernel = new KernelForTest('test', true);

$this->assertEquals('Fixtures', $kernel->getName());
}

/**
* @group legacy
*/
public function testOverrideGetName()
{
$kernel = new KernelForOverrideName('test', true);
Expand Down Expand Up @@ -506,6 +512,9 @@ public function testKernelWithoutBundles()
$this->assertTrue($kernel->getContainer()->getParameter('test_executed'));
}

/**
* @group legacy
*/
public function testKernelRootDirNameStartingWithANumber()
{
$dir = __DIR__.'/Fixtures/123';
Expand All @@ -532,14 +541,14 @@ public function testKernelReset()

$containerClass = \get_class($kernel->getContainer());
$containerFile = (new \ReflectionClass($kernel->getContainer()))->getFileName();
unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');
unlink(__DIR__.'/Fixtures/cache/custom/FixturesSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta');

$kernel = new CustomProjectDirKernel();
$kernel->boot();

$this->assertInstanceOf($containerClass, $kernel->getContainer());
$this->assertFileExists($containerFile);
unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta');
unlink(__DIR__.'/Fixtures/cache/custom/FixturesSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta');

$kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); });
$kernel->boot();
Expand Down Expand Up @@ -707,9 +716,9 @@ class CustomProjectDirKernel extends Kernel
private $buildContainer;
private $httpKernel;

public function __construct(\Closure $buildContainer = null, HttpKernelInterface $httpKernel = null, $name = 'custom')
public function __construct(\Closure $buildContainer = null, HttpKernelInterface $httpKernel = null, $env = 'custom')
{
parent::__construct($name, true);
parent::__construct($env, true);

$this->baseDir = 'foo';
$this->buildContainer = $buildContainer;
Expand All @@ -725,6 +734,11 @@ public function registerContainerConfiguration(LoaderInterface $loader)
{
}

protected function getContainerClass()
{
return $this->name.parent::getContainerClass();
}

public function getProjectDir()
{
return $this->baseDir;
Expand Down

0 comments on commit 5fda7e2

Please sign in to comment.