Composer update ignore php version

Enforcing a PHP Version for Installed Composer Packages

If your development and production environments don’t match you can easily get tripped up when the time comes to deploy to the live server. It’s not too uncommon for developers to find themselves working with one version of PHP and using another in an app or website’s final destination. If you use Composer to manage PHP packages it would be nice to be able to take this into account to avoid any nasty surprises post deployment. Thankfully Composer has this covered.

We can tell Composer what version of PHP we are supporting with our app/website by using the platform configuration in our composer.json file.

In this example we are faking the version of PHP to 5.6.1. This means that whenever we try and install or update a package with Composer the faked platform version of PHP will be taken into account rather than the version of PHP being used on the command-line we are running Composer from. This is really useful if our production environment uses a different setup to the one we are developing on. For example, you may be using an up to date version of PHP 7 locally, but deploy to a server still using 5.6.

Another way of setting this is from the command-line.

composer config platform.php 5.6.1

This will set the platform option in the composer.json file for us. In this example it would add PHP 5.6.1 to the JSON file just like in the previous example. You can also use the -g flag to set this globally.

On a Composer package platform requirements are added just like package dependencies. So for example, if we have a package that requires a minimum of PHP 7.1.0 the composer.json file would look like:-

This is what Composer will use when installing and updating packages when our project has a platform configuration.

Note that when adding a minimum PHP requirement we add this as a dependency using the require key, whereas setting the platform setup uses platform under the config options.

When it comes to installing and updating packages you may at times want to ignore the platform requirements. For example, when adding or updating a dev dependency that will never be installed on the production environment. For this we can use the —ignore-platform-reqs flag.

composer update phpunit/phpunit --ignore-platform-reqs

Although using the platform configuration in composer.json might not catch all unsuitable packages when using Composer it should hopefully catch most incompatible code that could otherwise have found their way onto your production environment. This has been a life saver for me in the past and helped me avoid many headaches.

Читайте также:  Java округлить до одной сотой

Источник

How to install Composer packages ignoring PHP version requirements

A new feature in Composer v2 allows you to selectively ignore platform requirements.

composer install --ignore-platform-req=php

Compared to —ignore-platform-reqs

Composer already has a —ignore-platform-reqs option (notice the s in reqs ), but it ignores all platform requirements, including PHP version, extensions ( ext-* ), and composer-plugin-api .

The new —ignore-platform-req option can be used to set specific requirements that Composer can ignore.

Ignore multiple platform requirements

You can specify one or more platform requirements to ignore by simply using the option multiple times.

For example, to ignore the PHP version and ext-zip , but enforce all other platform requirements, you can use the command like this:

composer install --ignore-platform-req=php --ignore-platform-req=ext-zip

Install Composer packages on unreleased PHP versions

A common use-case of ignore-platform-req option would be to test PHP packages in unreleased PHP versions, or dev builds of PHP.

Libraries such as PHPUnit come with rather strict PHP version constraint in their composer.json file, which prevents them the libraries from being installed in dev versions of PHP.

Instead of using ignore-platform-reqs , which would ignore all platform requirements such as extensions (which might be required for the package to function), you can use ignore-platform-req option to selectively specify platform requirements you intentionally ignore.

Recent Articles on PHP.Watch

How to extend lifetime of legacy PHP applications

How to extend lifetime of legacy PHP applications

As PHP continue to evolve with new breaking changes, and while that is great for most PHP applications, there are legacy applications that can’t justify the human and financial cost of keeping up. Here is a guide on how to extend the lifetime of legacy PHP applications with security updates and maintenance.

Читайте также:  Python indent vs code

PHP 8.2 Highlights: What

PHP 8.2 Highlights: What’s New and Changed

How to install/upgrade PHP 8.2 on Debian and Ubuntu systems

How to install/upgrade PHP 8.2 on Debian and Ubuntu systems

