Thursday, October 11, 2012

PHP from the command line using Clip CLI framework

Just wanted to post about my latest invention. It's called Clip and it's a very simple framework for making PHP CLI applications. I recently got a new job and have been working a lot with several PHP console commands. The problem I found was that there wasn't a good stand alone framework I could use to make creating and editing these commands easy.

Sure there are a lot of great frameworks with some kind of console component to them (Symfony, Yii, Zend, etc.) but I wanted something where the console was the sole focus. So partially for fun and partially to scratch my perceived itch I came up with Clip.

Clip IMO turned out pretty good. It's a single file that comes in at ~130 LOC (as of right now) and packs a few really nice features. First and foremost it's dead simple to use. All you need to do is create a PHP file in the commands directory. In that file you will add a class that shares the same name as the file and implements the Clip Command interface. Here is an example using a file named "Example.php":

class Example implements \Clip\Command
    public function run(array $params)
        /* Do command stuff */

    public function help()
        echo 'This is some example help text';

That's it. Now you have a PHP command that can be run via the console. To execute that command all you need to do is open a terminal and type:

$ clip Example

Say you need to pass your command some additional parameters. Those can be passed by just specifying them at the end. They are passed to the run method as the $params array. If the parameters are specified as key=value then $params will be an associative array. For example:

$ clip Example test foo=bar
$params == array('test', 'foo' => 'bar');

Finally one of the best parts about Clip is its powerful configuration class. You can easily create and work with config files. To create a new configuration file all you do is add a new PHP file to the "config" directory. In that file you will return an associative array of parameters. For example you could make a config file called "myconfig.php" which looked like this:

return array(

    'param1' => 'foo',
    'param2' => 'bar'


Now that the config file is setup to access it in your code you would call the config class like so:


Which will return the value 'foo' from the config file. If you want to fetch more than one parameter you can specify them and the config class will return an array of the values like so:

\Clip\Config::myconfig('param1', 'param2');

If you need to fetch an entire config file as an array you would call the config class without any parameters:


Take note that the static function being called exactly matches the name of the config file we created (minus the PHP file extension). That will let you create as many config files as you would like.

That is pretty much all there is to Clip it's very simple and easy to use. Check it out by following the link to the repo below.

Here is the Repo