[HttpKernel] deprecated usage of getRootDir() and kernel.root_dir

This commit is contained in:
Fabien Potencier 2018-10-10 08:44:30 -07:00
parent 5fda7e2fb1
commit 0b851c6956
24 changed files with 106 additions and 116 deletions

View File

@ -177,6 +177,12 @@ HttpFoundation
* The default value of the "$secure" and "$samesite" arguments of Cookie's constructor
changed respectively from "false" to "null" and from "null" to "lax".
HttpKernel
----------
* The `Kernel::getRootDir()` and the `kernel.root_dir` parameter have been removed
* The `KernelInterface::getName()` and the `kernel.name` parameter have been removed
Monolog
-------

View File

@ -365,7 +365,7 @@ abstract class AbstractDoctrineExtension extends Extension
if ($container->hasParameter('cache.prefix.seed')) {
$seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
} else {
$seed = '_'.$container->getParameter('kernel.root_dir');
$seed = '_'.$container->getParameter('kernel.project_dir');
}
$seed .= '.'.$container->getParameter('kernel.container_class').'.'.$container->getParameter('kernel.environment').'.'.$container->getParameter('kernel.debug');
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.ContainerBuilder::hash($seed);

View File

@ -272,7 +272,7 @@ class DoctrineExtensionTest extends TestCase
'kernel.debug' => false,
'kernel.environment' => 'test',
'kernel.container_class' => 'kernel',
'kernel.root_dir' => __DIR__,
'kernel.project_dir' => __DIR__,
), $data)));
}
}

View File

@ -74,7 +74,6 @@ EOT
array('Environment', $kernel->getEnvironment()),
array('Debug', $kernel->isDebug() ? 'true' : 'false'),
array('Charset', $kernel->getCharset()),
array('Root directory', self::formatPath($kernel->getRootDir(), $kernel->getProjectDir())),
array('Cache directory', self::formatPath($kernel->getCacheDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getCacheDir()).'</>)'),
array('Log directory', self::formatPath($kernel->getLogDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getLogDir()).'</>)'),
new TableSeparator(),

View File

@ -130,11 +130,11 @@ EOF
$kernel = $this->getApplication()->getKernel();
// Define Root Paths
$transPaths = array($kernel->getRootDir().'/Resources/translations');
$transPaths = array($kernel->getRootDir(false).'/Resources/translations');
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
$viewsPaths = array($kernel->getRootDir().'/Resources/views');
$viewsPaths = array($kernel->getRootDir(false).'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}
@ -147,12 +147,12 @@ EOF
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
}
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $bundle->getName());
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(false), $bundle->getName());
$viewsPaths = array($bundle->getPath().'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
}
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $bundle->getName());
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(false), $bundle->getName());
} catch (\InvalidArgumentException $e) {
// such a bundle does not exist, so treat the argument as path
$transPaths = array($input->getArgument('bundle').'/Resources/translations');
@ -168,12 +168,12 @@ EOF
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
}
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $bundle->getName());
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(false), $bundle->getName());
$viewsPaths[] = $bundle->getPath().'/Resources/views';
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
}
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $bundle->getName());
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(false), $bundle->getName());
}
}

View File

