Skip to content
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

How to force Composer to load local symfony/filesystem instead of global Composer's symfony/filesystem in post-install-cmd script #7708

Closed
leymannx opened this issue Oct 12, 2018 · 2 comments

Comments

@leymannx
Copy link

leymannx commented Oct 12, 2018

My composer.json:

{
    "name": "leymannx/wordpress-project",
    "description": "Project template for WordPress projects with composer",
    "type": "project",
    "license": "GPL-2.0+",
    "authors": [
        {
            "name": "",
            "role": ""
        }
    ],
    "repositories": [
        {
            "type": "composer",
            "url": "https://wpackagist.org"
        }
    ],
    "minimum-stability": "dev",
    "prefer-stable": true,
    "config": {
        "sort-packages": true,
        "bin-dir": "bin/"
    },
    "require": {
        "johnpbloch/wordpress-core": "~4.9",
        "leymannx/wordpress-finder": "^0.0.14",
        "oomphinc/composer-installers-extender": "^1.1",
        "wp-cli/wp-cli-bundle": "^2.0"
    },
    "require-dev": {
        "behat/mink-goutte-driver": "^1.2",
        "behat/mink-selenium2-driver": "^1.3",
        "paulgibbs/behat-wordpress-extension": "^0.7.1"
    },
    "extra": {
        "installer-types": [
            "library",
            "wordpress-core",
            "wordpress-plugin",
            "wordpress-theme",
            "wordpress-muplugin",
            "wordpress-dropin"
        ],
        "installer-paths": {
            "web/": ["type:wordpress-core"],
            "wp-vendor/plugins/{$name}/": ["type:wordpress-plugin"],
            "wp-vendor/themes/{$name}/": ["type:wordpress-theme"],
            "wp-vendor/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
            "wp-vendor/{$name}/": ["type:wordpress-dropin"]
        }
    },
    "autoload": {
        "classmap": [
            "scripts/composer/ScriptHandler.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "WordpressProject\\composer\\ScriptHandler::CreateRequiredFiles",
            "WordpressProject\\composer\\ScriptHandler::CreateSymlinks",
            "WordpressProject\\composer\\ScriptHandler::removeBrokenSymlinks"
        ],
        "post-update-cmd": [
            "WordpressProject\\composer\\ScriptHandler::CreateRequiredFiles",
            "WordpressProject\\composer\\ScriptHandler::CreateSymlinks",
            "WordpressProject\\composer\\ScriptHandler::removeBrokenSymlinks"
        ]
    }
}

Output of composer diagnose:

Checking composer.json: WARNING
License "GPL-2.0+" is a deprecated SPDX license identifier, use "GPL-2.0-or-later" instead
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com oauth access: OK
Checking disk free space: OK
Checking pubkeys: 
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0  87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B  0C708369 153E328C AD90147D AFE50952
OK
Checking composer version: OK
Composer version: 1.7.2
PHP version: 7.1.16
PHP binary path: /usr/bin/php

When I run this command inside my script fired as post-install-cmd:

use Symfony\Component\Filesystem\Filesystem;
$fs = new Filesystem();
$a = new \ReflectionClass($fs);
echo $a->getFileName();

I get the following output:

phar:///usr/local/bin/composer/vendor/symfony/filesystem/Filesystem.php

And I expected this to happen:

/Users/leymannx/Sites/wordpress-project/vendor/symfony/filesystem/Filesystem.php

What else?

Steps to reproduce

@stof
Copy link
Contributor

stof commented Oct 12, 2018

Well, during the run of composer itself, the code coming from the phar is being used. that's totally expected, as PHP does not allow redefining classes, and the code from composer is loaded first in the process (to run composer itself).

If your script requires using your own deps and not the version shipped in a composer, a solution is to a use a CLI script rather than a PHP callable as the script (as it means the code will run in a separate PHP process with its own loading of classes)

@leymannx
Copy link
Author

leymannx commented Oct 12, 2018

My question on Stack Overflow got answered and finally pointed me in the right direction. All I needed to do was to require_once __DIR__.'/../vendor/autoload.php'; at the top of my script.

On The Filesystem Component docs page it's also written:

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer.

@leymannx leymannx changed the title How to force Composer to load local symfony/filesystem instead of global Composer's symfony/filesystem How to force Composer to load local symfony/filesystem instead of global Composer's symfony/filesystem in post-install-cmd script Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants