2010-01-14 09:45:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-01-14 09:45:37 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-01-14 09:45:37 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-01-14 09:45:37 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Console;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Input\StringInput;
|
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput;
|
|
|
|
|
2010-01-14 09:45:37 +00:00
|
|
|
/**
|
|
|
|
* A Shell wraps an Application to add shell capabilities to it.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-06-30 11:55:47 +01:00
|
|
|
* @author Martin Hasoň <martin.hason@gmail.com>
|
2010-01-14 09:45:37 +00:00
|
|
|
*/
|
|
|
|
class Shell
|
|
|
|
{
|
2011-03-11 11:53:42 +00:00
|
|
|
private $application;
|
|
|
|
private $history;
|
|
|
|
private $output;
|
2011-06-30 11:55:47 +01:00
|
|
|
private $hasreadline;
|
|
|
|
private $prompt;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* If there is no readline support for the current PHP executable
|
|
|
|
* a \RuntimeException exception is thrown.
|
|
|
|
*
|
|
|
|
* @param Application $application An application instance
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException When Readline extension is not enabled
|
|
|
|
*/
|
|
|
|
public function __construct(Application $application)
|
2010-01-14 09:45:37 +00:00
|
|
|
{
|
2011-06-30 11:55:47 +01:00
|
|
|
$this->hasreadline = function_exists('readline') ? true : false;
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->application = $application;
|
|
|
|
$this->history = getenv('HOME').'/.history_'.$application->getName();
|
|
|
|
$this->output = new ConsoleOutput();
|
2011-06-30 11:55:47 +01:00
|
|
|
$this->prompt = $application->getName().' > ';
|
2010-01-14 09:45:37 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Runs the shell.
|
|
|
|
*/
|
|
|
|
public function run()
|
2010-01-14 09:45:37 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->application->setAutoExit(false);
|
|
|
|
$this->application->setCatchExceptions(true);
|
2010-01-14 09:45:37 +00:00
|
|
|
|
2011-06-30 11:55:47 +01:00
|
|
|
if ($this->hasreadline) {
|
|
|
|
readline_read_history($this->history);
|
|
|
|
readline_completion_function(array($this, 'autocompleter'));
|
|
|
|
}
|
2010-01-14 09:45:37 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->output->writeln($this->getHeader());
|
2010-05-07 15:09:11 +01:00
|
|
|
while (true) {
|
2011-06-30 11:55:47 +01:00
|
|
|
$command = $this->readline();
|
2010-01-14 09:45:37 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false === $command) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->output->writeln("\n");
|
2010-01-14 09:45:37 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-01-14 09:45:37 +00:00
|
|
|
|
2011-06-30 11:55:47 +01:00
|
|
|
if ($this->hasreadline) {
|
|
|
|
readline_add_history($command);
|
|
|
|
readline_write_history($this->history);
|
|
|
|
}
|
2010-01-14 09:45:37 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (0 !== $ret = $this->application->run(new StringInput($command), $this->output)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
|
|
|
|
}
|
|
|
|
}
|
2010-01-14 09:45:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-30 11:55:47 +01:00
|
|
|
/**
|
|
|
|
* Returns the shell header.
|
|
|
|
*
|
|
|
|
* @return string The header string
|
|
|
|
*/
|
|
|
|
protected function getHeader()
|
|
|
|
{
|
|
|
|
return <<<EOF
|
|
|
|
|
|
|
|
Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>).
|
|
|
|
|
|
|
|
At the prompt, type <comment>help</comment> for some help,
|
|
|
|
or <comment>list</comment> to get a list available commands.
|
|
|
|
|
|
|
|
To exit the shell, type <comment>^D</comment>.
|
|
|
|
|
|
|
|
EOF;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Tries to return autocompletion for the current entered text.
|
|
|
|
*
|
|
|
|
* @param string $text The last segment of the entered text
|
|
|
|
* @param integer $position The current position
|
|
|
|
*/
|
2011-03-11 11:53:42 +00:00
|
|
|
private function autocompleter($text, $position)
|
2010-01-14 09:45:37 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$info = readline_info();
|
|
|
|
$text = substr($info['line_buffer'], 0, $info['end']);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($info['point'] !== $info['end']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// task name?
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false === strpos($text, ' ') || !$text) {
|
2010-11-23 08:42:19 +00:00
|
|
|
return array_keys($this->application->all());
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// options and arguments?
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-12-27 05:36:46 +00:00
|
|
|
$command = $this->application->find(substr($text, 0, strpos($text, ' ')));
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = array('--help');
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($command->getDefinition()->getOptions() as $option) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$list[] = '--'.$option->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
2010-01-14 09:45:37 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2011-06-30 11:55:47 +01:00
|
|
|
* Reads a single line from standard input.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-06-30 11:55:47 +01:00
|
|
|
* @return string The single line from standard input
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-06-30 11:55:47 +01:00
|
|
|
private function readline()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-06-30 11:55:47 +01:00
|
|
|
$completeLine = '';
|
|
|
|
if ($this->hasreadline) {
|
|
|
|
$completeLine = readline($this->prompt);
|
|
|
|
} else {
|
|
|
|
$this->output->write($this->prompt);
|
|
|
|
while (true) {
|
|
|
|
$line = fgets(STDIN, 1024);
|
|
|
|
if (!$line && strlen($line) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$line = rtrim($line);
|
|
|
|
$completeLine .= $line;
|
|
|
|
if (substr($line,-1) != '#') {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$completeLine = substr($completeLine, 0, -1).PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-14 09:45:37 +00:00
|
|
|
|
2011-06-30 11:55:47 +01:00
|
|
|
return $completeLine;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-14 09:45:37 +00:00
|
|
|
}
|