On this page
Class ConsoleIo
A wrapper around the various IO operations shell tasks need to do.
Packages up the stdout, stderr, and stdin streams providing a simple consistent interface for shells to use. This class also makes mocking streams easy to do in unit tests.
Constants summary
integer1integer0integer2
Properties summary
-
The error stream
$_errprotected -
The helper registry.
$_helpersprotected -
The input stream
$_inprotected -
$_lastWrittenprotectedintegerThe number of bytes last written to the output stream used when overwriting the previous message.
-
$_levelprotectedThe current output level.integer -
The output stream
$_outprotected
Method Summary
-
__construct() publicConstructor
-
_getInput() protectedPrompts the user for input, and returns it.
-
ask() publicPrompts the user for input, and returns it.
-
askChoice() publicPrompts the user for input based on a list of options, and returns it.
-
err() public
Outputs a single or multiple error messages to stderr. If no parameters are passed outputs just a newline.
-
helper() publicRender a Console Helper
-
hr() publicOutputs a series of minus characters to the standard output, acts as a visual separator.
-
level() publicGet/set the current output level.
-
nl() publicReturns a single or multiple linefeeds sequences.
-
out() public
Outputs a single or multiple messages to stdout. If no parameters are passed outputs just a newline.
-
outputAs() publicChange the output mode of the stdout stream
-
overwrite() publicOverwrite some already output text.
-
quiet() publicOutput at all levels.
-
setLoggers() publicConnects or disconnects the loggers to the console output.
-
styles() publicAdd a new output style or get defined styles.
-
verbose() publicOutput at the verbose level.
Method Detail
__construct()source public
__construct( Cake\Console\ConsoleOutput $out null , Cake\Console\ConsoleOutput $err null , Cake\Console\ConsoleInput $in null , Cake\Console\HelperRegistry $helpers null )
Constructor
Parameters
Cake\Console\ConsoleOutput$outoptional null- A ConsoleOutput object for stdout.
Cake\Console\ConsoleOutput$erroptional null- A ConsoleOutput object for stderr.
Cake\Console\ConsoleInput$inoptional null- A ConsoleInput object for stdin.
Cake\Console\HelperRegistry$helpersoptional null- A HelperRegistry instance
_getInput()source protected
_getInput( string $prompt , string|null $options , string|null $default )
Prompts the user for input, and returns it.
Parameters
-
string
$prompt - Prompt text.
-
string|null
$options - String of options. Pass null to omit.
-
string|null
$default - Default input value. Pass null to omit.
Returns
stringEither the default value, or the user-provided input.
ask()source public
ask( string $prompt , string|null $default null )
Prompts the user for input, and returns it.
Parameters
-
string
$prompt - Prompt text.
-
string|null
$defaultoptional null - Default input value.
Returns
mixedEither the default value, or the user-provided input.
askChoice()source public
askChoice( string $prompt , string|array $options , string|null $default null )
Prompts the user for input based on a list of options, and returns it.
Parameters
-
string
$prompt - Prompt text.
-
string|array
$options - Array or string of options.
-
string|null
$defaultoptional null - Default input value.
Returns
mixedEither the default value, or the user-provided input.
err()source public
err( string|array $message '' , integer $newlines 1 )
Outputs a single or multiple error messages to stderr. If no parameters are passed outputs just a newline.
Parameters
-
string|array
$messageoptional '' - A string or an array of strings to output
-
integer
$newlinesoptional 1 - Number of newlines to append
helper()source public
helper( string $name , array $settings [] )
Render a Console Helper
Create and render the output for a helper object. If the helper object has not already been loaded, it will be loaded and constructed.
Parameters
-
string
$name - The name of the helper to render
-
array
$settingsoptional [] - Configuration data for the helper.
Returns
Cake\Console\HelperThe created helper instance.
hr()source public
hr( integer $newlines 0 , integer $width 79 )
Outputs a series of minus characters to the standard output, acts as a visual separator.
Parameters
-
integer
$newlinesoptional 0 - Number of newlines to pre- and append
-
integer
$widthoptional 79 - Width of the line, defaults to 79
level()source public
level( null|integer $level null )
Get/set the current output level.
Parameters
-
null|integer
$leveloptional null - The current output level.
Returns
integerThe current output level.
nl()source public
nl( integer $multiplier 1 )
Returns a single or multiple linefeeds sequences.
Parameters
-
integer
$multiplieroptional 1 - Number of times the linefeed sequence should be repeated
Returns
stringout()source public
out( string|array $message '' , integer $newlines 1 , integer $level ConsoleIo::NORMAL )
Outputs a single or multiple messages to stdout. If no parameters are passed outputs just a newline.
Output levels
There are 3 built-in output level. Shell::QUIET, Shell::NORMAL, Shell::VERBOSE. The verbose and quiet output levels, map to the verbose and quiet output switches present in most shells. Using Shell::QUIET for a message means it will always display. While using Shell::VERBOSE means it will only display when verbose output is toggled.
Parameters
-
string|array
$messageoptional '' - A string or an array of strings to output
-
integer
$newlinesoptional 1 - Number of newlines to append
-
integer
$leveloptional ConsoleIo::NORMAL - The message's output level, see above.
Returns
integer|booleanReturns the number of bytes returned from writing to stdout.
outputAs()source public
outputAs( integer $mode )
Change the output mode of the stdout stream
Parameters
-
integer
$mode - The output mode.
See
\Cake\Console\ConsoleOutput::outputAs()overwrite()source public
overwrite( array|string $message , integer $newlines 1 , integer|null $size null )
Overwrite some already output text.
Useful for building progress bars, or when you want to replace text already output to the screen with new text.
Warning You cannot overwrite text that contains newlines.
Parameters
-
array|string
$message - The message to output.
-
integer
$newlinesoptional 1 - Number of newlines to append.
-
integer|null
$sizeoptional null -
The number of bytes to overwrite. Defaults to the length of the last message output.
quiet()source public
quiet( string|array $message , integer $newlines 1 )
Output at all levels.
Parameters
-
string|array
$message - A string or an array of strings to output
-
integer
$newlinesoptional 1 - Number of newlines to append
Returns
integer|booleanReturns the number of bytes returned from writing to stdout.
setLoggers()source public
setLoggers( integer|boolean $enable )
Connects or disconnects the loggers to the console output.
Used to enable or disable logging stream output to stdout and stderr If you don't wish all log output in stdout or stderr through Cake's Log class, call this function with $enable=false.
Parameters
-
integer|boolean
$enable -
Use a boolean to enable/toggle all logging. Use one of the verbosity constants (self::VERBOSE, self::QUIET, self::NORMAL) to control logging levels. VERBOSE enables debug logs, NORMAL does not include debug logs, QUIET disables notice, info and debug logs.
styles()source public
styles( string|null $style null , array|boolean|null $definition null )
Add a new output style or get defined styles.
Parameters
-
string|null
$styleoptional null - The style to get or create.
-
array|boolean|null
$definitionoptional null -
The array definition of the style to change or create a style or false to remove a style.
Returns
mixedIf you are getting styles, the style or null will be returned. If you are creating/modifying styles true will be returned.
See
\Cake\Console\ConsoleOutput::styles()verbose()source public
verbose( string|array $message , integer $newlines 1 )
Output at the verbose level.
Parameters
-
string|array
$message - A string or an array of strings to output
-
integer
$newlinesoptional 1 - Number of newlines to append
Returns
integer|booleanReturns the number of bytes returned from writing to stdout.
Properties detail
$_lastWrittensource
protected integer
The number of bytes last written to the output stream used when overwriting the previous message.
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/3.2/class-Cake.Console.ConsoleIo.html