[HttpKernel] fix deprecating KernelInterface::getRootDir()

This commit is contained in:
Nicolas Grekas 2018-10-16 15:47:03 +02:00
parent 78c98e2b15
commit 819f525547
10 changed files with 36 additions and 47 deletions

View File

@ -128,13 +128,14 @@ EOF
$domain = $input->getOption('domain'); $domain = $input->getOption('domain');
/** @var KernelInterface $kernel */ /** @var KernelInterface $kernel */
$kernel = $this->getApplication()->getKernel(); $kernel = $this->getApplication()->getKernel();
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
// Define Root Paths // Define Root Paths
$transPaths = array($kernel->getRootDir(false).'/Resources/translations'); $transPaths = array($rootDir.'/Resources/translations');
if ($this->defaultTransPath) { if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath; $transPaths[] = $this->defaultTransPath;
} }
$viewsPaths = array($kernel->getRootDir(false).'/Resources/views'); $viewsPaths = array($rootDir.'/Resources/views');
if ($this->defaultViewsPath) { if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath; $viewsPaths[] = $this->defaultViewsPath;
} }
@ -147,12 +148,12 @@ EOF
if ($this->defaultTransPath) { if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName(); $transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
} }
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(false), $bundle->getName()); $transPaths[] = sprintf('%s/Resources/%s/translations', $rootDir, $bundle->getName());
$viewsPaths = array($bundle->getPath().'/Resources/views'); $viewsPaths = array($bundle->getPath().'/Resources/views');
if ($this->defaultViewsPath) { if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName(); $viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
} }
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(false), $bundle->getName()); $viewsPaths[] = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName());
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
// such a bundle does not exist, so treat the argument as path // such a bundle does not exist, so treat the argument as path
$transPaths = array($input->getArgument('bundle').'/Resources/translations'); $transPaths = array($input->getArgument('bundle').'/Resources/translations');
@ -168,12 +169,12 @@ EOF
if ($this->defaultTransPath) { if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName(); $transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
} }
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(false), $bundle->getName()); $transPaths[] = sprintf('%s/Resources/%s/translations', $rootDir, $bundle->getName());
$viewsPaths[] = $bundle->getPath().'/Resources/views'; $viewsPaths[] = $bundle->getPath().'/Resources/views';
if ($this->defaultViewsPath) { if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName(); $viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
} }
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(false), $bundle->getName()); $viewsPaths[] = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName());
} }
} }

View File

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

View File

@ -17,19 +17,14 @@ use Symfony\Component\Templating\Helper\Helper;
/** /**
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* *
* @internal * @internal since Symfony 4.2, all properties will be private in 5.0
*/ */
class CodeHelper extends Helper class CodeHelper extends Helper
{ {
protected $fileLinkFormat; protected $fileLinkFormat;
/** protected $rootDir; // to be renamed $projectDir in 5.0
* @deprecated since Symfony 4.2
*/
protected $rootDir;
protected $charset; protected $charset;
private $projectDir;
/** /**
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files * @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
* @param string $projectDir The project root directory * @param string $projectDir The project root directory
@ -38,8 +33,7 @@ class CodeHelper extends Helper
public function __construct($fileLinkFormat, string $projectDir, 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->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$this->projectDir = str_replace('\\', '/', $projectDir).'/'; $this->rootDir = str_replace('\\', '/', $projectDir).'/';
$this->rootDir = $this->projectDir;
$this->charset = $charset; $this->charset = $charset;
} }
@ -164,10 +158,10 @@ class CodeHelper extends Helper
if (null === $text) { if (null === $text) {
$file = trim($file); $file = trim($file);
$fileStr = $file; $fileStr = $file;
if (0 === strpos($fileStr, $this->projectDir)) { if (0 === strpos($fileStr, $this->rootDir)) {
$fileStr = str_replace(array('\\', $this->projectDir), array('/', ''), $fileStr); $fileStr = str_replace(array('\\', $this->rootDir), array('/', ''), $fileStr);
$fileStr = htmlspecialchars($fileStr, $flags, $this->charset); $fileStr = htmlspecialchars($fileStr, $flags, $this->charset);
$fileStr = sprintf('<abbr title="%s">kernel.project_dir</abbr>/%s', htmlspecialchars($this->projectDir, $flags, $this->charset), $fileStr); $fileStr = sprintf('<abbr title="%s">kernel.project_dir</abbr>/%s', htmlspecialchars($this->rootDir, $flags, $this->charset), $fileStr);
} }
$text = sprintf('%s at line %d', $fileStr, $line); $text = sprintf('%s at line %d', $fileStr, $line);

View File

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

View File

@ -26,19 +26,9 @@ class TestAppKernel extends Kernel
); );
} }
public function setProjectDir($projectDir) public function getProjectDir()
{ {
$this->projectDir = $projectDir; return __DIR__.'/test';
}
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) public function registerContainerConfiguration(LoaderInterface $loader)

View File

@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand; use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel; use Symfony\Component\HttpKernel;
@ -176,20 +177,18 @@ class TranslationDebugCommandTest extends TestCase
->will($this->returnValueMap($returnValues)); ->will($this->returnValueMap($returnValues));
} }
$kernel
->expects($this->any())
->method('getRootDir')
->will($this->returnValue($this->translationDir));
$kernel $kernel
->expects($this->any()) ->expects($this->any())
->method('getBundles') ->method('getBundles')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
$container = new Container();
$container->setParameter('kernel.root_dir', $this->translationDir);
$kernel $kernel
->expects($this->any()) ->expects($this->any())
->method('getContainer') ->method('getContainer')
->will($this->returnValue($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock())); ->will($this->returnValue($container));
$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates'); $command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates');

View File

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

View File

@ -123,6 +123,8 @@ interface KernelInterface extends HttpKernelInterface, \Serializable
* Gets the application root dir (path of the project's Kernel class). * Gets the application root dir (path of the project's Kernel class).
* *
* @return string The Kernel root dir * @return string The Kernel root dir
*
* @deprecated since Symfony 4.2
*/ */
public function getRootDir(); public function getRootDir();

View File

@ -26,6 +26,11 @@ class KernelWithoutBundles extends Kernel
{ {
} }
public function getProjectDir()
{
return __DIR__;
}
protected function build(ContainerBuilder $container) protected function build(ContainerBuilder $container)
{ {
$container->setParameter('test_executed', true); $container->setParameter('test_executed', true);

View File

@ -521,7 +521,7 @@ EOF;
$kernel->boot(); $kernel->boot();
$this->assertSame(__DIR__.'/Fixtures', $kernel->getProjectDir()); $this->assertSame(__DIR__.'/Fixtures', $kernel->getProjectDir());
$this->assertSame(__DIR__.'/Fixtures', $kernel->getContainer()->getParameter('kernel.project_dir')); $this->assertSame(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures', $kernel->getContainer()->getParameter('kernel.project_dir'));
} }
public function testKernelReset() public function testKernelReset()