You will receive an email on last Wednesday of every month and on major PHP releases with new articles related to PHP, upcoming changes, new features and what’s changing in the language. No marketing emails, no selling of your contacts, no click-tracking, and one-click instant unsubscribe from any email you receive.

© 2018-2023 PHP.Watch, with ❤ from Ayesh • About PHP.Watch

Источник

Composer ignore-platform-reqs flag

Since version 1.0.0-alpha9 composer has a new option flag available on the install and update commands. I’m talking about the —ignore-platform-reqs flag. From the changelog:

Added --ignore-platform-reqs to install/update commands to install even if you are missing a php extension or have an invalid php version.
--ignore-platform-reqs: ignore php, hhvm, lib-* and ext-* requirements and force the installation even if the local machine does not fulfill these.

Normally when you run composer install, update, require or create-project (these will invoke update), composer will compare your installed PHP version with the PHP version limitations from your composer.json’s require field and the PHP version requirements of your other dependencies. The same goes for HHVM. It will also check if some optional PHP extensions are loaded and match the required version, as well as the PHP extension requirements of the required dependencies. When the versions don’t match up, composer will not install your required packagist packages.

But if you add the —ignore-platform-reqs option when you run composer update , it will gracefully ignore these restrictions.

Now, when this feature was being added, people +1’d this for the following reason: they could run composer commands from a different environment than the environment in which the application would run. This is people running composer install on their host machine while their application runs on their vagrant box. I don’t think this solves their problem in all cases because of possible composer after scripts.

I don’t have anything installed on my host system except vagrant, git and some desktop apps. So this is not the reason why I am excited about the new flag.

In my opinion this is a useful addition because you can now install dependencies on simplified test machines. Automated systems like travis-ci and scrutinizer-ci use test boxes without any, or with only a few PHP extensions installed on it. In order to get composer to install your dependencies, one needs to run bash commands as before scripts to install pear/pecl packages and maybe even modify php.ini files to load these extensions. When for example you have mongodb/mongodb defined as a dependency, you are required to have «ext-mongo»: «~1.6» installed on your system. You can now use the —ignore-platform-reqs flag to install the MongoDB package anyway, without needing the extension. Like so, one can inject mocked MongoDB Client objects into classes and never hit the mongo extension requirement. Unless you’re doing acceptance/integration testing with an actual MongoDB database server installed on the test box, this new flag might be useful to you. Your .travis.yml file before_script section will be able to be reduced to:

Читайте также:  Python parse russian date

One other case when this flag might help you is when one of your dependencies requires a higher PHP version than you. Let’s say you want to run tests for PHP version 5.3 and you have some helper package as a dependency, but not for testing, which requires PHP 5.4. In that case you may also use the —ignore-platform-reqs flag. Be careful not to misabuse this flag, though. Minimum PHP versions are defined as they are for a reason!

Well, this was why I think this flag is very useful for package developers. Scrutinizer-ci confirmed they will use this flag in the near future when they updated their boxes with the newest composer. If you happen to know other cases where this flag is useful, leave a comment or hit me up on twitter (@hannesvdvreken).

I also added this small addendum for the people who wonder how composer is able to check your PHP environment. Here’s how: you’re able to retrieve your PHP version using the phpversion function. This function returns the value that is stored in the PHP_VERSION global constant. To check if an extension is loaded one can use the extension_loaded function and to retrieve the extension version: phpversion($extensionName); . Version comparisons can be done performed using the version_compare function, although the tilde (~) and caret (^) operators that composer allows will need some more string manipulations and conditionals.

  • Composer changelog: https://github.com/composer/composer/releases/tag/1.0.0-alpha9
  • ignore-platform-reqs GitHub issue: https://github.com/composer/composer/issues/1426
  • ignore-platform-reqs documentation: https://getcomposer.org/doc/03-cli.md#require
  • Travis: https://travis-ci.org/
  • Scrutinizer: https://scrutinizer-ci.com/
  • travis_retry http://docs.travis-ci.com/user/build-timeouts/

Источник

Оцените статью