minor #28890 [HttpKernel] fix deprecating KernelInterface::getRootDir() (nicolas-grekas)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[HttpKernel] fix deprecating KernelInterface::getRootDir()

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Follow up of #28810.
Did I get it right?

Commits
-------

819f525547 [HttpKernel] fix deprecating KernelInterface::getRootDir()
This commit is contained in:
Fabien Potencier 2018-10-16 21:55:54 +02:00
commit 557f85d1b3
10 changed files with 36 additions and 47 deletions

View File

@ -128,13 +128,14 @@ EOF
$domain = $input->getOption('domain');
/** @var KernelInterface $kernel */
$kernel = $this->getApplication()->getKernel();
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
// Define Root Paths
$transPaths = array($kernel->getRootDir(false).'/Resources/translations');
$transPaths = array($rootDir.'/Resources/translations');
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
$viewsPaths = array($kernel->getRootDir(false).'/Resources/views');
$viewsPaths = array($rootDir.'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}
@ -147,12 +148,12 @@ EOF
if ($this->defaultTransPath) {
$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');
if ($this->defaultViewsPath) {
$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) {
// such a bundle does not exist, so treat the argument as path
$transPaths = array($input->getArgument('bundle').'/Resources/translations');
@ -168,12 +169,12 @@ EOF
if ($this->defaultTransPath) {
$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';
if ($this->defaultViewsPath) {
$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 */
$kernel = $this->getApplication()->getKernel();
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
// Define Root Paths
$transPaths = array($kernel->getRootDir(false).'/Resources/translations');
$transPaths = array($rootDir.'/Resources/translations');
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
$viewsPaths = array($kernel->getRootDir(false).'/Resources/views');
$viewsPaths = array($rootDir.'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}
@ -138,12 +139,12 @@ EOF
if ($this->defaultTransPath) {
$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');
if ($this->defaultViewsPath) {
$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();
} catch (\InvalidArgumentException $e) {
// 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>
*
* @internal
* @internal since Symfony 4.2, all properties will be private in 5.0
*/
class CodeHelper extends Helper
{
protected $fileLinkFormat;
/**
* @deprecated since Symfony 4.2
*/
protected $rootDir;
protected $rootDir; // to be renamed $projectDir in 5.0
protected $charset;
private $projectDir;
/**
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
* @param string $projectDir The project root directory
@ -38,8 +33,7 @@ class CodeHelper extends Helper
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->projectDir = str_replace('\\', '/', $projectDir).'/';
$this->rootDir = $this->projectDir;
$this->rootDir = str_replace('\\', '/', $projectDir).'/';
$this->charset = $charset;
}
@ -164,10 +158,10 @@ class CodeHelper extends Helper
if (null === $text) {
$file = trim($file);
$fileStr = $file;
if (0 === strpos($fileStr, $this->projectDir)) {
$fileStr = str_replace(array('\\', $this->projectDir), array('/', ''), $fileStr);
if (0 === strpos($fileStr, $this->rootDir)) {
$fileStr = str_replace(array('\\', $this->rootDir), array('/', ''), $fileStr);
$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);

View File

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

View File

@ -26,19 +26,9 @@ class TestAppKernel extends Kernel
);
}
public function setProjectDir($projectDir)
public function getProjectDir()
{
$this->projectDir = $projectDir;
}
public function getCacheDir()
{
return $this->getProjectDir().'/Tests/Fixtures/cache.'.$this->environment;
}
public function getLogDir()
{
return $this->getProjectDir().'/Tests/Fixtures/logs';
return __DIR__.'/test';
}
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\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel;
@ -176,20 +177,18 @@ class TranslationDebugCommandTest extends TestCase
->will($this->returnValueMap($returnValues));
}
$kernel
->expects($this->any())
->method('getRootDir')
->will($this->returnValue($this->translationDir));
$kernel
->expects($this->any())
->method('getBundles')
->will($this->returnValue(array()));
$container = new Container();
$container->setParameter('kernel.root_dir', $this->translationDir);
$kernel
->expects($this->any())
->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');

View File

@ -4,7 +4,7 @@ CHANGELOG
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
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).
*
* @return string The Kernel root dir
*
* @deprecated since Symfony 4.2
*/
public function getRootDir();

View File

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

View File

@ -521,7 +521,7 @@ EOF;
$kernel->boot();
$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()