ConsoleOutput
in package
Object wrapper for outputting information from a shell application.
Can be connected to any stream resource that can be used with fopen()
Can generate colorized output on consoles that support it. There are a few built in styles
-
error
Error messages. -
warning
Warning messages. -
info
Informational messages. -
comment
Additional text. -
question
Magenta text used for user prompts
By defining styles with addStyle() you can create custom console styles.
Using styles in output
You can format console output using tags with the name of the style to apply. From inside a shell object
$this->out('<warning>Overwrite:</warning> foo.php was overwritten.');
This would create orange 'Overwrite:' text, while the rest of the text would remain the normal color. See ConsoleOutput::styles() to learn more about defining your own styles. Nested styles are not supported at this time.
Table of Contents
Constants
- COLOR = 2
- Color output - Convert known tags in to ANSI color escape codes.
- LF = PHP_EOL
- Constant for a newline.
- PLAIN = 1
- Plain output - tags will be stripped.
- RAW = 0
- Raw output constant - no modification of output text.
Properties
- $_backgroundColors : array<string, int>
- background colors used in colored output.
- $_foregroundColors : array<string, int>
- text colors used in colored output.
- $_options : array<string, int>
- Formatting options for colored output.
- $_output : resource
- File handle for output.
- $_outputAs : int
- The current output type.
- $_styles : array<string, array<string|int, mixed>>
- Styles that are available as tags in console output.
Methods
- __construct() : mixed
- Construct the output object.
- __destruct() : mixed
- Clean up and close handles
- getOutputAs() : int
- Get the output type on how formatting tags are treated.
- getStyle() : array<string|int, mixed>
- Gets the current styles offered
- setOutputAs() : void
- Set the output type on how formatting tags are treated.
- setStyle() : void
- Sets style.
- styles() : array<string, mixed>
- Gets all the style definitions.
- styleText() : string
- Apply styling to text.
- write() : int
- Outputs a single or multiple messages to stdout or stderr. If no parameters are passed, outputs just a newline.
- _replaceTags() : string
- Replace tags with color codes.
- _write() : int
- Writes a message to the output stream.
Constants
COLOR
Color output - Convert known tags in to ANSI color escape codes.
public
int
COLOR
= 2
LF
Constant for a newline.
public
string
LF
= PHP_EOL
PLAIN
Plain output - tags will be stripped.
public
int
PLAIN
= 1
RAW
Raw output constant - no modification of output text.
public
int
RAW
= 0
Properties
$_backgroundColors
background colors used in colored output.
protected
static array<string, int>
$_backgroundColors
= ['black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47]
$_foregroundColors
text colors used in colored output.
protected
static array<string, int>
$_foregroundColors
= ['black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37]
$_options
Formatting options for colored output.
protected
static array<string, int>
$_options
= ['bold' => 1, 'underline' => 4, 'blink' => 5, 'reverse' => 7]
$_output
File handle for output.
protected
resource
$_output
$_outputAs
The current output type.
protected
int
$_outputAs
= self::COLOR
Tags
$_styles
Styles that are available as tags in console output.
protected
static array<string, array<string|int, mixed>>
$_styles
= ['emergency' => ['text' => 'red'], 'alert' => ['text' => 'red'], 'critical' => ['text' => 'red'], 'error' => ['text' => 'red'], 'warning' => ['text' => 'yellow'], 'info' => ['text' => 'cyan'], 'debug' => ['text' => 'yellow'], 'success' => ['text' => 'green'], 'comment' => ['text' => 'blue'], 'question' => ['text' => 'magenta'], 'notice' => ['text' => 'cyan']]
You can modify these styles with ConsoleOutput::styles()
Methods
__construct()
Construct the output object.
public
__construct([string|resource $stream = 'php://stdout' ]) : mixed
Checks for a pretty console environment. Ansicon and ConEmu allows pretty consoles on Windows, and is supported.
Parameters
- $stream : string|resource = 'php://stdout'
-
The identifier of the stream to write output to.
Tags
__destruct()
Clean up and close handles
public
__destruct() : mixed
getOutputAs()
Get the output type on how formatting tags are treated.
public
getOutputAs() : int
Return values
intgetStyle()
Gets the current styles offered
public
getStyle(string $style) : array<string|int, mixed>
Parameters
- $style : string
-
The style to get.
Return values
array<string|int, mixed> —The style or empty array.
setOutputAs()
Set the output type on how formatting tags are treated.
public
setOutputAs(int $type) : void
Parameters
- $type : int
-
The output type to use. Should be one of the class constants.
Tags
setStyle()
Sets style.
public
setStyle(string $style, array<string|int, mixed> $definition) : void
Creates or modifies an existing style.
$output->setStyle('annoy', ['text' => 'purple', 'background' => 'yellow', 'blink' => true]);
Remove a style
$this->output->setStyle('annoy', []);
Parameters
- $style : string
-
The style to set.
- $definition : array<string|int, mixed>
-
The array definition of the style to change or create..
styles()
Gets all the style definitions.
public
styles() : array<string, mixed>
Return values
array<string, mixed>styleText()
Apply styling to text.
public
styleText(string $text) : string
Parameters
- $text : string
-
Text with styling tags.
Return values
string —String with color codes added.
write()
Outputs a single or multiple messages to stdout or stderr. If no parameters are passed, outputs just a newline.
public
write(array<string|int, string>|string $message[, int $newlines = 1 ]) : int
Parameters
- $message : array<string|int, string>|string
-
A string or an array of strings to output
- $newlines : int = 1
-
Number of newlines to append
Return values
int —The number of bytes returned from writing to output.
_replaceTags()
Replace tags with color codes.
protected
_replaceTags(array<string, string> $matches) : string
Parameters
- $matches : array<string, string>
-
An array of matches to replace.
Return values
string_write()
Writes a message to the output stream.
protected
_write(string $message) : int
Parameters
- $message : string
-
Message to write.
Return values
int —The number of bytes returned from writing to output.