As a rule, you use Composer to handle the dependencies of your project and to create your own packages to be included in other projects. But you can also use Composer to start a Script at given points during the tool life cycle. In particular the most used are the Hooks of: “Pre Install”, “Post Install”, “Pre Update” and “Post Update”.

For which purpose can you use these events? One of the most common functions is, for example, to generate the application configurations.  Usually, the configurations are not included in the code revision system as: username and password, as each programmer has own personal credentials for code development. The Hooks can be useful to create files dedicated to the programmer in a simple and fast manner. As first step, you need to configure the "scripts" section of “composer.json” file, to declare the Hooks to be used.

{
    "scripts": {
        "post-update-cmd": [
            "Corley\Composer\MyHook::localConf"
        ],
        "post-install-cmd": [
            "Corley\Deploy\MyHook::localConf"
        ]
    },
    "autoload": {
        "psr-0": {
            "Corley": "src/"
        }
    }
}

Afterwards, you need to simply create a new class for handling events.

<?php
namespace CorleyComposer;

use ComposerScriptEvent;

class MyHook
{
    public static function localConf(Event $event)
    {
        $event->getIO()->write("Show me after INSTALL/UPDATE command");
    }
}

Then, when running the Composer commands

php composer.phar install           # Install operation
php composer.phar update         # Update operation

Your Hook will be invoked and consequently the system will be automatically configured. As you can see, the event receives a parameter allowing the interaction with the platform  IO system: so you can implement your functions very easily.

Comments

comments powered by Disqus

cloudparty

Follow Us