2017 © Pedro Peláez
 

library obj2cli

Create Command Line Interfaces from PHP Objects

image

flsouto/obj2cli

Create Command Line Interfaces from PHP Objects

  • Monday, October 24, 2016
  • by flsouto
  • Repository
  • 0 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

obj2cli

Overview

This library allows you to create an interactive shell application from a plain php object. Commands are routed to instance methods, and command arguments are passed over as method arguments., (*1)

Installation

You can simply download obj2cli.php file to your project's folder or install it via composer:, (*2)

composer require flsouto/obj2cli

Notice: in both cases you will have to include the file manually, since it will not be autoloaded!, (*3)

Usage

Let's say we want to create an app that has two available commands:, (*4)

  • say_hello - prints "hello" to stdout
  • say - which takes a parameter and prints it

This is how you could implement it using obj2cli:, (*5)

<?php // my_app.php

require_once('obj2cli.php');

class MyApp{

    function say_hello(){
        echo "Hello!";
    }

    function say($what){
        echo $what;
    }

}

obj2cli(new MyApp());

That's all! Save it as my_app.php and run it through the command line:, (*6)

$ php my_app.php

A new session will start with the name of your app (which by default is the name of your object's class):, (*7)

MyApp>

Now let's play around with it and see if it works as expected:, (*8)

MyApp> say_hello
hello
MyApp> say Cool!
Cool!

Notice: command arguments are separated by space., (*9)

Optional parameters

If you provide a default value to a parameter it will work as expected:, (*10)

<?php 
require_once('obj2cli.php');

class MyApp{

    function generate_file_name($name, $ext='txt'){
        echo $name.'.'.$ext;
    }

}

obj2cli(new MyApp());

$ php my_app.php
MyApp> generate_file_name cache 
cache.txt
MyApp> generate_file_name cache json
cache.json
MyApp> ^C

Especial Commands

help

The help command, if not defined in your class, will list all methods/commands available:, (*11)

MyApp> help
- say_hello (no parameters)
- say <what>
- generate_file_name <name> [ext = txt]

command --help

Shows usage of one specific command:, (*12)

MyApp> generate_file_name --help
generate_file_name <name> [ext = txt]

exit

Exists the app., (*13)

Notice: this is not the same as hitting CTRL+C. The exit command returns to the parent context (see below) while CTRL+C exists the entire app., (*14)

Creating child contexts

If a command returns another object, control will be passed to that object. See an example:, (*15)

<?php
require_once('obj2cli.php');

class MyApp{

    function multiply($number){
        return new Multiply($number);
    }

}

class Multiply{

    var $number;

    function __construct($number){
        $this->number = $number;
    }

    function by($number2){
        echo $this->number * $number2;
    }

}

obj2cli(new MyApp());
$ php my_app.php 
MyApp> multiply 3

Multiply> by 5
15
Multiply> by 6
18
Multiply> by 7
21
Multiply> exit
MyApp> 

Notice how the "exit" command finished the "Multiply" context and brought us back to the "MyApp" parent context. CTRL+C would have closed both contexts., (*16)

Naming contexts

While the example above worked, it would be nice to customize the name of the "Multiply" context so that it read "Multiply 3...>". By default, the object's class name is used as name for the context, but this can be changed by implementing a method called getObj2cliName:, (*17)

// ...
class Multiply{

    var $number;

    function __construct($number){
        $this->number = $number;
    }

    // Customize name of context
    function getObj2cliName(){
        return "Multiply $this->number....";
    }

    function by($number2){
        echo $this->number * $number2;
    }

}
//...

Running any class from the command line

This repository comes with a script called "run.php" which allows you to instantiate any class into an interactive shell program:, (*18)

$ php run.php /path/to/file.php MyClass arg1 arg2
MyClass> 

If the class you want to work with is autoloaded by composer's autoloader, you should provide the path to vendor/autoload.php:, (*19)

$ php run.php /path/to/vendor/autoload.php SomeClass arg1 arg2
SomeClass>

Final thoughts

This is a useful tool for building interactive shell programs very quickly and also can be used to test/debug classes on the fly., (*20)

The Versions

24/10 2016

dev-master

9999999-dev

Create Command Line Interfaces from PHP Objects

  Sources   Download

by Fabio Souto

24/10 2016

1.0.2

1.0.2.0

Create Command Line Interfaces from PHP Objects

  Sources   Download

by Fabio Souto

24/10 2016

1.0.1

1.0.1.0

Create Command Line Interfaces from PHP Objects

  Sources   Download

by Fabio Souto

24/10 2016

1.0.0

1.0.0.0

Create Command Line Interfaces from PHP Objects

  Sources   Download

by Fabio Souto