Cli
in package
Class for registering functions to be called from the command line
function test_echo_fn($data) {
Cli::success("Params:");
var_dump ($data);
}
Cli::set('test_echo', 'test_echo_fn');
Opening the shell inside the phpBridge folder
$ php cli.php test_echo foo bar
Table of Contents
Properties
- $last_error : string
- Last error message from CLI operations
Methods
- callFunction() : bool
- Calls a registered function
- drawTable() : mixed
- Draws a formatted table in the console
- echo() : mixed
- Prints a message on the console
- error() : mixed
- Prints an error message in red on the console
- get_all_fn() : array<string|int, mixed>
- Returns the names of all registered functions
- get_last_error() : string
- is_cli() : bool
- Checks if the code is running from the command line
- run() : bool
- Executes the function passed as an argument
- set() : bool
- Registers a function
- success() : mixed
- Prints a success message in green on the console
Properties
$last_error
Last error message from CLI operations
public
static string
$last_error
= ''
Methods
callFunction()
Calls a registered function
public
static callFunction(string $name, array<string|int, mixed> ...$args) : bool
Parameters
- $name : string
-
Function name
- $args : array<string|int, mixed>
-
Arguments to pass to the function
Return values
bool —True if the function was called successfully, false otherwise
drawTable()
Draws a formatted table in the console
public
static drawTable(array<string|int, mixed> $data[, array<string|int, mixed> $columns = null ]) : mixed
Parameters
- $data : array<string|int, mixed>
-
Data to display in the table
- $columns : array<string|int, mixed> = null
-
Column definitions (optional)
echo()
Prints a message on the console
public
static echo(string $msg) : mixed
Parameters
- $msg : string
-
Message to print
error()
Prints an error message in red on the console
public
static error(string $msg) : mixed
Parameters
- $msg : string
-
Message to print
get_all_fn()
Returns the names of all registered functions
public
static get_all_fn() : array<string|int, mixed>
Return values
array<string|int, mixed> —Array of function names
get_last_error()
public
static get_last_error() : string
Return values
string —The last error message
is_cli()
Checks if the code is running from the command line
public
static is_cli() : bool
Return values
bool —True if running from CLI, false otherwise
run()
Executes the function passed as an argument
public
static run(array<string|int, mixed> $argv) : bool
Parameters
- $argv : array<string|int, mixed>
-
Arguments from command line
Return values
bool —True if the function was executed successfully, false otherwise
set()
Registers a function
public
static set(string $name, callable $function) : bool
Parameters
- $name : string
-
Function name
- $function : callable
-
Function to register
Return values
bool —True if the function was registered successfully, false otherwise
success()
Prints a success message in green on the console
public
static success(string $msg) : mixed
Parameters
- $msg : string
-
Message to print