Zhalgas Zhamaliev
Zhalgas Zhamaliev

Reputation: 23

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

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 on this, install the correct version of my package?

Upvotes: 2

Views: 2380

Answers (2)

Raju Sadadiya
Raju Sadadiya

Reputation: 49

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

Upvotes: 0

Bartosz Zasada
Bartosz Zasada

Reputation: 3900

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.

Upvotes: 6

Related Questions