symfony/recipes

Kernel::getProjectDir() not working as expected when invoked from a vendor library

yannoff opened this issue · 2 comments

Following up on #1000 with a new but problematic use-case.

Use-case

Let's say (I actually did) I developed a 3rd party library, published on packagist.org, which needs to create a new instance of the Kernel.

For example:

# vendor/yannoff/acme/src/Demo.php

namespace Yannoff\Component\Acme;

use App\Kernel;

class Demo
{
    // ...
    public function doSomething(string $env, bool $debug)
    {
         $kernel = new Kernel($env, $debug);
    }
    // ...
}

Result

Guess what ? The component have it's own composer.json !

So with the actual defaults, the base Kernel::getProjectDir() class returns a wrong path, namely:

vendor/yannoff/acme

Meaningless to say, the above mentioned component does not have control over to the main app's Kernel class behavior.

Creating a Kernel in a vendor is suspicious design to me.
Of course, I don't know your use case.
Anyway, this is not something we should support in the default setup (aka it's yours to fix ;) )

@nicolas-grekas Thanks for the reply :)
Actually I create a Kernel instance to run a session in a secluded environment for a Symfony REPL I developed on my own, I known this may not be the best design or practice but I haven't figured out another way so far...
I still think relying on the composer.json path to find the project dir is not consistent enough but it's my opinion ^^
As you said, it's my fix: I've found a workaround by extending the Kernel class and overriding the getProjectDir() method, so that's fine ;)