CLI
extends Base
in package
Class CLI
Your commandline script should inherit from this class and implement the abstract methods.
Tags
Table of Contents
Properties
- $colors : Colors
- $bin : string
- $logdefault : string
- $loglevel : array<string|int, mixed>
- $options : Options
Methods
- __construct() : mixed
- constructor
- alert() : mixed
- Action must be taken immediately.
- critical() : mixed
- Critical conditions.
- debug() : mixed
- Detailed debug information.
- emergency() : void
- System is unusable.
- error() : mixed
- Runtime errors that do not require immediate action but should typically be logged and monitored.
- fatal() : mixed
- Exits the program on a fatal error
- info() : mixed
- Interesting events.
- isLogLevelEnabled() : bool
- Check if a message with the given level should be logged
- log() : mixed
- notice() : mixed
- Normal but significant events.
- run() : mixed
- Execute the CLI program
- setLogLevel() : mixed
- Set the current log level
- success() : mixed
- Normal, positive outcome (This is not a PSR-3 level)
- warning() : mixed
- Exceptional occurrences that are not errors.
- checkArguments() : mixed
- Wrapper around the argument checking
- execute() : mixed
- Wrapper around main
- handleDefaultOptions() : mixed
- Handle the default options
- interpolate() : string
- Interpolates context values into the message placeholders.
- logMessage() : mixed
- main() : void
- Your main program
- parseOptions() : mixed
- Wrapper around the option parsing
- registerDefaultOptions() : mixed
- Add the default help, color and log options
- setup() : void
- Register options and arguments on the given $options object
- setupLogging() : mixed
- Handle the logging options
Properties
$colors
public
Colors
$colors
$bin
protected
string
$bin
the executed script itself
$logdefault
protected
string
$logdefault
= 'info'
default log level
$loglevel
protected
array<string|int, mixed>
$loglevel
= array('debug' => array('icon' => '', 'color' => \splitbrain\phpcli\Colors::C_RESET, 'channel' => STDOUT, 'enabled' => true), 'info' => array('icon' => 'ℹ ', 'color' => \splitbrain\phpcli\Colors::C_CYAN, 'channel' => STDOUT, 'enabled' => true), 'notice' => array('icon' => '☛ ', 'color' => \splitbrain\phpcli\Colors::C_CYAN, 'channel' => STDOUT, 'enabled' => true), 'success' => array('icon' => '✓ ', 'color' => \splitbrain\phpcli\Colors::C_GREEN, 'channel' => STDOUT, 'enabled' => true), 'warning' => array('icon' => '⚠ ', 'color' => \splitbrain\phpcli\Colors::C_BROWN, 'channel' => STDERR, 'enabled' => true), 'error' => array('icon' => '✗ ', 'color' => \splitbrain\phpcli\Colors::C_RED, 'channel' => STDERR, 'enabled' => true), 'critical' => array('icon' => '☠ ', 'color' => \splitbrain\phpcli\Colors::C_LIGHTRED, 'channel' => STDERR, 'enabled' => true), 'alert' => array('icon' => '✖ ', 'color' => \splitbrain\phpcli\Colors::C_LIGHTRED, 'channel' => STDERR, 'enabled' => true), 'emergency' => array('icon' => '✘ ', 'color' => \splitbrain\phpcli\Colors::C_LIGHTRED, 'channel' => STDERR, 'enabled' => true))
PSR-3 compatible loglevels and their prefix, color, output channel, enabled status
$options
protected
Options
$options
the option parser
Methods
__construct()
constructor
public
__construct([bool $autocatch = true ]) : mixed
Initialize the arguments, set up helper classes and set up the CLI environment
Parameters
- $autocatch : bool = true
-
should exceptions be catched and handled automatically?
alert()
Action must be taken immediately.
public
alert(string $message[, array<string|int, mixed> $context = array() ]) : mixed
Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
Parameters
- $message : string
- $context : array<string|int, mixed> = array()
critical()
Critical conditions.
public
critical(string $message[, array<string|int, mixed> $context = array() ]) : mixed
Example: Application component unavailable, unexpected exception.
Parameters
- $message : string
- $context : array<string|int, mixed> = array()
debug()
Detailed debug information.
public
debug(string $message[, array<string|int, mixed> $context = array() ]) : mixed
Parameters
- $message : string
- $context : array<string|int, mixed> = array()
emergency()
System is unusable.
public
emergency(string $message[, array<string|int, mixed> $context = array() ]) : void
Parameters
- $message : string
- $context : array<string|int, mixed> = array()
error()
Runtime errors that do not require immediate action but should typically be logged and monitored.
public
error(string $message[, array<string|int, mixed> $context = array() ]) : mixed
Parameters
- $message : string
- $context : array<string|int, mixed> = array()
fatal()
Exits the program on a fatal error
public
fatal(Exception|string $error[, array<string|int, mixed> $context = array() ]) : mixed
Parameters
- $error : Exception|string
-
either an exception or an error message
- $context : array<string|int, mixed> = array()
info()
Interesting events.
public
info(string $message[, array<string|int, mixed> $context = array() ]) : mixed
Example: User logs in, SQL logs.
Parameters
- $message : string
- $context : array<string|int, mixed> = array()
isLogLevelEnabled()
Check if a message with the given level should be logged
public
isLogLevelEnabled(string $level) : bool
Parameters
- $level : string
Return values
boollog()
public
log(string $level, string $message[, array<string|int, mixed> $context = array() ]) : mixed
Parameters
- $level : string
- $message : string
- $context : array<string|int, mixed> = array()
notice()
Normal but significant events.
public
notice(string $message[, array<string|int, mixed> $context = array() ]) : mixed
Parameters
- $message : string
- $context : array<string|int, mixed> = array()
run()
Execute the CLI program
public
run() : mixed
Executes the setup() routine, adds default options, initiate the options parsing and argument checking and finally executes main() - Each part is split into their own protected function below, so behaviour can easily be overwritten
Tags
setLogLevel()
Set the current log level
public
setLogLevel(string $level) : mixed
Parameters
- $level : string
success()
Normal, positive outcome (This is not a PSR-3 level)
public
success(string $string[, array<string|int, mixed> $context = array() ]) : mixed
Parameters
- $string : string
- $context : array<string|int, mixed> = array()
warning()
Exceptional occurrences that are not errors.
public
warning(string $message[, array<string|int, mixed> $context = array() ]) : mixed
Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
Parameters
- $message : string
- $context : array<string|int, mixed> = array()
checkArguments()
Wrapper around the argument checking
protected
checkArguments() : mixed
execute()
Wrapper around main
protected
execute() : mixed
handleDefaultOptions()
Handle the default options
protected
handleDefaultOptions() : mixed
interpolate()
Interpolates context values into the message placeholders.
protected
interpolate(mixed $message[, array<string|int, mixed> $context = array() ]) : string
Parameters
- $message : mixed
- $context : array<string|int, mixed> = array()
Return values
stringlogMessage()
protected
logMessage(string $level, string $message[, array<string|int, mixed> $context = array() ]) : mixed
Parameters
- $level : string
- $message : string
- $context : array<string|int, mixed> = array()
main()
Your main program
protected
abstract main(Options $options) : void
Arguments and options have been parsed when this is run
Parameters
- $options : Options
Tags
parseOptions()
Wrapper around the option parsing
protected
parseOptions() : mixed
registerDefaultOptions()
Add the default help, color and log options
protected
registerDefaultOptions() : mixed
setup()
Register options and arguments on the given $options object
protected
abstract setup(Options $options) : void
Parameters
- $options : Options
Tags
setupLogging()
Handle the logging options
protected
setupLogging() : mixed