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:

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

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:

\Clip\Config::myconfig();

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

2 comments:

  1. Hello,

    We're seeing a lot of hits from this domain to our website :

    http://www.nzg.club/

    What is your interest in sending traffic our way?

    Regards,

    -Security

    ReplyDelete
  2. The ClipAir for snoring is a very soft, high quality little clip that is inserted in the nose to increase airflow.  It will:
    1. Improve quality of sleep by reducing or eliminate snoring sounds
    2. Improve your mood
    3. Improve the mood of your partner
    4. Prevent you from being sent to the couch by that same partner
    Now the exact solution has been found to get rid of this disease. ClipAir Nasal Dilator is one such device that can be easily fitted in the nose to get rid of snoring all night. There is no need to do any kind of surgery to apply it in the nose. This clip can be easily fitted to the nose. This clip is very soft and comfortable, by applying it, there will be no pain in your nose. Its price is also not much. you can buy ClipAir Nasal Dilator from our website as we are authorised dealer available in Canada & USA. If you or your partner has this type of disease, you can buy it from here
    anti snoring device
    snoring remedies
    nasal dilator
    clip air nasal dilator
    anti snoring solution
    anti snoring magnetic
    snoring nose clip
    anti snoring devices walmart canada
    nasal strips for snoring
    nose strips for snoring
    snoring device
    how to stop snoring
    snoring solutions
    what causes snoring
    snoring mouth gaurd
    how to stop snoring immediately
    snoring treatment
    stop snoring

    ReplyDelete