PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/RestAPI/vendor/symfony/console/Shell.php

https://gitlab.com/martinstti/silex-microframework-rest
PHP | 233 lines | 194 code | 9 blank | 30 comment | 2 complexity | 265bc3f03cf490991d54d05bfc4b9dc4 MD5 | raw file
  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\Exception\RuntimeException;
  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. * @deprecated since version 2.8, to be removed in 3.0.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. * @author Martin HasoĊˆ <martin.hason@gmail.com>
  26. */
  27. class Shell
  28. {
  29. private $application;
  30. private $history;
  31. private $output;
  32. private $hasReadline;
  33. private $processIsolation = false;
  34. /**
  35. * Constructor.
  36. *
  37. * If there is no readline support for the current PHP executable
  38. * a \RuntimeException exception is thrown.
  39. *
  40. * @param Application $application An application instance
  41. */
  42. public function __construct(Application $application)
  43. {
  44. @trigger_error('The '.__CLASS__.' class is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
  45. $this->hasReadline = function_exists('readline');
  46. $this->application = $application;
  47. $this->history = getenv('HOME').'/.history_'.$application->getName();
  48. $this->output = new ConsoleOutput();
  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. * Renders a prompt.
  122. *
  123. * @return string The prompt
  124. */
  125. protected function getPrompt()
  126. {
  127. // using the formatter here is required when using readline
  128. return $this->output->getFormatter()->format($this->application->getName().' > ');
  129. }
  130. protected function getOutput()
  131. {
  132. return $this->output;
  133. }
  134. protected function getApplication()
  135. {
  136. return $this->application;
  137. }
  138. /**
  139. * Tries to return autocompletion for the current entered text.
  140. *
  141. * @param string $text The last segment of the entered text
  142. *
  143. * @return bool|array A list of guessed strings or true
  144. */
  145. private function autocompleter($text)
  146. {
  147. $info = readline_info();
  148. $text = substr($info['line_buffer'], 0, $info['end']);
  149. if ($info['point'] !== $info['end']) {
  150. return true;
  151. }
  152. // task name?
  153. if (false === strpos($text, ' ') || !$text) {
  154. return array_keys($this->application->all());
  155. }
  156. // options and arguments?
  157. try {
  158. $command = $this->application->find(substr($text, 0, strpos($text, ' ')));
  159. } catch (\Exception $e) {
  160. return true;
  161. }
  162. $list = array('--help');
  163. foreach ($command->getDefinition()->getOptions() as $option) {
  164. $list[] = '--'.$option->getName();
  165. }
  166. return $list;
  167. }
  168. /**
  169. * Reads a single line from standard input.
  170. *
  171. * @return string The single line from standard input
  172. */
  173. private function readline()
  174. {
  175. if ($this->hasReadline) {
  176. $line = readline($this->getPrompt());
  177. } else {
  178. $this->output->write($this->getPrompt());
  179. $line = fgets(STDIN, 1024);
  180. $line = (false === $line || '' === $line) ? false : rtrim($line);
  181. }
  182. return $line;
  183. }
  184. public function getProcessIsolation()
  185. {
  186. return $this->processIsolation;
  187. }
  188. public function setProcessIsolation($processIsolation)
  189. {
  190. $this->processIsolation = (bool) $processIsolation;
  191. if ($this->processIsolation && !class_exists('Symfony\\Component\\Process\\Process')) {
  192. throw new RuntimeException('Unable to isolate processes as the Symfony Process Component is not installed.');
  193. }
  194. }
  195. }