Shell.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Input\StringInput;
  13. use Symfony\Component\Console\Output\ConsoleOutput;
  14. use Symfony\Component\Process\ProcessBuilder;
  15. use Symfony\Component\Process\PhpExecutableFinder;
  16. /**
  17. * A Shell wraps an Application to add shell capabilities to it.
  18. *
  19. * Support for history and completion only works with a PHP compiled
  20. * with readline support (either --with-readline or --with-libedit)
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. * @author Martin Hasoň <martin.hason@gmail.com>
  24. */
  25. class Shell
  26. {
  27. private $application;
  28. private $history;
  29. private $output;
  30. private $hasReadline;
  31. private $prompt;
  32. private $processIsolation;
  33. /**
  34. * Constructor.
  35. *
  36. * If there is no readline support for the current PHP executable
  37. * a \RuntimeException exception is thrown.
  38. *
  39. * @param Application $application An application instance
  40. */
  41. public function __construct(Application $application)
  42. {
  43. $this->hasReadline = function_exists('readline');
  44. $this->application = $application;
  45. $this->history = getenv('HOME').'/.history_'.$application->getName();
  46. $this->output = new ConsoleOutput();
  47. $this->prompt = $application->getName().' > ';
  48. $this->processIsolation = false;
  49. }
  50. /**
  51. * Runs the shell.
  52. */
  53. public function run()
  54. {
  55. $this->application->setAutoExit(false);
  56. $this->application->setCatchExceptions(true);
  57. if ($this->hasReadline) {
  58. readline_read_history($this->history);
  59. readline_completion_function(array($this, 'autocompleter'));
  60. }
  61. $this->output->writeln($this->getHeader());
  62. $php = null;
  63. if ($this->processIsolation) {
  64. $finder = new PhpExecutableFinder();
  65. $php = $finder->find();
  66. $this->output->writeln(<<<EOF
  67. <info>Running with process isolation, you should consider this:</info>
  68. * each command is executed as separate process,
  69. * commands don't support interactivity, all params must be passed explicitly,
  70. * commands output is not colorized.
  71. EOF
  72. );
  73. }
  74. while (true) {
  75. $command = $this->readline();
  76. if (false === $command) {
  77. $this->output->writeln("\n");
  78. break;
  79. }
  80. if ($this->hasReadline) {
  81. readline_add_history($command);
  82. readline_write_history($this->history);
  83. }
  84. if ($this->processIsolation) {
  85. $pb = new ProcessBuilder();
  86. $process = $pb
  87. ->add($php)
  88. ->add($_SERVER['argv'][0])
  89. ->add($command)
  90. ->inheritEnvironmentVariables(true)
  91. ->getProcess()
  92. ;
  93. $output = $this->output;
  94. $process->run(function($type, $data) use ($output) {
  95. $output->writeln($data);
  96. });
  97. $ret = $process->getExitCode();
  98. } else {
  99. $ret = $this->application->run(new StringInput($command), $this->output);
  100. }
  101. if (0 !== $ret) {
  102. $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
  103. }
  104. }
  105. }
  106. /**
  107. * Returns the shell header.
  108. *
  109. * @return string The header string
  110. */
  111. protected function getHeader()
  112. {
  113. return <<<EOF
  114. Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>).
  115. At the prompt, type <comment>help</comment> for some help,
  116. or <comment>list</comment> to get a list of available commands.
  117. To exit the shell, type <comment>^D</comment>.
  118. EOF;
  119. }
  120. /**
  121. * Tries to return autocompletion for the current entered text.
  122. *
  123. * @param string $text The last segment of the entered text
  124. *
  125. * @return Boolean|array A list of guessed strings or true
  126. */
  127. private function autocompleter($text)
  128. {
  129. $info = readline_info();
  130. $text = substr($info['line_buffer'], 0, $info['end']);
  131. if ($info['point'] !== $info['end']) {
  132. return true;
  133. }
  134. // task name?
  135. if (false === strpos($text, ' ') || !$text) {
  136. return array_keys($this->application->all());
  137. }
  138. // options and arguments?
  139. try {
  140. $command = $this->application->find(substr($text, 0, strpos($text, ' ')));
  141. } catch (\Exception $e) {
  142. return true;
  143. }
  144. $list = array('--help');
  145. foreach ($command->getDefinition()->getOptions() as $option) {
  146. $list[] = '--'.$option->getName();
  147. }
  148. return $list;
  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. return $line;
  165. }
  166. public function getProcessIsolation()
  167. {
  168. return $this->processIsolation;
  169. }
  170. public function setProcessIsolation($processIsolation)
  171. {
  172. $this->processIsolation = (Boolean) $processIsolation;
  173. if ($this->processIsolation && !class_exists('Symfony\\Component\\Process\\Process')) {
  174. throw new \RuntimeException('Unable to isolate processes as the Symfony Process Component is not installed.');
  175. }
  176. }
  177. }