|
| 1 | +# Console Helper |
| 2 | + |
| 3 | +Writing one-off scripts or vendor binaries for a package is often problematic: |
| 4 | + |
| 5 | +- You need to parse arguments manually. |
| 6 | +- You need to send output to the console in a meaningful fashion: |
| 7 | + - Using `STDOUT` for meaningful, expected output |
| 8 | + - Using `STDERR` for error messages |
| 9 | + - Ensuring any line breaks are converted to `PHP_EOL` |
| 10 | + - Optionally, using console colors to provide context, which means: |
| 11 | + - Detecting whether or not the console supports colors in the first place |
| 12 | + - Providing appropriate escape sequences to produce color |
| 13 | + |
| 14 | +`Zend\Stdlib\ConsoleHelper` helps to address the second major bullet point and |
| 15 | +all beneath it in a minimal fashion. |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Typical usage is to instantiate a `ConsoleHelper`, and call one of its methods: |
| 20 | + |
| 21 | +```php |
| 22 | +use Zend\Stdlib\ConsoleHelper; |
| 23 | + |
| 24 | +$helper = new ConsoleHelper(); |
| 25 | +$helper->writeLine('This is output'); |
| 26 | +``` |
| 27 | + |
| 28 | +You can optionally pass a PHP stream resource to the constructor, which will be |
| 29 | +used to determine whether or not color support is available: |
| 30 | + |
| 31 | +```php |
| 32 | +$helper = new ConsoleHelper($stream); |
| 33 | +``` |
| 34 | + |
| 35 | +By default, it assumes `STDOUT`, and tests against that. |
| 36 | + |
| 37 | +## Available methods |
| 38 | + |
| 39 | +`ConsoleHelper` provides the following methods. |
| 40 | + |
| 41 | +### colorize |
| 42 | + |
| 43 | +- `colorize(string $string) : string` |
| 44 | + |
| 45 | +`colorize()` accepts a formatted string, and will then apply ANSI color |
| 46 | +sequences to them, if color support is detected. |
| 47 | + |
| 48 | +The following sequences are currently supported: |
| 49 | + |
| 50 | +- `<info>...</info>` will apply a green color sequence around the provided text. |
| 51 | +- `<error>...</error>` will apply a red color sequence around the provided text. |
| 52 | + |
| 53 | +You may mix multiple sequences within the same stream. |
| 54 | + |
| 55 | +### write |
| 56 | + |
| 57 | +- `write(string $string, bool $colorize = true, resource $stream = STDOUT) : void` |
| 58 | + |
| 59 | +Emits the provided `$string` to the provided `$stream` (which defaults to |
| 60 | +`STDOUT` if not provided). Any EOL sequences are convered to `PHP_EOL`. If |
| 61 | +`$colorize` is `true`, the string is first passed to `colorize()` as well. |
| 62 | + |
| 63 | +### writeline |
| 64 | + |
| 65 | +- `writeLine(string $string, bool $colorize = true, resource $stream = STDOUT) : void` |
| 66 | + |
| 67 | +Same as `write()`, except it also appends a `PHP_EOL` sequence to the `$string`. |
| 68 | + |
| 69 | +### writeErrorMessage |
| 70 | + |
| 71 | +- `writeErrorMessage(string $message)` |
| 72 | + |
| 73 | +Wraps `$message` in an `<error></error>` sequence, and passes it to |
| 74 | +`writeLine()`, using `STDERR` as the `$stream`. |
| 75 | + |
| 76 | +## Example |
| 77 | + |
| 78 | +Below is an example class that accepts an argument list, and determines how and |
| 79 | +what to emit. |
| 80 | + |
| 81 | +```php |
| 82 | +namespace Foo; |
| 83 | + |
| 84 | +use Zend\Stdlib\ConsoleHelper; |
| 85 | + |
| 86 | +class HelloWorld |
| 87 | +{ |
| 88 | + private $helper; |
| 89 | + |
| 90 | + public function __construct(ConsoleHelper $helper = null) |
| 91 | + { |
| 92 | + $this->helper = $helper ?: new ConsoleHelper(); |
| 93 | + } |
| 94 | + |
| 95 | + public function __invoke(array $args) |
| 96 | + { |
| 97 | + if (! count($args)) { |
| 98 | + $this->helper->writeErrorMessage('Missing arguments!'); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (count($args) > 1) { |
| 103 | + $this->helper->writeErrorMessage('Too many arguments!'); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + $target = array_shift($args); |
| 108 | + |
| 109 | + $this->helper->writeLine(sprintf( |
| 110 | + '<info>Hello</info> %s', |
| 111 | + $target |
| 112 | + )); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## When to upgrade |
| 118 | + |
| 119 | +`ConsoleHelper` is deliberately simple, and assumes that your primary need for |
| 120 | +console tooling is for output considerations. |
| 121 | + |
| 122 | +If you need to parse complex argument strings, we recommend using |
| 123 | +[zend-console](https://docs.zendframework.com/zend-console/)/[zf-console](https://github.com/zfcampus/zf-console) |
| 124 | +or [symfony/console](http://symfony.com/doc/current/components/console.html), |
| 125 | +as these packages provide those capabilities, as well as far more colorization |
| 126 | +and console feature detection facilities. |
0 commit comments