Question

I know that Composer can determine php, hhvm, ext-<name> and some lib-<name> dependencies. Would the same possible over commands and binaries on PATH? Actually, for instance, I wrote scripts to ensure that Tesseract OCR is present. It can be done with own Composer features?

Was it helpful?

Solution

You can achieve this using a Composer hook, like pre-install-cmd or pre-update-cmd, which executes a PHP method. Here is my test:

composer.json

{
  "require": { "pimple/pimple": "*" },
  "autoload": { "psr-0": { "Acme\\": "src/" } }
  "scripts": {
    "pre-install-cmd": "Acme\\Composer\\Hooks::checkBinary",
    "pre-update-cmd": "Acme\\Composer\\Hooks::checkBinary"
  }
}

src/Acme/Composer/Hooks.php

<?php
namespace Acme\Composer;

use Composer\Script\Event;

class Hooks
{
    public static function checkBinary(Event $event) {
        $io = $event->getIO();
        $path = explode(':', getenv('PATH'));
        // do something with $path elements or anything else
        if ($somethingWentWrong) {
            // Throwing an Exception will cause Composer to stop processing.
            throw new \Exception('Check your PATH');
        } else {
            $io->write('checkBinary() completed.');
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top