Class libraries


toKernel makes writing applications working in command line mode very simple.

For example, if you have the addon "home" with action "cli_welcome", you can write your own addon "my_addon" and run it by using call like:

/usr/bin/php index.php --lng ru --addon my_addon --action my_func --param1 param1_value --param2 param2_value

The cli library is implemented as a singleton class. It parses the command line interface arguments and, through calls to its methods, gives access to parsed data such as the language prefix, the requested addon (controller), action (the addon's method that will be called) and any parameters that may specified in the command line.

File: tokernel.framework/lib/cli.lib.php

Library methods

string action()

Returns requested method name of addon.
In this example the value is: 'my_func' which reads from '--action' argument.

string alias()

Returns alias name.

string get_ct(string $string [, string $fore_color = NULL ][, string $back_color = NULL])

Returns string with colored format.

$my_colored_string = $this->lib->cli->get_ct('Hello!', 'yellow', 'green');

This method returns colored text only in *nix Operating systems.

string id_addon()

Returns addon (controller) name from command line.
In this example the value is: 'my_addon' which reads from '--addon' argument.

string in()

Returns data inserted into command line interactive mode.

echo 'Enter number: '; $number = $this->lib->cli->in(); echo 'The number is: ' . $number;

string language_prefix()

Returns requested language prefix. In this example, value is: 'ru'.
Reads from '--lng' argument.

void out(string $string [, string $fore_color = NULL ][, string $back_color = NULL])

Outputs colored text to command line.

$this->lib->cli->out('Hello!', 'red', 'white');

This method outputs colored text only in *nix Operating systems.

void output_colors()

Output possible colors of string to console.

void output_usage([$show_message = NULL])

This method outputs the simple help info of CLI arguments usage.
The output result looks like:

- | toKernel - Universal PHP Framework v1.2.0 | Copyright (c) 2012 toKernel | | Running in Linux Vostok1961 2.6.32-279.11.1.el6.i686 #1 SMP Tue Oct 16 14:40:53 UTC 2012 i686 - Invalid Command line arguments! Usage: /usr/bin/php /data/www/bankpdf/index.php --addon addon_name --action action_name [--lng language_prefix] [--arg name] [--arg value]

See also: Running an application in CLI mode.

mixed params([$item = NULL])

Returns parameter(s) from command line
for example:

$param1 = $this->lib->cli->params('param1'); // Will return 'param1_value', which reads the value of '--param1' argument.

In other case, if the method called without parameter it will return the array of cli arguments.

$params = $this->lib->cli->params(); /* Will return: array( 'param1' => 'param1_value', 'param2' => 'param2_value' ) */

int params_count()

Returns count of cli arguments.

void set_language_prefix(string $language_prefix)

Manually set language prefix to CLI