Skip to content

Commit 7b8409a

Browse files
committed
feature symfony#22315 Add Kernel::getProjectDir() (fabpot)
This PR was merged into the 3.3-dev branch. Discussion ---------- Add Kernel::getProjectDir() | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes/no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | not yet `Kernel::getRootDir()` is misleading. It returns the path where `AppKernel.php` is stored, which is `app/` in Symfony 2/3, but `src/` in Symfony 4. But most of the time, we are using `getRootDir()` to get the project root dir, so `%kernel.root_dir%/../` is a common idiom. Changing the value of `getRootDir()` would be a hard BC break, so I propose to create a new method, `getProjectDir()` to "replace" `getRootDir()` gradually. Commits ------- 1f680cc added Kernel::getProjectDir()
2 parents 49ae724 + 1f680cc commit 7b8409a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
3.3.0
55
-----
66

7+
* Added `kernel.project_dir` and `Kernel::getProjectDir()`
8+
* Deprecated `kernel.root_dir` and `Kernel::getRootDir()`
79
* Deprecated `Kernel::getEnvParameters()`
810
* Deprecated the special `SYMFONY__` environment variables
911
* added the possibility to change the query string parameter used by `UriSigner`

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ abstract class Kernel implements KernelInterface, TerminableInterface
5959
protected $startTime;
6060
protected $loadClassCache;
6161

62+
private $projectDir;
63+
6264
const VERSION = '3.3.0-DEV';
6365
const VERSION_ID = 30300;
6466
const MAJOR_VERSION = 3;
@@ -80,6 +82,7 @@ public function __construct($environment, $debug)
8082
$this->environment = $environment;
8183
$this->debug = (bool) $debug;
8284
$this->rootDir = $this->getRootDir();
85+
$this->projectDir = $this->getProjectDir();
8386
$this->name = $this->getName();
8487

8588
if ($this->debug) {
@@ -306,6 +309,28 @@ public function getRootDir()
306309
return $this->rootDir;
307310
}
308311

312+
/**
313+
* Gets the application root dir (path of the project's composer file).
314+
*
315+
* @return string The project root dir
316+
*/
317+
public function getProjectDir()
318+
{
319+
if (null === $this->projectDir) {
320+
$r = new \ReflectionObject($this);
321+
$dir = $rootDir = dirname($r->getFileName());
322+
while (!file_exists($dir.'/composer.json')) {
323+
if ($dir === dirname($dir)) {
324+
return $this->projectDir = $rootDir;
325+
}
326+
$dir = dirname($dir);
327+
}
328+
$this->projectDir = $dir;
329+
}
330+
331+
return $this->projectDir;
332+
}
333+
309334
/**
310335
* {@inheritdoc}
311336
*/
@@ -559,6 +584,7 @@ protected function getKernelParameters()
559584
return array_merge(
560585
array(
561586
'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir,
587+
'kernel.project_dir' => realpath($this->projectDir) ?: $this->projectDir,
562588
'kernel.environment' => $this->environment,
563589
'kernel.debug' => $this->debug,
564590
'kernel.name' => $this->name,

src/Symfony/Component/HttpKernel/KernelInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public function getEnvironment();
121121
public function isDebug();
122122

123123
/**
124-
* Gets the application root dir.
124+
* Gets the application root dir (path of the project's Kernel class).
125125
*
126-
* @return string The application root dir
126+
* @return string The Kernel root dir
127127
*/
128128
public function getRootDir();
129129

0 commit comments

Comments
 (0)