Skip to content

Instantly share code, notes, and snippets.

@hannesvdvreken
Last active March 18, 2023 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hannesvdvreken/379aef9640ffccb487d5 to your computer and use it in GitHub Desktop.
Save hannesvdvreken/379aef9640ffccb487d5 to your computer and use it in GitHub Desktop.
ignore platform requirements

Composer update without PHP environment checking

Hi there, welcome for another blog post on Composer.

Since version 1.0.0-alpha9 composer has a new flag for 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.

And the docs read this:

--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 or require (which 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 were +1'd 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.

Though this is not the reason why I am excited about the new flag. Mainly because I don't even have PHP installed on my host machine. I'm a vagrant purist.

In my opinion this is a useful addition because you can now install dependencies on automated systems. Test boxes, if you will. Automated systems like tavis-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 development 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 Mockery defined as a dev-dependency for testing and "ext-mongo": "~1.5" as production use, you can now use the --ignore-platform-reqs flag to install Mockery anayway. Like so, one can inject mocked MongoDB Client objects into classes without the need to load the mongo extension. Unless you're doing acceptance/integration testing with the extension and 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:

- before_script:
  - travis_retry composer self-update
  - travis_retry composer install --prefer-source --no-interaction --dev --ignore-platform-reqs

One other case when this flag might help you is when one of your dependencies requires a higher PHP version than your package. 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 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 comes in handy, leave a comment or hit me up on twitter (@hannesvdvreken).

By the way, I 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 return 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 for both PHP version and extension versions can be done using the version_compare function, although the tilde and caret operators will need some more string manipulations and conditionals.

Peace Out.

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment