-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add Kernel::getProjectDir() #22315
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
Add Kernel::getProjectDir() #22315
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,8 @@ abstract class Kernel implements KernelInterface, TerminableInterface | |
protected $startTime; | ||
protected $loadClassCache; | ||
|
||
private $projectDir; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the extension point is the public method, not the property. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well the issue is that the property is set in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making things protected means we then have to ensure backward compatibility on the access to the property. This is a strong commitment. So we never replace private with protected without a strong use case for it. And if you have one, please open a dedicated issue to discuss it instead of doing it on the merged PR (people are generally looking at opened discussion only when catching up) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #22727 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #22728 for alternative |
||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation is really strange. It basically assigns the variable twice. Once in the method and once in the constructor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Tobion the method itself can be overwritten to change the logic (for instance for people wanting to avoid issues with open base dir). But the Kernel still wants to ensure its private property stores the right thing. And then, the default implementation relies on the property internally to avoid doing the filesystem traversal multiple times |
||
$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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not happening right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Virtually at least :) We cannot really display a deprecation as it's used everywhere. But, I want to get rid of it in Symfony Flex, so that in 4, we can really deprecate
root_dir
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it should have a
@deprecated
but not atrigger_error()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well.. deprecation !== crashing :) then again migrating it is a burden.. so i see we care a bit more here.
Adding
@deprecated
sounds like a good idea 👍