Composer. How to install a specific version of package based on user php version?

后端 未结 2 1605
清歌不尽
清歌不尽 2021-01-22 21:50

I have a two version of my package: for php7 and for php5. Is it possible to make composer determine when installing the package which version of php the user has, and depending

相关标签:
2条回答
  • 2021-01-22 22:33

    Install only one composer you can access this composer with different php version

    /usr/bin/php /usr/local/bin/composer
    /usr/bin/php7.1 /usr/local/bin/composer
    /usr/bin/php7.0 /usr/local/bin/composer
    
    0 讨论(0)
  • 2021-01-22 22:34

    TL;DR: Yes.

    By default, composer uses the version of the php executable to determine, which version of the package to install. This can be overridden in the config section of composer.json, for example:

    "config": {
        "vendor-dir": "vendor",
        "platform": {
            "php": "5.6"
        }
     }
    

    When someone requires your package, this version is compared to the one specified in the requirements list of your package's composer.json:

    "require": {
        "php": ">=7.2.0",
    }
    

    So if, for example, version 1 of your package requires php 5.6, and version 2 requires php 7.0, someone who runs composer require your-package with php 5.6 will have version 1 installed. If someone runs it with an older version than required by any of your versions, they'll get an error stating that composer could not find a package that satisfies all the requirements, the php version being one of them.

    0 讨论(0)
提交回复
热议问题