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