@ -120,11 +120,11 @@ EOF
$kernel = $this->getApplication()->getKernel();
// Define Root Paths
$transPaths = array($kernel->getRootDir().'/Resources/translations');
$transPaths = array($kernel->getRootDir(false).'/Resources/translations');
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
$viewsPaths = array($kernel->getRootDir().'/Resources/views');
$viewsPaths = array($kernel->getRootDir(false).'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}
@ -138,12 +138,12 @@ EOF
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath.'/'.$foundBundle->getName();
}
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $foundBundle->getName());
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(false), $foundBundle->getName());
$viewsPaths = array($foundBundle->getPath().'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$foundBundle->getName();
}
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $foundBundle->getName());
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(false), $foundBundle->getName());
$currentName = $foundBundle->getName();
} catch (\InvalidArgumentException $e) {
// such a bundle does not exist, so treat the argument as path

View File

@ -16,22 +16,30 @@ use Symfony\Component\Templating\Helper\Helper;
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @internal
*/
class CodeHelper extends Helper
{
protected $fileLinkFormat;
/**
* @deprecated since Symfony 4.2
*/
protected $rootDir;
protected $charset;
private $projectDir;
/**
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
* @param string $rootDir The project root directory
* @param string $projectDir The project root directory
* @param string $charset The charset
*/
public function __construct($fileLinkFormat, string $rootDir, string $charset)
public function __construct($fileLinkFormat, string $projectDir, string $charset)
{
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$this->rootDir = str_replace('\\', '/', $rootDir).'/';
$this->projectDir = str_replace('\\', '/', $projectDir).'/';
$this->rootDir = $this->projectDir;
$this->charset = $charset;
}
@ -156,10 +164,10 @@ class CodeHelper extends Helper
if (null === $text) {
$file = trim($file);
$fileStr = $file;
if (0 === strpos($fileStr, $this->rootDir)) {
$fileStr = str_replace(array('\\', $this->rootDir), array('/', ''), $fileStr);
if (0 === strpos($fileStr, $this->projectDir)) {
$fileStr = str_replace(array('\\', $this->projectDir), array('/', ''), $fileStr);
$fileStr = htmlspecialchars($fileStr, $flags, $this->charset);
$fileStr = sprintf('<abbr title="%s">kernel.root_dir</abbr>/%s', htmlspecialchars($this->rootDir, $flags, $this->charset), $fileStr);
$fileStr = sprintf('<abbr title="%s">kernel.project_dir</abbr>/%s', htmlspecialchars($this->projectDir, $flags, $this->charset), $fileStr);
}
$text = sprintf('%s at line %d', $fileStr, $line);

View File

@ -27,20 +27,20 @@ class CacheClearCommandTest extends TestCase
private $kernel;
/** @var Filesystem */
private $fs;
private $rootDir;
private $projectDir;
protected function setUp()
{
$this->fs = new Filesystem();
$this->kernel = new TestAppKernel('test', true);
$this->rootDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('sf_cache_', true);
$this->kernel->setRootDir($this->rootDir);
$this->fs->mkdir($this->rootDir);
$this->projectDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('sf_cache_', true);
$this->kernel->setProjectDir($this->projectDir);
$this->fs->mkdir($this->projectDir);
}
protected function tearDown()
{
$this->fs->remove($this->rootDir);
$this->fs->remove($this->projectDir);
}
public function testCacheIsFreshAfterCacheClearedWithWarmup()

View File

@ -26,9 +26,19 @@ class TestAppKernel extends Kernel
);
}
public function setRootDir($rootDir)
public function setProjectDir($projectDir)
{
$this->rootDir = $rootDir;
$this->projectDir = $projectDir;
}
public function getCacheDir()
{
return $this->getProjectDir().'/Tests/Fixtures/cache.'.$this->environment;
}
public function getLogDir()
{
return $this->getProjectDir().'/Tests/Fixtures/logs';
}
public function registerContainerConfiguration(LoaderInterface $loader)

View File

@ -32,7 +32,7 @@ class CachePoolClearerPassTest extends TestCase
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$container->setParameter('kernel.project_dir', 'foo');
$globalClearer = new Definition(Psr6CacheClearer::class);
$container->setDefinition('cache.global_clearer', $globalClearer);

View File

@ -37,7 +37,7 @@ class CachePoolPassTest extends TestCase
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$container->setParameter('kernel.project_dir', 'foo');
$adapter = new Definition();
$adapter->setAbstract(true);
$adapter->addTag('cache.pool');
@ -58,7 +58,7 @@ class CachePoolPassTest extends TestCase
$container = new ContainerBuilder();
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.root_dir', 'foo');
$container->setParameter('kernel.project_dir', 'foo');
$container->register('cache.adapter.array', ArrayAdapter::class)->addArgument(0);
@ -128,7 +128,7 @@ class CachePoolPassTest extends TestCase
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$container->setParameter('kernel.project_dir', 'foo');
$adapter = new Definition();
$adapter->setAbstract(true);
$adapter->addTag('cache.pool');

View File

@ -47,14 +47,14 @@ class AppKernel extends Kernel
public function registerBundles()
{
if (!file_exists($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
if (!file_exists($filename = $this->getProjectDir().'/'.$this->testCase.'/bundles.php')) {
throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
}
return include $filename;
}
public function getRootDir()
public function getProjectDir()
{
return __DIR__;
}

View File

@ -1,6 +1,6 @@
framework:
secret: test
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
router: { resource: "%kernel.project_dir%/%kernel.test_case%/routing.yml" }
validation: { enabled: true, enable_annotations: true }
csrf_protection: true
form: true

View File

@ -53,14 +53,14 @@ class AppKernel extends Kernel
public function registerBundles()
{
if (!is_file($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
if (!is_file($filename = $this->getProjectDir().'/'.$this->testCase.'/bundles.php')) {
throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
}
return include $filename;
}
public function getRootDir()
public function getProjectDir()
{
return __DIR__;
}

View File

@ -1,6 +1,6 @@
framework:
secret: test
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
router: { resource: "%kernel.project_dir%/%kernel.test_case%/routing.yml" }
validation: { enabled: true, enable_annotations: true }
csrf_protection: true
form: true

View File

@ -1,6 +1,6 @@
framework:
secret: test
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
router: { resource: "%kernel.project_dir%/%kernel.test_case%/routing.yml" }
validation: { enabled: true, enable_annotations: true }
assets: ~
csrf_protection: true

View File

@ -50,7 +50,7 @@ class CachePoolPass implements CompilerPassInterface
if ($container->hasParameter('cache.prefix.seed')) {
$seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
} else {
$seed = '_'.$container->getParameter('kernel.root_dir');
$seed = '_'.$container->getParameter('kernel.project_dir');
}
$seed .= '.'.$container->getParameter('kernel.container_class').'.'.$container->getParameter('kernel.environment');

View File

@ -29,7 +29,7 @@ class CachePoolClearerPassTest extends TestCase
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$container->setParameter('kernel.project_dir', 'foo');
$globalClearer = new Definition(Psr6CacheClearer::class);
$container->setDefinition('cache.global_clearer', $globalClearer);

View File

@ -34,7 +34,7 @@ class CachePoolPassTest extends TestCase
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$container->setParameter('kernel.project_dir', 'foo');
$adapter = new Definition();
$adapter->setAbstract(true);
$adapter->addTag('cache.pool');
@ -55,7 +55,7 @@ class CachePoolPassTest extends TestCase
$container = new ContainerBuilder();
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.root_dir', 'foo');
$container->setParameter('kernel.project_dir', 'foo');
$container->register('cache.adapter.array', ArrayAdapter::class)->addArgument(0);
@ -125,7 +125,7 @@ class CachePoolPassTest extends TestCase
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('kernel.root_dir', 'foo');
$container->setParameter('kernel.project_dir', 'foo');
$adapter = new Definition();
$adapter->setAbstract(true);
$adapter->addTag('cache.pool');

View File

@ -4,6 +4,7 @@ CHANGELOG
4.2.0
-----
* deprecated `Kernel::getRootDir()` and the `kernel.root_dir` parameter
* deprecated `KernelInterface::getName()` and the `kernel.name` parameter
4.1.0

View File

@ -54,6 +54,9 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
protected $bundles = array();
protected $container;
/**
* @deprecated since Symfony 4.2
*/
protected $rootDir;
protected $environment;
protected $debug;
@ -83,7 +86,7 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
{
$this->environment = $environment;
$this->debug = $debug;
$this->rootDir = $this->getRootDir();
$this->rootDir = $this->getRootDir(false);
$this->name = $this->getName(false);
}
@ -281,7 +284,7 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
}
if (null === $this->name) {
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->getProjectDir()));
if (ctype_digit($this->name[0])) {
$this->name = '_'.$this->name;
}
@ -308,9 +311,15 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2, use getProjectDir() instead
*/
public function getRootDir()
public function getRootDir(/* $triggerDeprecation = true */)
{
if (0 === \func_num_args() || func_get_arg(0)) {
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use getProjectDir() instead.', __METHOD__), E_USER_DEPRECATED);
}
if (null === $this->rootDir) {
$r = new \ReflectionObject($this);
$this->rootDir = \dirname($r->getFileName());
@ -370,7 +379,7 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
*/
public function getCacheDir()
{
return $this->rootDir.'/cache/'.$this->environment;
return $this->getProjectDir().'/var/cache/'.$this->environment;
}
/**
@ -378,7 +387,7 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
*/
public function getLogDir()
{
return $this->rootDir.'/logs';
return $this->getProjectDir().'/var/log';
}
/**
@ -582,6 +591,9 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
}
return array(
/*
* @deprecated since Symfony 4.2, use kernel.project_dir instead
*/
'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir,
'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(),
'kernel.environment' => $this->environment,

View File

@ -1,37 +0,0 @@
<?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\Component\HttpKernel\Tests\Fixtures\_123;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
class Kernel123 extends Kernel
{
public function registerBundles()
{
return array();
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
}
public function getCacheDir()
{
return sys_get_temp_dir().'/'.Kernel::VERSION.'/kernel123/cache/'.$this->environment;
}
public function getLogDir()
{
return sys_get_temp_dir().'/'.Kernel::VERSION.'/kernel123/logs';
}
}

View File

@ -34,4 +34,14 @@ class KernelForTest extends Kernel
{
return $this->booted;
}
public function getCacheDir()
{
return $this->getProjectDir().'/Tests/Fixtures/cache.'.$this->environment;
}
public function getLogDir()
{
return $this->getProjectDir().'/Tests/Fixtures/logs';
}
}

View File

@ -33,7 +33,7 @@ class KernelTest extends TestCase
public static function tearDownAfterClass()
{
$fs = new Filesystem();
$fs->remove(__DIR__.'/Fixtures/cache');
$fs->remove(__DIR__.'/Fixtures/var');
}
public function testConstructor()
@ -67,15 +67,15 @@ class KernelTest extends TestCase
public function testInitializeContainerClearsOldContainers()
{
$fs = new Filesystem();
$legacyContainerDir = __DIR__.'/Fixtures/cache/custom/ContainerA123456';
$legacyContainerDir = __DIR__.'/Fixtures/var/cache/custom/ContainerA123456';
$fs->mkdir($legacyContainerDir);
touch($legacyContainerDir.'.legacy');
$kernel = new CustomProjectDirKernel();
$kernel->boot();
$containerDir = __DIR__.'/Fixtures/cache/custom/'.substr(\get_class($kernel->getContainer()), 0, 16);
$this->assertTrue(unlink(__DIR__.'/Fixtures/cache/custom/FixturesSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta'));
$containerDir = __DIR__.'/Fixtures/var/cache/custom/'.substr(\get_class($kernel->getContainer()), 0, 16);
$this->assertTrue(unlink(__DIR__.'/Fixtures/var/cache/custom/Symfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta'));
$this->assertFileExists($containerDir);
$this->assertFileNotExists($containerDir.'.legacy');
@ -295,6 +295,9 @@ EOF;
$this->assertEquals($expected, $output);
}
/**
* @group legacy
*/
public function testGetRootDir()
{
$kernel = new KernelForTest('test', true);
@ -309,7 +312,7 @@ EOF;
{
$kernel = new KernelForTest('test', true);
$this->assertEquals('Fixtures', $kernel->getName());
$this->assertEquals('HttpKernel', $kernel->getName());
}
/**
@ -512,43 +515,32 @@ EOF;
$this->assertTrue($kernel->getContainer()->getParameter('test_executed'));
}
/**
* @group legacy
*/
public function testKernelRootDirNameStartingWithANumber()
{
$dir = __DIR__.'/Fixtures/123';
require_once $dir.'/Kernel123.php';
$kernel = new \Symfony\Component\HttpKernel\Tests\Fixtures\_123\Kernel123('dev', true);
$this->assertEquals('_123', $kernel->getName());
}
public function testProjectDirExtension()
{
$kernel = new CustomProjectDirKernel();
$kernel->boot();
$this->assertSame('foo', $kernel->getProjectDir());
$this->assertSame('foo', $kernel->getContainer()->getParameter('kernel.project_dir'));
$this->assertSame(__DIR__.'/Fixtures', $kernel->getProjectDir());
$this->assertSame(__DIR__.'/Fixtures', $kernel->getContainer()->getParameter('kernel.project_dir'));
}
public function testKernelReset()
{
(new Filesystem())->remove(__DIR__.'/Fixtures/cache');
(new Filesystem())->remove(__DIR__.'/Fixtures/var/cache');
$kernel = new CustomProjectDirKernel();
$kernel->boot();
$containerClass = \get_class($kernel->getContainer());
$containerFile = (new \ReflectionClass($kernel->getContainer()))->getFileName();
unlink(__DIR__.'/Fixtures/cache/custom/FixturesSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta');
unlink(__DIR__.'/Fixtures/var/cache/custom/Symfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta');
$kernel = new CustomProjectDirKernel();
$kernel->boot();
$this->assertInstanceOf($containerClass, $kernel->getContainer());
$this->assertFileExists($containerFile);
unlink(__DIR__.'/Fixtures/cache/custom/FixturesSymfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta');
unlink(__DIR__.'/Fixtures/var/cache/custom/Symfony_Component_HttpKernel_Tests_CustomProjectDirKernelCustomDebugContainer.php.meta');
$kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); });
$kernel->boot();
@ -720,7 +712,6 @@ class CustomProjectDirKernel extends Kernel
{
parent::__construct($env, true);
$this->baseDir = 'foo';
$this->buildContainer = $buildContainer;
$this->httpKernel = $httpKernel;
}
@ -734,17 +725,7 @@ class CustomProjectDirKernel extends Kernel
{
}
protected function getContainerClass()
{
return $this->name.parent::getContainerClass();
}
public function getProjectDir()
{
return $this->baseDir;
}
public function getRootDir()
{
return __DIR__.'/Fixtures';
}