MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
Shell.php
Ir para a documentação deste ficheiro.
1<?php
2
4
8
9/*
10 * This file is part of the Symfony framework.
11 *
12 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
13 *
14 * This source file is subject to the MIT license that is bundled
15 * with this source code in the file LICENSE.
16 */
17
26class Shell
27{
28 protected $application;
29 protected $history;
30 protected $output;
31
43 {
44 if (!function_exists('readline')) {
45 throw new \RuntimeException('Unable to start the shell as the Readline extension is not enabled.');
46 }
47
48 $this->application = $application;
49 $this->history = getenv('HOME').'/.history_'.$application->getName();
50 $this->output = new ConsoleOutput();
51 }
52
56 public function run()
57 {
58 $this->application->setAutoExit(false);
59 $this->application->setCatchExceptions(true);
60
61 readline_read_history($this->history);
62 readline_completion_function(array($this, 'autocompleter'));
63
64 $this->output->writeln($this->getHeader());
65 while (true) {
66 $command = readline($this->application->getName().' > ');
67
68 if (false === $command) {
69 $this->output->writeln("\n");
70
71 break;
72 }
73
74 readline_add_history($command);
75 readline_write_history($this->history);
76
77 if (0 !== $ret = $this->application->run(new StringInput($command), $this->output)) {
78 $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
79 }
80 }
81 }
82
89 protected function autocompleter($text, $position)
90 {
91 $info = readline_info();
92 $text = substr($info['line_buffer'], 0, $info['end']);
93
94 if ($info['point'] !== $info['end']) {
95 return true;
96 }
97
98 // task name?
99 if (false === strpos($text, ' ') || !$text) {
100 return array_keys($this->application->all());
101 }
102
103 // options and arguments?
104 try {
105 $command = $this->application->findCommand(substr($text, 0, strpos($text, ' ')));
106 } catch (\Exception $e) {
107 return true;
108 }
109
110 $list = array('--help');
111 foreach ($command->getDefinition()->getOptions() as $option) {
112 $list[] = '--'.$option->getName();
113 }
114
115 return $list;
116 }
117
123 protected function getHeader()
124 {
125 return <<<EOF
126
127Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>).
128
129At the prompt, type <comment>help</comment> for some help,
130or <comment>list</comment> to get a list available commands.
131
132To exit the shell, type <comment>^D</comment>.
133
134EOF;
135 }
136}
__construct(Application $application)
Definição Shell.php:42
autocompleter($text, $position)
Definição Shell.php:89