phpdependenciescomposer-phpshell-exec

Is there a way to specify in composer.json that a binary must be available?


I know that when creating the composer.json I can define which php extensions need to be installed to get my application running. Can the same be done for a binary that should be executed via exec() or shell_exec()?


Solution

  • There seems to be a lengthy discussion about this kind of functionality in the composer repository: https://github.com/composer/composer/issues/9636

    And composer plugins that aim to solve this: https://github.com/bamarni/composer-bin-plugin

    A rather simple solution is to declare a callback script (https://getcomposer.org/doc/articles/scripts.md) that checks if the binary can be found. For example a pre-autoload-dump script that will be executed everytime the autoload is generated.

    E.g. composer.json requiring 7zip to be installed:

    {
        "name": "lwohlhart/app-with-7zip-requirement",
        "authors": [
            {
                "name": "Lucas Wohlhart",
                "email": "lucas@wohlhart.at"
            }
        ],
        "require": {
        },
        "scripts": {
            "pre-autoload-dump": [
                    "which 7z >> /dev/null; ERROR=$?; if [ $ERROR -ne 0 ]; then echo \"7zip not installed\"; (exit $ERROR); fi"
            ]
        }
    }
    

    Or if you don't want a custom error message just:

    "pre-autoload-dump": [
        "which 7z"
    ]