2010-10-03 18:54:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-10-04 01:40:12 +00:00
|
|
|
* ConsoleOutput file.
|
2010-10-03 18:54:41 +00:00
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-10-03 18:54:41 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 18:54:41 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2010-10-03 18:54:41 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-10-03 18:54:41 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2010-10-03 19:41:31 +00:00
|
|
|
/**
|
2011-04-25 19:17:59 +00:00
|
|
|
* Object wrapper for outputting information from a shell application.
|
2010-10-03 19:41:31 +00:00
|
|
|
* Can be connected to any stream resource that can be used with fopen()
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
|
|
|
* Can generate colorized output on consoles that support it. There are a few
|
2010-10-03 19:41:31 +00:00
|
|
|
* built in styles
|
|
|
|
*
|
2010-10-09 15:01:22 +00:00
|
|
|
* - `error` Error messages.
|
|
|
|
* - `warning` Warning messages.
|
|
|
|
* - `info` Informational messages.
|
|
|
|
* - `comment` Additional text.
|
2010-10-24 21:58:44 +00:00
|
|
|
* - `question` Magenta text used for user prompts
|
2010-10-03 19:41:31 +00:00
|
|
|
*
|
|
|
|
* 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.');`
|
|
|
|
*
|
2011-04-25 19:17:59 +00:00
|
|
|
* This would create orange 'Overwrite:' text, while the rest of the text would remain the normal color.
|
2012-12-22 22:48:15 +00:00
|
|
|
* See ConsoleOutput::styles() to learn more about defining your own styles. Nested styles are not supported
|
2010-10-03 22:54:34 +00:00
|
|
|
* at this time.
|
2010-10-03 19:41:31 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console
|
2010-10-03 19:41:31 +00:00
|
|
|
*/
|
2010-10-03 18:54:41 +00:00
|
|
|
class ConsoleOutput {
|
2012-11-28 22:30:47 +00:00
|
|
|
|
2010-10-03 18:54:41 +00:00
|
|
|
/**
|
2010-10-23 03:30:08 +00:00
|
|
|
* Raw output constant - no modification of output text.
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2010-10-03 18:54:41 +00:00
|
|
|
*/
|
2010-10-23 03:30:08 +00:00
|
|
|
const RAW = 0;
|
2010-10-03 18:54:41 +00:00
|
|
|
|
2010-10-03 23:13:48 +00:00
|
|
|
/**
|
2010-10-23 03:30:08 +00:00
|
|
|
* Plain output - tags will be stripped.
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2010-10-03 23:13:48 +00:00
|
|
|
*/
|
2010-10-23 03:30:08 +00:00
|
|
|
const PLAIN = 1;
|
|
|
|
|
|
|
|
/**
|
2011-04-25 19:17:59 +00:00
|
|
|
* Color output - Convert known tags in to ANSI color escape codes.
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2010-10-23 03:30:08 +00:00
|
|
|
*/
|
|
|
|
const COLOR = 2;
|
2010-10-03 23:13:48 +00:00
|
|
|
|
2010-10-03 18:54:41 +00:00
|
|
|
/**
|
|
|
|
* Constant for a newline.
|
2014-02-07 14:45:00 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2010-10-03 18:54:41 +00:00
|
|
|
*/
|
|
|
|
const LF = PHP_EOL;
|
|
|
|
|
2010-10-23 03:30:08 +00:00
|
|
|
/**
|
|
|
|
* File handle for output.
|
|
|
|
*
|
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
protected $_output;
|
|
|
|
|
2015-12-01 12:01:39 +00:00
|
|
|
/**
|
|
|
|
* The number of bytes last written to the output stream
|
|
|
|
* used when overwriting the previous message.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-12-01 14:26:22 +00:00
|
|
|
protected $_lastWritten = 0;
|
2015-12-01 12:01:39 +00:00
|
|
|
|
2010-10-23 03:30:08 +00:00
|
|
|
/**
|
|
|
|
* The current output type. Manipulated with ConsoleOutput::outputAs();
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2010-10-23 03:30:08 +00:00
|
|
|
*/
|
|
|
|
protected $_outputAs = self::COLOR;
|
|
|
|
|
2010-10-03 19:41:31 +00:00
|
|
|
/**
|
2011-04-25 19:17:59 +00:00
|
|
|
* text colors used in colored output.
|
2010-10-03 19:41:31 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_foregroundColors = array(
|
|
|
|
'black' => 30,
|
|
|
|
'red' => 31,
|
|
|
|
'green' => 32,
|
|
|
|
'yellow' => 33,
|
|
|
|
'blue' => 34,
|
|
|
|
'magenta' => 35,
|
|
|
|
'cyan' => 36,
|
|
|
|
'white' => 37
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2011-04-25 19:17:59 +00:00
|
|
|
* background colors used in colored output.
|
2010-10-03 19:41:31 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_backgroundColors = array(
|
|
|
|
'black' => 40,
|
|
|
|
'red' => 41,
|
|
|
|
'green' => 42,
|
|
|
|
'yellow' => 43,
|
|
|
|
'blue' => 44,
|
|
|
|
'magenta' => 45,
|
|
|
|
'cyan' => 46,
|
|
|
|
'white' => 47
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2011-04-25 19:17:59 +00:00
|
|
|
* formatting options for colored output
|
2010-10-03 19:41:31 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $_options = array(
|
|
|
|
'bold' => 1,
|
2010-10-03 23:01:27 +00:00
|
|
|
'underline' => 4,
|
2010-10-03 19:41:31 +00:00
|
|
|
'blink' => 5,
|
|
|
|
'reverse' => 7,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Styles that are available as tags in console output.
|
|
|
|
* You can modify these styles with ConsoleOutput::styles()
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_styles = array(
|
2012-05-24 03:29:59 +00:00
|
|
|
'emergency' => array('text' => 'red', 'underline' => true),
|
|
|
|
'alert' => array('text' => 'red', 'underline' => true),
|
|
|
|
'critical' => array('text' => 'red', 'underline' => true),
|
2010-10-09 15:01:22 +00:00
|
|
|
'error' => array('text' => 'red', 'underline' => true),
|
2010-10-03 19:41:31 +00:00
|
|
|
'warning' => array('text' => 'yellow'),
|
2010-10-04 03:58:07 +00:00
|
|
|
'info' => array('text' => 'cyan'),
|
2012-05-24 03:29:59 +00:00
|
|
|
'debug' => array('text' => 'yellow'),
|
2010-10-09 15:01:22 +00:00
|
|
|
'success' => array('text' => 'green'),
|
2010-10-24 21:58:44 +00:00
|
|
|
'comment' => array('text' => 'blue'),
|
2012-06-04 06:01:22 +00:00
|
|
|
'question' => array('text' => 'magenta'),
|
2013-05-28 11:59:37 +00:00
|
|
|
'notice' => array('text' => 'cyan')
|
2010-10-03 19:41:31 +00:00
|
|
|
);
|
|
|
|
|
2010-10-03 18:54:41 +00:00
|
|
|
/**
|
|
|
|
* Construct the output object.
|
|
|
|
*
|
2015-07-23 03:48:34 +00:00
|
|
|
* Checks for a pretty console environment. Ansicon and ConEmu allows
|
|
|
|
* pretty consoles on Windows, and is supported.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2010-12-22 04:11:23 +00:00
|
|
|
* @param string $stream The identifier of the stream to write output to.
|
2010-10-03 18:54:41 +00:00
|
|
|
*/
|
|
|
|
public function __construct($stream = 'php://stdout') {
|
|
|
|
$this->_output = fopen($stream, 'w');
|
2010-10-03 23:13:48 +00:00
|
|
|
|
2015-07-23 03:48:34 +00:00
|
|
|
if ((DS === '\\' && !(bool)env('ANSICON') && env('ConEmuANSI') !== 'ON') ||
|
2015-01-26 03:24:24 +00:00
|
|
|
$stream === 'php://output' ||
|
2014-09-03 08:47:05 +00:00
|
|
|
(function_exists('posix_isatty') && !posix_isatty($this->_output))
|
|
|
|
) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$this->_outputAs = static::PLAIN;
|
2010-10-03 23:13:48 +00:00
|
|
|
}
|
2010-10-03 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs a single or multiple messages to stdout. If no parameters
|
2010-12-22 04:11:23 +00:00
|
|
|
* are passed, outputs just a newline.
|
2010-10-03 18:54:41 +00:00
|
|
|
*
|
2014-07-30 20:11:03 +00:00
|
|
|
* @param string|array $message A string or an array of strings to output
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $newlines Number of newlines to append
|
|
|
|
* @return int Returns the number of bytes returned from writing to stdout.
|
2010-10-03 18:54:41 +00:00
|
|
|
*/
|
|
|
|
public function write($message, $newlines = 1) {
|
|
|
|
if (is_array($message)) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$message = implode(static::LF, $message);
|
2010-10-03 18:54:41 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
return $this->_write($this->styleText($message . str_repeat(static::LF, $newlines)));
|
2010-10-03 22:54:34 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:01:39 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param array|string $message The message to output.
|
|
|
|
* @param int $newlines Number of newlines to append.
|
2015-12-01 17:09:42 +00:00
|
|
|
* @param int|null $size The number of bytes to overwrite. Defaults to the
|
2015-12-01 12:01:39 +00:00
|
|
|
* length of the last message output.
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-12-01 14:26:22 +00:00
|
|
|
public function overwrite($message, $newlines = 1, $size = null) {
|
|
|
|
$size = $size ?: $this->_lastWritten;
|
|
|
|
// Output backspaces.
|
|
|
|
$this->write(str_repeat("\x08", $size), 0);
|
|
|
|
$newBytes = $this->write($message, 0);
|
|
|
|
// Fill any remaining bytes with spaces.
|
|
|
|
$fill = $size - $newBytes;
|
|
|
|
if ($fill > 0) {
|
|
|
|
$this->write(str_repeat(' ', $fill), 0);
|
|
|
|
}
|
|
|
|
if ($newlines) {
|
|
|
|
$this->write("", $newlines);
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 12:01:39 +00:00
|
|
|
|
2010-10-03 22:54:34 +00:00
|
|
|
/**
|
|
|
|
* Apply styling to text.
|
|
|
|
*
|
|
|
|
* @param string $text Text with styling tags.
|
|
|
|
* @return string String with color codes added.
|
|
|
|
*/
|
|
|
|
public function styleText($text) {
|
2015-07-21 08:22:53 +00:00
|
|
|
if ($this->_outputAs == static::RAW) {
|
2010-10-23 03:30:08 +00:00
|
|
|
return $text;
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
if ($this->_outputAs == static::PLAIN) {
|
|
|
|
$tags = implode('|', array_keys(static::$_styles));
|
2011-11-03 01:50:16 +00:00
|
|
|
return preg_replace('#</?(?:' . $tags . ')>#', '', $text);
|
2010-10-03 23:13:48 +00:00
|
|
|
}
|
2010-10-03 22:54:34 +00:00
|
|
|
return preg_replace_callback(
|
2012-06-02 14:09:58 +00:00
|
|
|
'/<(?P<tag>[a-z0-9-_]+)>(?P<text>.*?)<\/(\1)>/ims', array($this, '_replaceTags'), $text
|
2010-10-03 22:54:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace tags with color codes.
|
|
|
|
*
|
2014-05-28 03:34:53 +00:00
|
|
|
* @param array $matches An array of matches to replace.
|
2010-10-03 22:54:34 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _replaceTags($matches) {
|
|
|
|
$style = $this->styles($matches['tag']);
|
|
|
|
if (empty($style)) {
|
2010-10-21 03:40:05 +00:00
|
|
|
return '<' . $matches['tag'] . '>' . $matches['text'] . '</' . $matches['tag'] . '>';
|
2010-10-03 22:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$styleInfo = array();
|
2015-07-21 08:22:53 +00:00
|
|
|
if (!empty($style['text']) && isset(static::$_foregroundColors[$style['text']])) {
|
|
|
|
$styleInfo[] = static::$_foregroundColors[$style['text']];
|
2010-10-03 22:54:34 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
if (!empty($style['background']) && isset(static::$_backgroundColors[$style['background']])) {
|
|
|
|
$styleInfo[] = static::$_backgroundColors[$style['background']];
|
2010-10-03 22:54:34 +00:00
|
|
|
}
|
|
|
|
unset($style['text'], $style['background']);
|
|
|
|
foreach ($style as $option => $value) {
|
|
|
|
if ($value) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$styleInfo[] = static::$_options[$option];
|
2010-10-03 22:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "\033[" . implode($styleInfo, ';') . 'm' . $matches['text'] . "\033[0m";
|
2010-10-03 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-22 04:11:23 +00:00
|
|
|
* Writes a message to the output stream.
|
2010-10-03 18:54:41 +00:00
|
|
|
*
|
|
|
|
* @param string $message Message to write.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool success
|
2010-10-03 18:54:41 +00:00
|
|
|
*/
|
|
|
|
protected function _write($message) {
|
2015-12-01 12:01:39 +00:00
|
|
|
$this->_lastWritten = fwrite($this->_output, $message);
|
2015-12-01 14:26:22 +00:00
|
|
|
return $this->_lastWritten;
|
2010-10-03 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2010-10-03 19:41:31 +00:00
|
|
|
/**
|
|
|
|
* Get the current styles offered, or append new ones in.
|
|
|
|
*
|
2010-10-03 22:54:34 +00:00
|
|
|
* ### Get a style definition
|
|
|
|
*
|
|
|
|
* `$this->output->styles('error');`
|
|
|
|
*
|
|
|
|
* ### Get all the style definitions
|
|
|
|
*
|
|
|
|
* `$this->output->styles();`
|
|
|
|
*
|
|
|
|
* ### Create or modify an existing style
|
|
|
|
*
|
|
|
|
* `$this->output->styles('annoy', array('text' => 'purple', 'background' => 'yellow', 'blink' => true));`
|
|
|
|
*
|
|
|
|
* ### Remove a style
|
|
|
|
*
|
|
|
|
* `$this->output->styles('annoy', false);`
|
|
|
|
*
|
2010-10-03 19:41:31 +00:00
|
|
|
* @param string $style The style to get or create.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array $definition The array definition of the style to change or create a style
|
2010-10-03 19:41:31 +00:00
|
|
|
* or false to remove a style.
|
2010-10-03 22:54:34 +00:00
|
|
|
* @return mixed If you are getting styles, the style or null will be returned. If you are creating/modifying
|
|
|
|
* styles true will be returned.
|
2010-10-03 19:41:31 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function styles($style = null, $definition = null) {
|
2010-10-03 22:54:34 +00:00
|
|
|
if ($style === null && $definition === null) {
|
2015-07-21 08:22:53 +00:00
|
|
|
return static::$_styles;
|
2010-10-03 22:54:34 +00:00
|
|
|
}
|
2010-10-03 19:41:31 +00:00
|
|
|
if (is_string($style) && $definition === null) {
|
2015-07-21 08:22:53 +00:00
|
|
|
return isset(static::$_styles[$style]) ? static::$_styles[$style] : null;
|
2010-10-03 19:41:31 +00:00
|
|
|
}
|
|
|
|
if ($definition === false) {
|
2015-07-21 08:22:53 +00:00
|
|
|
unset(static::$_styles[$style]);
|
2010-10-03 19:41:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_styles[$style] = $definition;
|
2010-10-03 22:54:34 +00:00
|
|
|
return true;
|
2010-10-03 19:41:31 +00:00
|
|
|
}
|
|
|
|
|
2010-10-23 03:30:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Get/Set the output type to use. The output type how formatting tags are treated.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $type The output type to use. Should be one of the class constants.
|
2010-10-23 03:30:08 +00:00
|
|
|
* @return mixed Either null or the value if getting.
|
|
|
|
*/
|
|
|
|
public function outputAs($type = null) {
|
|
|
|
if ($type === null) {
|
|
|
|
return $this->_outputAs;
|
|
|
|
}
|
|
|
|
$this->_outputAs = $type;
|
|
|
|
}
|
|
|
|
|
2010-10-03 18:54:41 +00:00
|
|
|
/**
|
2013-09-08 19:19:46 +00:00
|
|
|
* Clean up and close handles
|
2010-10-03 18:54:41 +00:00
|
|
|
*/
|
|
|
|
public function __destruct() {
|
Fix closing non resource
I get a load of these errors when running tests in the shell, this check stops the errors from happening
Warning: 2 :: fclose() expects parameter 1 to be resource, integer given on line 298 of CORE\Cake\Console\ConsoleOutput.php
Trace:
fclose - [internal], line ??
ConsoleOutput::__destruct() - CORE\Cake\Console\ConsoleOutput.php, line 298
ToolbarComponent::_saveState() - APP\Plugin\DebugKit\Controller\Component\ToolbarComponent.php, line 307
ToolbarComponent::beforeRedirect() - APP\Plugin\DebugKit\Controller\Component\ToolbarComponent.php, line 307
ObjectCollection::trigger() - CORE\Cake\Utility\ObjectCollection.php, line 132
call_user_func - [internal], line ??
CakeEventManager::dispatch() - CORE\Cake\Event\CakeEventManager.php, line 247
Controller::redirect() - CORE\Cake\Controller\Controller.php, line 765
AuthComponent::_unauthenticated() - CORE\Cake\Controller\Component\AuthComponent.php, line 364
AuthComponent::startup() - CORE\Cake\Controller\Component\AuthComponent.php, line 304
ObjectCollection::trigger() - CORE\Cake\Utility\ObjectCollection.php, line 132
call_user_func - [internal], line ??
CakeEventManager::dispatch() - CORE\Cake\Event\CakeEventManager.php, line 247
Controller::startupProcess() - CORE\Cake\Controller\Controller.php, line 675
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 182
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 160
2014-04-14 13:15:46 +00:00
|
|
|
if (is_resource($this->_output)) {
|
|
|
|
fclose($this->_output);
|
|
|
|
}
|
2010-10-03 18:54:41 +00:00
|
|
|
}
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2011-07-26 01:46:52 +00:00
|
|
|
}
|