php-pm / php-pm-httpkernel

HttpKernel adapter for use of Symfony and Laravel frameworks with PHP-PM
MIT License
246 stars 72 forks source link

Allow possibility to specify custom vendor directory or symfony app directory (thereby autoload.php location) #120

Closed faizanakram99 closed 6 years ago

faizanakram99 commented 6 years ago

PHPPM\Bootstraps\Symfony::getApplication() contains hard-coded autoload.php path which may not be always correct as we can specify a different vendor dir in composer.json file. Also some applications (particularly old ones) may have autoload.php in different location like the application I work upon has autoload.php at ./symfony/app/autoload.php instead of ./app/autoload.php

In my case I was not able to run the application, for now I just modified the source locally to make it work (which would be lost ofcourse when composer update runs)


public function getApplication()
    {
        // include applications autoload
        $appAutoLoader = './app/autoload.php';
        if (file_exists($appAutoLoader)) {
            require $appAutoLoader;
        } else {
            require './vendor/autoload.php';
        }
....

Would be nice if autoload path is configurable say by specifying vendor dir or symfony app dir in ppm.json or maybe by inferring vendor and app from composer.json and$container->parameter('kernel.root_dir') respectively. Without being able to modify the autoload.php path, I wont be able to use php-pm at production

andig commented 6 years ago

Without being able to modify the autoload.php path, I wont be able to use php-pm at production

You can always create your own bootstrap and subclass the Symfony one. Otherwise I'd be happy to accept a PR.

dnna commented 6 years ago

I'm also using a custom vendor dir and had to extend the Symfony bootstrap class just for this line. What would be the best way to fix this? Allow it to be configured via an environment variable and fallback to './vendor/autoload.php' if not defined?

andig commented 6 years ago

Not sure why it's such a big thing to override the bootstrap? Here's a simple example used outside the Symfony world: https://github.com/volkszaehler/volkszaehler.org/blob/master/lib/Server/PPMBootstrapAdapter.php

dnna commented 6 years ago

Well to update the vendor dir in https://github.com/php-pm/php-pm-httpkernel/blob/master/Bootstraps/Symfony.php you need to override the whole getApplication method, which contains almost the entire bootstrap logic. Maybe we should at least create a getVendorDir method that can be overridden individually?

andig commented 6 years ago

Sounds good- but without making it api in terms of an additional interface?

Any maybe- instead of getVendorDir- rather autoload or prepareAutoloader to do the actual require statements, too?

dnna commented 6 years ago

I was just thinking of adding a protected function getVendorDir() in the Symfony bootstrap class so it can be overridden without changing the entire getApplication(). If people from other frameworks end up wanting it too we could abstract it then.

I don't think we can do the actual require statements outside of getApplication due to scoping issues. Besides in the normal Symfony entry point (https://github.com/symfony/demo/blob/master/public/index.php) they also just straight out require it.

I'll make a PR so we can discuss it there.

andig commented 6 years ago

Closed via #122

dnna commented 6 years ago

@faizanakram99 you can use the COMPOSER_VENDOR_DIR environment variable now to set the path of the autoload.php

faizanakram99 commented 6 years ago

Great, thanks all of you