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\Application;
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 * @author Fabien Potencier <fabien@symfony.com>
27 * @author Martin Hasoň <martin.hason@gmail.com>
36 private $processIsolation;
41 * If there is no readline support for the current PHP executable
42 * a \RuntimeException exception is thrown.
44 * @param Application $application An application instance
46 public function __construct(Application $application)
48 $this->hasReadline = function_exists('readline');
49 $this->application = $application;
50 $this->history = getenv('HOME').'/.history_'.$application->getName();
51 $this->output = new ConsoleOutput();
52 $this->prompt = $application->getName().' > ';
53 $this->processIsolation = false;
61 $this->application->setAutoExit(false);
62 $this->application->setCatchExceptions(true);
64 if ($this->hasReadline) {
65 readline_read_history($this->history);
66 readline_completion_function(array($this, 'autocompleter'));
69 $this->output->writeln($this->getHeader());
71 if ($this->processIsolation) {
72 $finder = new PhpExecutableFinder();
73 $php = $finder->find();
74 $this->output->writeln(<<<EOF
75 <info>Running with process isolation, you should consider this:</info>
76 * each command is executed as separate process,
77 * commands don't support interactivity, all params must be passed explicitly,
78 * commands output is not colorized.
85 $command = $this->readline();
87 if (false === $command) {
88 $this->output->writeln("\n");
93 if ($this->hasReadline) {
94 readline_add_history($command);
95 readline_write_history($this->history);
98 if ($this->processIsolation) {
99 $pb = new ProcessBuilder();
103 ->add($_SERVER['argv'][0])
105 ->inheritEnvironmentVariables(true)
109 $output = $this->output;
110 $process->run(function($type, $data) use ($output) {
111 $output->writeln($data);
114 $ret = $process->getExitCode();
116 $ret = $this->application->run(new StringInput($command), $this->output);
120 $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
126 * Returns the shell header.
128 * @return string The header string
130 protected function getHeader()
134 Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>).
136 At the prompt, type <comment>help</comment> for some help,
137 or <comment>list</comment> to get a list of available commands.
139 To exit the shell, type <comment>^D</comment>.
145 * Tries to return autocompletion for the current entered text.
147 * @param string $text The last segment of the entered text
149 * @return Boolean|array A list of guessed strings or true
151 private function autocompleter($text)
153 $info = readline_info();
154 $text = substr($info['line_buffer'], 0, $info['end']);
156 if ($info['point'] !== $info['end']) {
161 if (false === strpos($text, ' ') || !$text) {
162 return array_keys($this->application->all());
165 // options and arguments?
167 $command = $this->application->find(substr($text, 0, strpos($text, ' ')));
168 } catch (\Exception $e) {
172 $list = array('--help');
173 foreach ($command->getDefinition()->getOptions() as $option) {
174 $list[] = '--'.$option->getName();
181 * Reads a single line from standard input.
183 * @return string The single line from standard input
185 private function readline()
187 if ($this->hasReadline) {
188 $line = readline($this->prompt);
190 $this->output->write($this->prompt);
191 $line = fgets(STDIN, 1024);
192 $line = (!$line && strlen($line) == 0) ? false : rtrim($line);
198 public function getProcessIsolation()
200 return $this->processIsolation;
203 public function setProcessIsolation($processIsolation)
205 $this->processIsolation = (Boolean) $processIsolation;