Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Deprecate the Kernel name #28809

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

Merged
merged 1 commit into from
Oct 15, 2018
Merged
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] deprecated the Kernel name
  • Loading branch information
fabpot committed Oct 15, 2018
commit 98ff750ee148a1c44032b67a669c9752930a28b9
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

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

4.1.0
-----

Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 @@ -78,7 +81,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 @@ -268,8 +271,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 @@ -421,7 +428,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';
Copy link
Member

@nicolas-grekas nicolas-grekas Oct 16, 2018

Choose a reason for hiding this comment

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

This change breaks BC by removing the "name" (the basename of the project_dir really) from the dimensions that compose separate namespaces.
This is why the CI is red (and was also caught before merging ;) )
Side note: the container class is also a legacy concept; its real name is random (it's the hash of its contents actually.)
The only real remaining use case of the container class is for generating the name of the file that serves as entry point for loading the container.

}

/**
Expand Down Expand Up @@ -576,6 +583,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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