|
| 1 | +<?php |
| 2 | + |
| 3 | +use Clue\React\Stdio\Stdio; |
| 4 | +use Clue\Arguments; |
| 5 | +use Clue\Commander\Router; |
| 6 | +use Clue\Commander\NoRouteFoundException; |
| 7 | + |
| 8 | +require __DIR__ . '/../vendor/autoload.php'; |
| 9 | + |
| 10 | +$loop = React\EventLoop\Factory::create(); |
| 11 | + |
| 12 | +$stdio = new Stdio($loop); |
| 13 | +$readline = $stdio->getReadline(); |
| 14 | +$readline->setPrompt('> '); |
| 15 | + |
| 16 | +// limit history to HISTSIZE env |
| 17 | +$limit = getenv('HISTSIZE'); |
| 18 | +if ($limit === '' || $limit < 0) { |
| 19 | + // empty string or negative value means unlimited |
| 20 | + $readline->limitHistory(null); |
| 21 | +} elseif ($limit !== false) { |
| 22 | + // apply any other value if given |
| 23 | + $readline->limitHistory($limit); |
| 24 | +} |
| 25 | + |
| 26 | +// register all available commands and their arguments |
| 27 | +$router = new Router(); |
| 28 | +$router->add('exit | quit', function() use ($stdio) { |
| 29 | + $stdio->end(); |
| 30 | +}); |
| 31 | +$router->add('help', function () use ($stdio) { |
| 32 | + $stdio->writeln('Use TAB-completion or use "exit"'); |
| 33 | +}); |
| 34 | +$router->add('(echo | print) <words>...', function (array $args) use ($stdio) { |
| 35 | + $stdio->writeln(implode(' ', $args['words'])); |
| 36 | +}); |
| 37 | +$router->add('printf <format> <args>...', function (array $args) use ($stdio) { |
| 38 | + $stdio->writeln(vsprintf($args['format'],$args['args'])); |
| 39 | +}); |
| 40 | + |
| 41 | +// autocomplete the following commands (at offset=0/1 only) |
| 42 | +$readline->setAutocomplete(function ($_, $offset) { |
| 43 | + return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf'); |
| 44 | +}); |
| 45 | + |
| 46 | +$stdio->writeln('Welcome to this interactive demo'); |
| 47 | + |
| 48 | +// react to commands the user entered |
| 49 | +$stdio->on('line', function ($line) use ($router, $stdio, $readline) { |
| 50 | + // add all lines from input to history |
| 51 | + // skip empty line and duplicate of previous line |
| 52 | + $all = $readline->listHistory(); |
| 53 | + if (trim($line) !== '' && $line !== end($all)) { |
| 54 | + $readline->addHistory($line); |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + $args = Arguments\split($line); |
| 59 | + } catch (Arguments\UnclosedQuotesException $e) { |
| 60 | + $stdio->writeln('Error: Invalid command syntax (unclosed quotes)'); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // skip empty lines |
| 65 | + if (!$args) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + $router->handleArgs($args); |
| 71 | + } catch (NoRouteFoundException $e) { |
| 72 | + $stdio->writeln('Error: Invalid command usage'); |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +$loop->run(); |
0 commit comments