Check for required PHP extensions with Composer
php composer.phar install
after updating my sources just makes
me smile.
That brings me to today and preparing to deploy a new Linode instance. After provisioning my new server, grabbing the sources and running the Composer install I sat back, put my feet up on the desk and thought life is just grand.
Houston we have a problem
Roughly 30 minutes later I get a notification from NewRelic informing me that my error rate is at 10%, Doh! So back to the server logs to see what’s up and find.
PHP Fatal error: Class 'Imagick' not found
Well that explains things. Just need to install the PHP Imagick module and we are back in business. But how can I make sure this doesn’t happen again? I could configure a build system using Phing or Ant, or just write a bash build script to automate it in the future. Both would work abet be overkill for the scope of the project. That’s when I turned to composer and found information on Platform packages.
Composer Platform Packages
Composer has platform packages, which are virtual packages for things that are installed on the system but are not actually installable by Composer. This includes PHP itself, PHP extensions and some system libraries.
Woo hoo! That’s exactly what I was looking for. To validate the availability of PHP extensions, all that is needed is to
preface its name with ext- and add it to the requre section of the package.json
file.
Example
{
"name": "composer/example",
"require": {
"php": ">=5.4.0",
"swiftmailer/swiftmailer": "5.3.0",
"ext-imagick": "*"
}
}
Note: Notice the specified version is set to *. The Composer docs state that:
Versioning can be quite inconsistent here, so it's often a good idea to just set the constraint to *.
Testing it out
Now to test I locally removed the imagick extension and ran composer again which returned:
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested PHP extension ext-imagick * is missing from your system.
Conclusion
With that in place now I can track these dependencies in the future. It would be nice to have it handle the installation of the missing extensions but at least knowing makes things a whole lot easier.