annotate vendor/zendframework/zend-stdlib/src/ConsoleHelper.php @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents
children
rev   line source
Chris@2 1 <?php
Chris@2 2 /**
Chris@2 3 * @link http://github.com/zendframework/zend-stdlib for the canonical source repository
Chris@2 4 * @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
Chris@2 5 * @license http://framework.zend.com/license/new-bsd New BSD License
Chris@2 6 */
Chris@2 7
Chris@2 8 namespace Zend\Stdlib;
Chris@2 9
Chris@2 10 /**
Chris@2 11 * Utilities for console tooling.
Chris@2 12 *
Chris@2 13 * Provides the following facilities:
Chris@2 14 *
Chris@2 15 * - Colorize strings using markup (e.g., `<info>message</info>`,
Chris@2 16 * `<error>message</error>`)
Chris@2 17 * - Write output to a specified stream, optionally with colorization.
Chris@2 18 * - Write a line of output to a specified stream, optionally with
Chris@2 19 * colorization, using the system EOL sequence..
Chris@2 20 * - Write an error message to STDERR.
Chris@2 21 *
Chris@2 22 * Colorization will only occur when expected sequences are discovered, and
Chris@2 23 * then, only if the console terminal allows it.
Chris@2 24 *
Chris@2 25 * Essentially, provides the bare minimum to allow you to provide messages to
Chris@2 26 * the current console.
Chris@2 27 */
Chris@2 28 class ConsoleHelper
Chris@2 29 {
Chris@2 30 const COLOR_GREEN = "\033[32m";
Chris@2 31 const COLOR_RED = "\033[31m";
Chris@2 32 const COLOR_RESET = "\033[0m";
Chris@2 33
Chris@2 34 const HIGHLIGHT_INFO = 'info';
Chris@2 35 const HIGHLIGHT_ERROR = 'error';
Chris@2 36
Chris@2 37 private $highlightMap = [
Chris@2 38 self::HIGHLIGHT_INFO => self::COLOR_GREEN,
Chris@2 39 self::HIGHLIGHT_ERROR => self::COLOR_RED,
Chris@2 40 ];
Chris@2 41
Chris@2 42 /**
Chris@2 43 * @var string Exists only for testing.
Chris@2 44 */
Chris@2 45 private $eol = PHP_EOL;
Chris@2 46
Chris@2 47 /**
Chris@2 48 * @var resource Exists only for testing.
Chris@2 49 */
Chris@2 50 private $stderr = STDERR;
Chris@2 51
Chris@2 52 /**
Chris@2 53 * @var bool
Chris@2 54 */
Chris@2 55 private $supportsColor;
Chris@2 56
Chris@2 57 /**
Chris@2 58 * @param resource $resource
Chris@2 59 */
Chris@2 60 public function __construct($resource = STDOUT)
Chris@2 61 {
Chris@2 62 $this->supportsColor = $this->detectColorCapabilities($resource);
Chris@2 63 }
Chris@2 64
Chris@2 65 /**
Chris@2 66 * Colorize a string for use with the terminal.
Chris@2 67 *
Chris@2 68 * Takes strings formatted as `<key>string</key>` and formats them per the
Chris@2 69 * $highlightMap; if color support is disabled, simply removes the formatting
Chris@2 70 * tags.
Chris@2 71 *
Chris@2 72 * @param string $string
Chris@2 73 * @return string
Chris@2 74 */
Chris@2 75 public function colorize($string)
Chris@2 76 {
Chris@2 77 $reset = $this->supportsColor ? self::COLOR_RESET : '';
Chris@2 78 foreach ($this->highlightMap as $key => $color) {
Chris@2 79 $pattern = sprintf('#<%s>(.*?)</%s>#s', $key, $key);
Chris@2 80 $color = $this->supportsColor ? $color : '';
Chris@2 81 $string = preg_replace($pattern, $color . '$1' . $reset, $string);
Chris@2 82 }
Chris@2 83 return $string;
Chris@2 84 }
Chris@2 85
Chris@2 86 /**
Chris@2 87 * @param string $string
Chris@2 88 * @param bool $colorize Whether or not to colorize the string
Chris@2 89 * @param resource $resource Defaults to STDOUT
Chris@2 90 * @return void
Chris@2 91 */
Chris@2 92 public function write($string, $colorize = true, $resource = STDOUT)
Chris@2 93 {
Chris@2 94 if ($colorize) {
Chris@2 95 $string = $this->colorize($string);
Chris@2 96 }
Chris@2 97
Chris@2 98 $string = $this->formatNewlines($string);
Chris@2 99
Chris@2 100 fwrite($resource, $string);
Chris@2 101 }
Chris@2 102
Chris@2 103 /**
Chris@2 104 * @param string $string
Chris@2 105 * @param bool $colorize Whether or not to colorize the line
Chris@2 106 * @param resource $resource Defaults to STDOUT
Chris@2 107 * @return void
Chris@2 108 */
Chris@2 109 public function writeLine($string, $colorize = true, $resource = STDOUT)
Chris@2 110 {
Chris@2 111 $this->write($string . $this->eol, $colorize, $resource);
Chris@2 112 }
Chris@2 113
Chris@2 114 /**
Chris@2 115 * Emit an error message.
Chris@2 116 *
Chris@2 117 * Wraps the message in `<error></error>`, and passes it to `writeLine()`,
Chris@2 118 * using STDERR as the resource; emits an additional empty line when done,
Chris@2 119 * also to STDERR.
Chris@2 120 *
Chris@2 121 * @param string $message
Chris@2 122 * @return void
Chris@2 123 */
Chris@2 124 public function writeErrorMessage($message)
Chris@2 125 {
Chris@2 126 $this->writeLine(sprintf('<error>%s</error>', $message), true, $this->stderr);
Chris@2 127 $this->writeLine('', false, $this->stderr);
Chris@2 128 }
Chris@2 129
Chris@2 130 /**
Chris@2 131 * @param resource $resource
Chris@2 132 * @return bool
Chris@2 133 */
Chris@2 134 private function detectColorCapabilities($resource = STDOUT)
Chris@2 135 {
Chris@2 136 if ('\\' === DIRECTORY_SEPARATOR) {
Chris@2 137 // Windows
Chris@2 138 return false !== getenv('ANSICON')
Chris@2 139 || 'ON' === getenv('ConEmuANSI')
Chris@2 140 || 'xterm' === getenv('TERM');
Chris@2 141 }
Chris@2 142
Chris@2 143 return function_exists('posix_isatty') && posix_isatty($resource);
Chris@2 144 }
Chris@2 145
Chris@2 146 /**
Chris@2 147 * Ensure newlines are appropriate for the current terminal.
Chris@2 148 *
Chris@2 149 * @param string
Chris@2 150 * @return string
Chris@2 151 */
Chris@2 152 private function formatNewlines($string)
Chris@2 153 {
Chris@2 154 $string = str_replace($this->eol, "\0PHP_EOL\0", $string);
Chris@2 155 $string = preg_replace("/(\r\n|\n|\r)/", $this->eol, $string);
Chris@2 156 return str_replace("\0PHP_EOL\0", $this->eol, $string);
Chris@2 157 }
Chris@2 158 }