Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjn/772b12a1f393eca845c1 to your computer and use it in GitHub Desktop.
Save tomjn/772b12a1f393eca845c1 to your computer and use it in GitHub Desktop.
Checking if composer ran succesfully after the fact

Here the composer file has a script that hooks into pre and post update/install commands. The logic is as follows:

  • Composer fires the pre method, which touches a file
  • Composer does its business
  • When finished, it fires the post method which removes the touched file

If for some reason the file still exists, then something went wrong. Either composer is still running, or it ran and encountered a fatal error, e.g. missing package, no internet connection, version lock, etc. This can now be picked up by the PHP application by testing for the presence of the file.

{
"require": {
"justinrainsdasdfbow/json-schema": "~1.1"
},
"scripts": {
"post-update-cmd": "cftp\\composer_checker::post",
"post-install-cmd": "cftp\\composer_checker::post",
"pre-install-cmd": "cftp\\composer_checker::pre",
"pre-update-cmd": "cftp\\composer_checker::pre"
},
"autoload": {
"psr-0": {
"cftp": "src/"
}
}
}
<?php
// this file goes inside the src/cftp folder
namespace cftp;
use Composer\Script\Event;
class composer_checker {
public static function post(Event $event)
{
unlink('.composerrunning');
}
public static function pre( Event $event ) {
touch('.composerrunning');
}
}
@tomjn
Copy link
Author

tomjn commented Aug 15, 2014

The composer.json in this gist has a deliberate spelling error in the required package name to demonstrate a failed composer run

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