Skip to content

Commit eaa8529

Browse files
[HttpKernel] fix deprecating KernelInterface::getRootDir()
1 parent 880281a commit eaa8529

File tree

6 files changed

+27
-45
lines changed

6 files changed

+27
-45
lines changed

Command/TranslationDebugCommand.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
128128
$domain = $input->getOption('domain');
129129
/** @var KernelInterface $kernel */
130130
$kernel = $this->getApplication()->getKernel();
131+
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
131132

132133
// Define Root Paths
133-
$transPaths = array($kernel->getRootDir(false).'/Resources/translations');
134+
$transPaths = array($rootDir.'/Resources/translations');
134135
if ($this->defaultTransPath) {
135136
$transPaths[] = $this->defaultTransPath;
136137
}
137-
$viewsPaths = array($kernel->getRootDir(false).'/Resources/views');
138+
$viewsPaths = array($rootDir.'/Resources/views');
138139
if ($this->defaultViewsPath) {
139140
$viewsPaths[] = $this->defaultViewsPath;
140141
}
@@ -147,12 +148,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
147148
if ($this->defaultTransPath) {
148149
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
149150
}
150-
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(false), $bundle->getName());
151+
$transPaths[] = sprintf('%s/Resources/%s/translations', $rootDir, $bundle->getName());
151152
$viewsPaths = array($bundle->getPath().'/Resources/views');
152153
if ($this->defaultViewsPath) {
153154
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
154155
}
155-
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(false), $bundle->getName());
156+
$viewsPaths[] = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName());
156157
} catch (\InvalidArgumentException $e) {
157158
// such a bundle does not exist, so treat the argument as path
158159
$transPaths = array($input->getArgument('bundle').'/Resources/translations');
@@ -168,12 +169,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
168169
if ($this->defaultTransPath) {
169170
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
170171
}
171-
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(false), $bundle->getName());
172+
$transPaths[] = sprintf('%s/Resources/%s/translations', $rootDir, $bundle->getName());
172173
$viewsPaths[] = $bundle->getPath().'/Resources/views';
173174
if ($this->defaultViewsPath) {
174175
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
175176
}
176-
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(false), $bundle->getName());
177+
$viewsPaths[] = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName());
177178
}
178179
}
179180

Command/TranslationUpdateCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
118118
}
119119
/** @var KernelInterface $kernel */
120120
$kernel = $this->getApplication()->getKernel();
121+
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
121122

