Composer’s New Colophon Friend: The Post Install Command Tail

Lately I got interested in composer’s feature of post install commands.

I created a simple library and its composer file as follows:

{
    "name": "cordoval/colophon",
    ...
    "require": {
        "php": ">=5.3.2",
        "symfony/console": "dev-master"
    },
    "autoload": {
        "psr-0": {
            "Colophon": "lib/"
        }
    },
    "bin": ["bin/colophon"]
}

The important thing is the `bin` and the `symfony/console` dependency. The `bin` will tell composer to place our library script bin/colophon to be taken into consideration so that it can be called from the your project’s top level directory `bin/colophon`. The `symfony/console` part is because the `bin/colophon` calls an application part of the console to run our lib command which is based on symfony command class inside `symfony/console`.

Our `cordoval/colophon` lib contains this class:

<?php
namespace Colophon;
 
class Colophon
{
    public static function postUpdate($event)
    {
        self::postInstall($event);
    }
 
    public static function postInstall($event)
    {
        //$composer = $event->getComposer();
 
        // 1st pull
        system('bin/colophon print');
    }
}

Colphon class shown above has static methods called by your project’s post install directives inside your project’s composer.json:

// ...
"require": {
        "php": ">=5.3.2",
        "cordoval/colophon": "*",
        // ...
}
// ...
"scripts": {
        "post-install-cmd": [
            "Colophon\\Colophon::postInstall",
        ],
        "post-update-cmd": [
            "Colophon\\Colophon::postUpdate",
        ]
    },
// ...
    "config": {
        "bin-dir": "bin"
    },
// ...

Your project’s composer.json also sets the require to get `cordoval/colophon` package and also tells your project to place all the scripts into the `bin` top level folder.

Notice `cordoval/colophon` defines a command `bin/colophon print` which basically outputs text into the cli. This would be used by `cordoval/colophon` to output some nice ascii text like the one image on top.

Thanks for your support. If you want to see me on San Francisco SymfonyLive please donate i need $1700 to go out of the initial $1800, thanks so much for all those who donated, it is so encouraging to see the community standing up on this!

Leave a Reply

Your email address will not be published. Required fields are marked *