Shell.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * @return Boolean|array A list of guessed strings or true
  125. */
  126. private function autocompleter($text)
  127. {
  128. $info = readline_info();
  129. $text = substr($info['line_buffer'], 0, $info['end']);
  130. if ($info['point'] !== $info['end']) {
  131. return true;
  132. }
  133. // task name?
  134. if (false === strpos($text, ' ') || !$text) {
  135. return array_keys($this->application->all());
  136. }
  137. // options and arguments?
  138. try {
  139. $command = $this->application->find(substr($text, 0, strpos($text, ' ')));
  140. } catch (\Exception $e) {
  141. return true;
  142. }
  143. $list = array('--help');
  144. foreach ($command->getDefinition()->getOptions() as $option) {
  145. $list[] = '--'.$option->getName();
  146. }
  147. return $list;
  148. }
  149. /**
  150. * Reads a single line from standard input.
  151. *
  152. * @return string The single line from standard input
  153. */
  154. private function readline()
  155. {
  156. if ($this->hasReadline) {
  157. $line = readline($this->prompt);
  158. } else {
  159. $this->output->write($this->prompt);
  160. $line = fgets(STDIN, 1024);
  161. $line = (!$line && strlen($line) == 0) ? false : rtrim($line);
  162. }
  163. return $line;
  164. }
  165. public function getProcessIsolation()
  166. {
  167. return $this->processIsolation;
  168. }
  169. public function setProcessIsolation($processIsolation)
  170. {
  171. $this->processIsolation = (Boolean) $processIsolation;
  172. }
  173. }