Skip to content

[HttpKernel] fix deprecating KernelInterface::getRootDir() #28890

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 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$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;
}
Expand All @@ -147,12 +148,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
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');
Expand All @@ -168,12 +169,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
/** @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;
}
Expand All @@ -138,12 +139,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@
/**
* @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
Expand All @@ -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).'/';
Copy link
Member

Choose a reason for hiding this comment

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

I would keep the new name, it's easier to follow along.

Copy link
Member Author

@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.

but changing it breaks BC...

Copy link
Member

Choose a reason for hiding this comment

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

Yes and no. That's internal/obsolete code (templating vs Twig).

$this->charset = $charset;
}

Expand Down Expand Up @@ -164,10 +158,10 @@ public function formatFile($file, $line, $text = null)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@ public function registerBundles()
);
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;

Expand Down Expand Up @@ -176,20 +177,18 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
->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');

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -123,6 +123,8 @@ public function isDebug();
* 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function registerContainerConfiguration(LoaderInterface $loader)
{
}

public function getProjectDir()
{
return __DIR__;
}

protected function build(ContainerBuilder $container)
{
$container->setParameter('test_executed', true);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public function testProjectDirExtension()
$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()
Expand Down