dface/tito
Helper class to make a CLI tool to call app services., (*1)
php tito.php <service_name> <method_name> <param1> <param2> ...
, (*2)
It takes a <service>
from a container, calls a method and outputs a result.
To locate a service it needs a service-locator callback., (*3)
Setup
composer require dface/tito
Usage
Let's create example tiny tool to demonstrate the concept., (*4)
Create file named tito.php
with following content. (You can choose any other name), (*5)
``` php
<?php
// please modify this to conform with your class-loading strategy:
include_once 'vendor/autoload.php';, (*6)
// example service class
class Test {, (*7)
function process($val){
return $val;
}
function info(){
return $_SERVER['argv'];
}
}, (*8)
// kinda container
$container = [
'service1' => new Test('service1'),
'service2' => new Test('service2'),
];, (*9)
// Initialize tito - pass in description and service-locator callback:
$tito = new \dface\tito\Tito(
'X-system command line tool.',
function ($service_name) use ($container){
return $container[$service_name];
}
);, (*10)
// ask tito to make the rest
$tito->call();, (*11)
Execute the script from command line. Don't pass any params for now.
`php tito.php`
You'll see the info screen.
X-system command line tool., (*12)
Makes a service method call and outputs a result., (*13)
Usage: php tito.php [options] , (*14)
A can be in the default form:
[ ...], (*15)
or as JSON array, if -j specified:
'["", "" [,]]', (*16)
A result is either:
[true, ] - for successful calls
or
[false, , ] - for failed ones., (*17)
Results are displayed in JSON format unless -p|-y|-l specified., (*18)
Options:
-j passed in JSON format
-p output a result with print_r instead of JSON
-y output a result as YAML instead of JSON
-l output a result as list of lines (values only)
-q quite mode - skip result status 'true' for successful call
-s silent mode - no output for successful calls
-v verbose mode - don't suppress service stdout, don't suppress error_reporting
-t add a stacktrace to failed results
-i input encoding (utf-8 assumed by default)
-b service internal encoding (utf-8 assumed by default)
-o output encoding (input encoding assumed by default)
-d max recursion depth for encoding conversion (default 512)
-x eval specified code before making service call
-e set exit code to '1' for failed calls, (*19)
Now try to use it:
php tito.php service1 process hi
[true,"hi"], (*20)
It outputs a JSON-formatted array of two elements - status and returned value.
Status `true` indicates that a call was successful and a returned value is located in the second element of array.
Status `false` indicates that a call was failed for some reasons. With `false` you'll also get exception type and message:
php tito.php asd process hi
[false,"dface\tito\TitoException","No such service 'asd'"]
```, (*21)