diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index e2ca619f4a63d..35379055bb7be 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -4,6 +4,8 @@ CHANGELOG 3.3.0 ----- + * Added `kernel.project_dir` and `Kernel::getProjectDir()` + * Deprecated `kernel.root_dir` and `Kernel::getRootDir()` * Deprecated `Kernel::getEnvParameters()` * Deprecated the special `SYMFONY__` environment variables * added the possibility to change the query string parameter used by `UriSigner` diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 6236fcef8b296..f166ce67cfe41 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -59,6 +59,8 @@ abstract class Kernel implements KernelInterface, TerminableInterface protected $startTime; protected $loadClassCache; + private $projectDir; + const VERSION = '3.3.0-DEV'; const VERSION_ID = 30300; const MAJOR_VERSION = 3; @@ -80,6 +82,7 @@ public function __construct($environment, $debug) $this->environment = $environment; $this->debug = (bool) $debug; $this->rootDir = $this->getRootDir(); + $this->projectDir = $this->getProjectDir(); $this->name = $this->getName(); if ($this->debug) { @@ -306,6 +309,28 @@ public function getRootDir() return $this->rootDir; } + /** + * Gets the application root dir (path of the project's composer file). + * + * @return string The project root dir + */ + public function getProjectDir() + { + if (null === $this->projectDir) { + $r = new \ReflectionObject($this); + $dir = $rootDir = dirname($r->getFileName()); + while (!file_exists($dir.'/composer.json')) { + if ($dir === dirname($dir)) { + return $this->projectDir = $rootDir; + } + $dir = dirname($dir); + } + $this->projectDir = $dir; + } + + return $this->projectDir; + } + /** * {@inheritdoc} */ @@ -553,6 +578,7 @@ protected function getKernelParameters() return array_merge( array( 'kernel.root_dir' => realpath($this->rootDir) ?: $this->rootDir, + 'kernel.project_dir' => realpath($this->projectDir) ?: $this->projectDir, 'kernel.environment' => $this->environment, 'kernel.debug' => $this->debug, 'kernel.name' => $this->name, diff --git a/src/Symfony/Component/HttpKernel/KernelInterface.php b/src/Symfony/Component/HttpKernel/KernelInterface.php index d7308ae5e0c14..b8609b9ededb9 100644 --- a/src/Symfony/Component/HttpKernel/KernelInterface.php +++ b/src/Symfony/Component/HttpKernel/KernelInterface.php @@ -121,9 +121,9 @@ public function getEnvironment(); public function isDebug(); /** - * Gets the application root dir. + * Gets the application root dir (path of the project's Kernel class). * - * @return string The application root dir + * @return string The Kernel root dir */ public function getRootDir();