122123
// Define Root Paths
123-
$transPaths = array($kernel->getRootDir(false).'/Resources/translations');
124+
$transPaths = array($rootDir.'/Resources/translations');
124125
if ($this->defaultTransPath) {
125126
$transPaths[] = $this->defaultTransPath;
126127
}
127-
$viewsPaths = array($kernel->getRootDir(false).'/Resources/views');
128+
$viewsPaths = array($rootDir.'/Resources/views');
128129
if ($this->defaultViewsPath) {
129130
$viewsPaths[] = $this->defaultViewsPath;
130131
}
@@ -138,12 +139,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
138139
if ($this->defaultTransPath) {
139140
$transPaths[] = $this->defaultTransPath.'/'.$foundBundle->getName();
140141
}
141-
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(false), $foundBundle->getName());
142+
$transPaths[] = sprintf('%s/Resources/%s/translations', $rootDir, $foundBundle->getName());
142143
$viewsPaths = array($foundBundle->getPath().'/Resources/views');
143144
if ($this->defaultViewsPath) {
144145
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$foundBundle->getName();
145146
}
146-
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(false), $foundBundle->getName());
147+
$viewsPaths[] = sprintf('%s/Resources/%s/views', $rootDir, $foundBundle->getName());
147148
$currentName = $foundBundle->getName();
148149
} catch (\InvalidArgumentException $e) {
149150
// such a bundle does not exist, so treat the argument as path

Templating/Helper/CodeHelper.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@
1717
/**
1818
* @author Fabien Potencier <fabien@symfony.com>
1919
*
20-
* @internal
20+
* @internal since Symfony 4.2, all properties will be private in 5.0
2121
*/
2222
class CodeHelper extends Helper
2323
{
2424
protected $fileLinkFormat;
25-
/**
26-
* @deprecated since Symfony 4.2
27-
*/
28-
protected $rootDir;
25+
protected $rootDir; // to be renamed $projectDir in 5.0
2926
protected $charset;
3027

31-
private $projectDir;
32-
3328
/**
3429
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
3530
* @param string $projectDir The project root directory
@@ -38,8 +33,7 @@ class CodeHelper extends Helper
3833
public function __construct($fileLinkFormat, string $projectDir, string $charset)
3934
{
4035
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
41-
$this->projectDir = str_replace('\\', '/', $projectDir).'/';
42-
$this->rootDir = $this->projectDir;
36+
$this->rootDir = str_replace('\\', '/', $projectDir).'/';
4337
$this->charset = $charset;
4438
}
4539

@@ -164,10 +158,10 @@ public function formatFile($file, $line, $text = null)
164158
if (null === $text) {
165159
$file = trim($file);
166160
$fileStr = $file;
167-
if (0 === strpos($fileStr, $this->projectDir)) {
168-
$fileStr = str_replace(array('\\', $this->projectDir), array('/', ''), $fileStr);
161+
if (0 === strpos($fileStr, $this->rootDir)) {
162+
$fileStr = str_replace(array('\\', $this->rootDir), array('/', ''), $fileStr);
169163
$fileStr = htmlspecialchars($fileStr, $flags, $this->charset);
170-
$fileStr = sprintf('<abbr title="%s">kernel.project_dir</abbr>/%s', htmlspecialchars($this->projectDir, $flags, $this->charset), $fileStr);
164+
$fileStr = sprintf('<abbr title="%s">kernel.project_dir</abbr>/%s', htmlspecialchars($this->rootDir, $flags, $this->charset), $fileStr);
171165
}
172166

173167
$text = sprintf('%s at line %d', $fileStr, $line);

Tests/Command/CacheClearCommand/CacheClearCommandTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,17 @@ class CacheClearCommandTest extends TestCase
2727
private $kernel;
2828
/** @var Filesystem */
2929
private $fs;
30-
private $projectDir;
3130

3231
protected function setUp()
3332
{
3433
$this->fs = new Filesystem();
3534
$this->kernel = new TestAppKernel('test', true);
36-
$this->projectDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('sf_cache_', true);
37-
$this->kernel->setProjectDir($this->projectDir);
38-
$this->fs->mkdir($this->projectDir);
35+
$this->fs->mkdir($this->kernel->getProjectDir());
3936
}
4037

4138
protected function tearDown()
4239
{
43-
$this->fs->remove($this->projectDir);
40+
$this->fs->remove($this->kernel->getProjectDir());
4441
}
4542

4643
public function testCacheIsFreshAfterCacheClearedWithWarmup()

Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,9 @@ public function registerBundles()
2626
);
2727
}
2828

29-
public function setProjectDir($projectDir)
29+
public function getProjectDir()
3030
{
31-
$this->projectDir = $projectDir;
32-
}
33-
34-
public function getCacheDir()
35-
{
36-
return $this->getProjectDir().'/Tests/Fixtures/cache.'.$this->environment;
37-
}
38-
39-
public function getLogDir()
40-
{
41-
return $this->getProjectDir().'/Tests/Fixtures/logs';
31+
return __DIR__.'/test';
4232
}
4333

4434
public function registerContainerConfiguration(LoaderInterface $loader)

Tests/Command/TranslationDebugCommandTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
1616
use Symfony\Bundle\FrameworkBundle\Console\Application;
1717
use Symfony\Component\Console\Tester\CommandTester;
18+
use Symfony\Component\DependencyInjection\Container;
1819
use Symfony\Component\Filesystem\Filesystem;
1920
use Symfony\Component\HttpKernel;
2021

@@ -176,20 +177,18 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
176177
->will($this->returnValueMap($returnValues));
177178
}
178179

179-
$kernel
180-
->expects($this->any())
181-
->method('getRootDir')
182-
->will($this->returnValue($this->translationDir));
183-
184180
$kernel
185181
->expects($this->any())
186182
->method('getBundles')
187183
->will($this->returnValue(array()));
188184

185+
$container = new Container();
186+
$container->setParameter('kernel.root_dir', $this->translationDir);
187+
189188
$kernel
190189
->expects($this->any())
191190
->method('getContainer')
192-
->will($this->returnValue($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock()));
191+
->will($this->returnValue($container));
193192

194193
$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates');
195194

0 commit comments

Comments
 (0)