Horde_Cli
in package
Horde_Cli API for basic command-line functionality/checks.
Tags
Table of Contents
- $_clearscreen : string
- The string to use for clearing the screen.
- $_color : Horde_Cli_Color
- The color formatter.
- $_indent : string
- The indent string to use.
- $_newline : string
- The newline string to use.
- $_output : resource
- The output pipe.
- $_space : string
- The formatted space string to use.
- $_width : int
- The terminal width.
- __construct() : mixed
- Detect the current environment (web server or console) and sets internal values accordingly.
- block() : string
- Formats text in a visual block with optional margin.
- blue() : string
- Returns a blue version of $text.
- bold() : string
- Returns a bold version of $text.
- clearScreen() : mixed
- Clears the entire screen, if possible.
- color() : string
- Returns a colored version of $text.
- fatal() : mixed
- Displays a fatal error message.
- getWidth() : int
- Retuns the detected terminal screen width.
- green() : string
- Returns a green version of $text.
- header() : mixed
- Creates a header from a string by drawing character lines above or below the header content.
- indent() : string
- Returns the indented string.
- init() : Horde_Cli
- CLI scripts shouldn't timeout, so try to set the time limit to none. Also initialize a few variables in $_SERVER that aren't present from the CLI.
- message() : mixed
- Displays a message.
- passwordPrompt() : string
- Interactively prompts for input without echoing to the terminal.
- prompt() : mixed
- Prompts for a user response.
- readStdin() : string
- Reads everything that is sent through standard input and returns it as a single string.
- red() : string
- Returns a red version of $text.
- runningFromCLI() : bool
- Make sure we're being called from the command line, and not via the web.
- shutdown() : mixed
- Destroys any session on script end.
- writeln() : mixed
- Prints $text on a single line.
- yellow() : string
- Returns a yellow version of $text.
- _addLines() : string
- Adds content to a line buffer, wrapping long lines if necessary.
- _detectPager() : mixed
- Detects available pagers (less, more) and creates pipes to output through them.
- _setWidth() : mixed
- Detects the terminal screen width.
Properties
$_clearscreen
The string to use for clearing the screen.
protected
string
$_clearscreen
= ''
$_color
The color formatter.
protected
Horde_Cli_Color
$_color
$_indent
The indent string to use.
protected
string
$_indent
$_newline
The newline string to use.
protected
string
$_newline
$_output
The output pipe.
protected
resource
$_output
$_space
The formatted space string to use.
protected
string
$_space
$_width
The terminal width.
protected
int
$_width
Methods
__construct()
Detect the current environment (web server or console) and sets internal values accordingly.
public
__construct([array<string|int, mixed> $opts = array() ]) : mixed
Use init() if you also want to set environment variables that may be missing in a CLI environment.
Parameters
- $opts : array<string|int, mixed> = array()
-
Configuration options:
- 'output': (resource, optional) Open pipe to write the output to.
- 'pager': (boolean) Pipe output through a pager? @since 2.3.0
Return values
mixed —block()
Formats text in a visual block with optional margin.
public
block(string $text, string $color[, string $margin = '' ][, int $width = null ]) : string
Parameters
- $text : string
-
The block text.
- $color : string
-
The background color.
- $margin : string = ''
-
The block margin string.
- $width : int = null
-
The block width.
Tags
Return values
string —The formatted block.
blue()
Returns a blue version of $text.
public
blue(string $text) : string
Parameters
- $text : string
-
The text to print in blue.
Tags
Return values
string —The blue text.
bold()
Returns a bold version of $text.
public
bold(string $text) : string
Parameters
- $text : string
-
The text to bold.
Tags
Return values
string —The bolded text.
clearScreen()
Clears the entire screen, if possible.
public
clearScreen() : mixed
Return values
mixed —color()
Returns a colored version of $text.
public
color(string $color, string $text) : string
Parameters
- $color : string
-
The color to use. @see $_foregroundColors
- $text : string
-
The text to print in this color.
Tags
Return values
string —The colored text.
fatal()
Displays a fatal error message.
public
fatal(mixed $error) : mixed
Parameters
- $error : mixed
-
The error text to display, an exception or an object with a getMessage() method.
Return values
mixed —getWidth()
Retuns the detected terminal screen width.
public
getWidth() : int
Defaults to 80 if the width cannot be detected automatically.
Tags
Return values
int —The terminal screen width or null if not a terminal.
green()
Returns a green version of $text.
public
green(string $text) : string
Parameters
- $text : string
-
The text to print in green.
Tags
Return values
string —The green text.
header()
Creates a header from a string by drawing character lines above or below the header content.
public
header(string $message[, string $below = '-' ][, string $above = null ]) : mixed
Parameters
- $message : string
-
A message to turn into a header.
- $below : string = '-'
-
Character to use for drawing the line below the message.
- $above : string = null
-
Character to use for drawing the line above the message.
Tags
Return values
mixed —indent()
Returns the indented string.
public
indent(string $text) : string
Parameters
- $text : string
-
The text to indent.
Return values
string —The indented text.
init()
CLI scripts shouldn't timeout, so try to set the time limit to none. Also initialize a few variables in $_SERVER that aren't present from the CLI.
public
static init([array<string|int, mixed> $opts = array() ]) : Horde_Cli
Parameters
- $opts : array<string|int, mixed> = array()
-
Configuration options:
- 'pager': (boolean) Pipe output through a pager? @since 2.3.0
Return values
Horde_Cli —A Horde_Cli instance.
message()
Displays a message.
public
message(mixed $message[, string $type = 'cli.message' ]) : mixed
Parameters
- $message : mixed
- $type : string = 'cli.message'
-
The type of message: 'cli.error', 'cli.warning', 'cli.success', or 'cli.message'.
Return values
mixed —passwordPrompt()
Interactively prompts for input without echoing to the terminal.
public
passwordPrompt(string $prompt) : string
Requires a bash shell or Windows and won't work with safe_mode settings (uses shell_exec).
From: http://www.sitepoint.com/blogs/2009/05/01/interactive-cli-password-prompt-in-php/
Parameters
- $prompt : string
-
The message to display when prompting the user.
Return values
string —The user's response to the prompt.
prompt()
Prompts for a user response.
public
prompt(string $prompt[, array<string|int, mixed> $choices = null ][, string $default = null ]) : mixed
Parameters
- $prompt : string
-
The message to display when prompting the user.
- $choices : array<string|int, mixed> = null
-
The choices available to the user or null for a text input.
- $default : string = null
-
The default value if no value specified.
Tags
Return values
mixed —The user's response to the prompt.
readStdin()
Reads everything that is sent through standard input and returns it as a single string.
public
readStdin() : string
Return values
string —The contents of the standard input.
red()
Returns a red version of $text.
public
red(string $text) : string
Parameters
- $text : string
-
The text to print in red.
Tags
Return values
string —The red text.
runningFromCLI()
Make sure we're being called from the command line, and not via the web.
public
static runningFromCLI() : bool
Return values
bool —True if we are, false otherwise.
shutdown()
Destroys any session on script end.
public
shutdown() : mixed
Tags
Return values
mixed —writeln()
Prints $text on a single line.
public
writeln([string $text = '' ][, bool $pre = false ]) : mixed
Parameters
- $text : string = ''
-
The text to print.
- $pre : bool = false
-
If true the linebreak is printed before the text instead of after it.
Return values
mixed —yellow()
Returns a yellow version of $text.
public
yellow(string $text) : string
Parameters
- $text : string
-
The text to print in yellow.
Tags
Return values
string —The yellow text.
_addLines()
Adds content to a line buffer, wrapping long lines if necessary.
protected
_addLines(array<string|int, mixed> $buffer, string $content) : string
Parameters
- $buffer : array<string|int, mixed>
-
The line buffer.
- $content : string
-
The lines to add.
Return values
string —The updated line buffer.
_detectPager()
Detects available pagers (less, more) and creates pipes to output through them.
protected
_detectPager() : mixed
Return values
mixed —_setWidth()
Detects the terminal screen width.
protected
_setWidth() : mixed