|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCR\Shell\Console\Application; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Application; |
| 6 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 7 | +use Symfony\Component\Process\ProcessBuilder; |
| 8 | +use Symfony\Component\Process\PhpExecutableFinder; |
| 9 | +use Symfony\Component\Console\Shell as BaseShell; |
| 10 | +use PHPCR\Shell\Console\Input\StringInput; |
| 11 | + |
| 12 | +class Shell |
| 13 | +{ |
| 14 | + private $application; |
| 15 | + private $history; |
| 16 | + private $output; |
| 17 | + private $hasReadline; |
| 18 | + private $prompt; |
| 19 | + private $processIsolation; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructor. |
| 23 | + * |
| 24 | + * If there is no readline support for the current PHP executable |
| 25 | + * a \RuntimeException exception is thrown. |
| 26 | + * |
| 27 | + * @param Application $application An application instance |
| 28 | + */ |
| 29 | + public function __construct(Application $application) |
| 30 | + { |
| 31 | + $this->hasReadline = function_exists('readline'); |
| 32 | + $this->application = $application; |
| 33 | + $this->history = getenv('HOME').'/.history_'.$application->getName(); |
| 34 | + $this->output = new ConsoleOutput(); |
| 35 | + $this->prompt = $application->getName().' > '; |
| 36 | + $this->processIsolation = false; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Runs the shell. |
| 41 | + */ |
| 42 | + public function run() |
| 43 | + { |
| 44 | + $this->application->setAutoExit(false); |
| 45 | + $this->application->setCatchExceptions(true); |
| 46 | + |
| 47 | + if ($this->hasReadline) { |
| 48 | + readline_read_history($this->history); |
| 49 | + readline_completion_function(array($this, 'autocompleter')); |
| 50 | + } |
| 51 | + |
| 52 | + $this->output->writeln($this->getHeader()); |
| 53 | + |
| 54 | + while (true) { |
| 55 | + $command = $this->readline(); |
| 56 | + |
| 57 | + if (false === $command) { |
| 58 | + $this->output->writeln("\n"); |
| 59 | + |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + if ($this->hasReadline) { |
| 64 | + readline_add_history($command); |
| 65 | + readline_write_history($this->history); |
| 66 | + } |
| 67 | + |
| 68 | + if ($this->processIsolation) { |
| 69 | + $pb = new ProcessBuilder(); |
| 70 | + |
| 71 | + $process = $pb |
| 72 | + ->add($php) |
| 73 | + ->add($_SERVER['argv'][0]) |
| 74 | + ->add($command) |
| 75 | + ->inheritEnvironmentVariables(true) |
| 76 | + ->getProcess() |
| 77 | + ; |
| 78 | + |
| 79 | + $output = $this->output; |
| 80 | + $process->run(function($type, $data) use ($output) { |
| 81 | + $output->writeln($data); |
| 82 | + }); |
| 83 | + |
| 84 | + $ret = $process->getExitCode(); |
| 85 | + } else { |
| 86 | + $ret = $this->application->run(new StringInput($command), $this->output); |
| 87 | + } |
| 88 | + |
| 89 | + if (0 !== $ret) { |
| 90 | + $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret)); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns the shell header. |
| 97 | + * |
| 98 | + * @return string The header string |
| 99 | + */ |
| 100 | + protected function getHeader() |
| 101 | + { |
| 102 | + return <<<EOF |
| 103 | +
|
| 104 | +Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>). |
| 105 | +
|
| 106 | +At the prompt, type <comment>help</comment> for some help, |
| 107 | +or <comment>list</comment> to get a list of available commands. |
| 108 | +
|
| 109 | +To exit the shell, type <comment>^D</comment>. |
| 110 | +
|
| 111 | +EOF; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Tries to return autocompletion for the current entered text. |
| 116 | + * |
| 117 | + * @param string $text The last segment of the entered text |
| 118 | + * |
| 119 | + * @return Boolean|array A list of guessed strings or true |
| 120 | + */ |
| 121 | + private function autocompleter($text) |
| 122 | + { |
| 123 | + $info = readline_info(); |
| 124 | + $text = substr($info['line_buffer'], 0, $info['end']); |
| 125 | + |
| 126 | + if ($info['point'] !== $info['end']) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + // task name? |
| 131 | + if (false === strpos($text, ' ') || !$text) { |
| 132 | + return array_keys($this->application->all()); |
| 133 | + } |
| 134 | + |
| 135 | + // options and arguments? |
| 136 | + try { |
| 137 | + $command = $this->application->find(substr($text, 0, strpos($text, ' '))); |
| 138 | + } catch (\Exception $e) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + $list = array('--help'); |
| 143 | + foreach ($command->getDefinition()->getOptions() as $option) { |
| 144 | + $list[] = '--'.$option->getName(); |
| 145 | + } |
| 146 | + |
| 147 | + return $list; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Reads a single line from standard input. |
| 152 | + * |
| 153 | + * @return string The single line from standard input |
| 154 | + */ |
| 155 | + private function readline() |
| 156 | + { |
| 157 | + if ($this->hasReadline) { |
| 158 | + $line = readline($this->prompt); |
| 159 | + } else { |
| 160 | + $this->output->write($this->prompt); |
| 161 | + $line = fgets(STDIN, 1024); |
| 162 | + $line = (!$line && strlen($line) == 0) ? false : rtrim($line); |
| 163 | + } |
| 164 | + |
| 165 | + return $line; |
| 166 | + } |
| 167 | + |
| 168 | + public function getProcessIsolation() |
| 169 | + { |
| 170 | + return $this->processIsolation; |
| 171 | + } |
| 172 | + |
| 173 | + public function setProcessIsolation($processIsolation) |
| 174 | + { |
| 175 | + $this->processIsolation = (Boolean) $processIsolation; |
| 176 | + } |
| 177 | +} |
| 178 | + |
0 commit comments