4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console;
14 use Symfony\Component\Console\Exception\RuntimeException;
15 use Symfony\Component\Console\Input\StringInput;
16 use Symfony\Component\Console\Output\ConsoleOutput;
17 use Symfony\Component\Process\ProcessBuilder;
18 use Symfony\Component\Process\PhpExecutableFinder;
21 * A Shell wraps an Application to add shell capabilities to it.
23 * Support for history and completion only works with a PHP compiled
24 * with readline support (either --with-readline or --with-libedit)
26 * @deprecated since version 2.8, to be removed in 3.0.
28 * @author Fabien Potencier <fabien@symfony.com>
29 * @author Martin HasoĊ <martin.hason@gmail.com>
37 private $processIsolation = false;
42 * If there is no readline support for the current PHP executable
43 * a \RuntimeException exception is thrown.
45 * @param Application $application An application instance
47 public function __construct(Application $application)
49 @trigger_error('The '.__CLASS__.' class is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
51 $this->hasReadline = function_exists('readline');
52 $this->application = $application;
53 $this->history = getenv('HOME').'/.history_'.$application->getName();
54 $this->output = new ConsoleOutput();
62 $this->application->setAutoExit(false);
63 $this->application->setCatchExceptions(true);
65 if ($this->hasReadline) {
66 readline_read_history($this->history);
67 readline_completion_function(array($this, 'autocompleter'));
70 $this->output->writeln($this->getHeader());
72 if ($this->processIsolation) {
73 $finder = new PhpExecutableFinder();
74 $php = $finder->find();
75 $this->output->writeln(<<<'EOF'
76 <info>Running with process isolation, you should consider this:</info>
77 * each command is executed as separate process,
78 * commands don't support interactivity, all params must be passed explicitly,
79 * commands output is not colorized.
86 $command = $this->readline();
88 if (false === $command) {
89 $this->output->writeln("\n");
94 if ($this->hasReadline) {
95 readline_add_history($command);
96 readline_write_history($this->history);
99 if ($this->processIsolation) {
100 $pb = new ProcessBuilder();
104 ->add($_SERVER['argv'][0])
106 ->inheritEnvironmentVariables(true)
110 $output = $this->output;
111 $process->run(function ($type, $data) use ($output) {
112 $output->writeln($data);
115 $ret = $process->getExitCode();
117 $ret = $this->application->run(new StringInput($command), $this->output);
121 $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
127 * Returns the shell header.
129 * @return string The header string
131 protected function getHeader()
135 Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>).
137 At the prompt, type <comment>help</comment> for some help,
138 or <comment>list</comment> to get a list of available commands.
140 To exit the shell, type <comment>^D</comment>.
148 * @return string The prompt
150 protected function getPrompt()
152 // using the formatter here is required when using readline
153 return $this->output->getFormatter()->format($this->application->getName().' > ');
156 protected function getOutput()
158 return $this->output;
161 protected function getApplication()
163 return $this->application;
167 * Tries to return autocompletion for the current entered text.
169 * @param string $text The last segment of the entered text
171 * @return bool|array A list of guessed strings or true
173 private function autocompleter($text)
175 $info = readline_info();
176 $text = substr($info['line_buffer'], 0, $info['end']);
178 if ($info['point'] !== $info['end']) {
183 if (false === strpos($text, ' ') || !$text) {
184 return array_keys($this->application->all());
187 // options and arguments?
189 $command = $this->application->find(substr($text, 0, strpos($text, ' ')));
190 } catch (\Exception $e) {
194 $list = array('--help');
195 foreach ($command->getDefinition()->getOptions() as $option) {
196 $list[] = '--'.$option->getName();
203 * Reads a single line from standard input.
205 * @return string The single line from standard input
207 private function readline()
209 if ($this->hasReadline) {
210 $line = readline($this->getPrompt());
212 $this->output->write($this->getPrompt());
213 $line = fgets(STDIN, 1024);
214 $line = (false === $line || '' === $line) ? false : rtrim($line);
220 public function getProcessIsolation()
222 return $this->processIsolation;
225 public function setProcessIsolation($processIsolation)
227 $this->processIsolation = (bool) $processIsolation;
229 if ($this->processIsolation && !class_exists('Symfony\\Component\\Process\\Process')) {
230 throw new RuntimeException('Unable to isolate processes as the Symfony Process Component is not installed.');