getkirby / ideas

This is the backlog of ideas and feature requests from the last two years. Use our new feedback platform to post your new ideas or vote on existing ideas.
https://feedback.getkirby.com
20 stars 0 forks source link

Static method to get the installation root #370

Open hdodov opened 5 years ago

hdodov commented 5 years ago

It would be very useful to be able to get the Kirby installation directory when developing Kirby utilities. I want to further advance my kirby-tester and to get the installation directory, I need to do something like:

$ref = new ReflectionClass('Kirby');
$path = $ref->getFileName();
$split = explode(DIRECTORY_SEPARATOR . 'kirby' . DIRECTORY_SEPARATOR . 'src', $path);
var_dump($split[0]); // installation directory

To help with that, the Kirby class (src/cms/App.php) could have this static method:

public static function dir() {
    return dirname(__DIR__, 3);
}

Then, I'd be able to use Kirby::dir() to get the directory, which will be much more reliable.

lukasbestle commented 5 years ago

We already have a static $root property, but it's protected. We could add a method to return that.

hdodov commented 5 years ago

Yeah, but that property is set in the constructor of the class. So if you want to get the root, there must be an instance first, which can't be guaranteed. If you create a new Kirby instance, you might overwrite a previous one.

Also, the $root points to the kirby directory, not the installation directory. I'm thinking something like the following pseudo code might be alright:

public static function webRoot () {
    if (hasInstance) {
        return instance->roots->index;
    } else {
        return dirname(__DIR__, 3);
    }
}

Since all roots can be changed, it should use the index root of an already existing instance, when there is one.


Also, why is this static::$root = dirname(__DIR__, 2); set in the class constructor when its constant? Couldn't it be set right after the class declaration just once? It might be against the PSR gospel, but it makes sense.