Documentation

Base
in package

AbstractYes

Class CLIBase

All base functionality is implemented here.

Your commandline should not inherit from this class, but from one of the CLI classes

Tags
author

Andreas Gohr andi@splitbrain.org

license

MIT

Table of Contents

Properties

$colors  : Colors
$bin  : string
$logdefault  : string
$loglevel  : array<string|int, mixed>
$options  : Options

Methods

__construct()  : mixed
constructor
fatal()  : mixed
Exits the program on a fatal error
isLogLevelEnabled()  : bool
Check if a message with the given level should be logged
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)
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

$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

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?

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()

isLogLevelEnabled()

Check if a message with the given level should be logged

public isLogLevelEnabled(string $level) : bool
Parameters
$level : string
Return values
bool

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
throws
Exception

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()

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
string

logMessage()

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
throws
Exception

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
throws
Exception

setupLogging()

Handle the logging options

protected setupLogging() : mixed

        
On this page

Search results