[Console] added support for interactive shell without readline extension

This commit is contained in:
Martin Hason 2011-06-30 12:55:47 +02:00
parent a724774fc0
commit a5ff6352c5

View File

@ -18,16 +18,16 @@ use Symfony\Component\Console\Output\ConsoleOutput;
/**
* A Shell wraps an Application to add shell capabilities to it.
*
* This class only works with a PHP compiled with readline support
* (either --with-readline or --with-libedit)
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Martin Hasoň <martin.hason@gmail.com>
*/
class Shell
{
private $application;
private $history;
private $output;
private $hasreadline;
private $prompt;
/**
* Constructor.
@ -41,13 +41,11 @@ class Shell
*/
public function __construct(Application $application)
{
if (!function_exists('readline')) {
throw new \RuntimeException('Unable to start the shell as the Readline extension is not enabled.');
}
$this->hasreadline = function_exists('readline') ? true : false;
$this->application = $application;
$this->history = getenv('HOME').'/.history_'.$application->getName();
$this->output = new ConsoleOutput();
$this->prompt = $application->getName().' > ';
}
/**
@ -58,12 +56,14 @@ class Shell
$this->application->setAutoExit(false);
$this->application->setCatchExceptions(true);
readline_read_history($this->history);
readline_completion_function(array($this, 'autocompleter'));
if ($this->hasreadline) {
readline_read_history($this->history);
readline_completion_function(array($this, 'autocompleter'));
}
$this->output->writeln($this->getHeader());
while (true) {
$command = readline($this->application->getName().' > ');
$command = $this->readline();
if (false === $command) {
$this->output->writeln("\n");
@ -71,8 +71,10 @@ class Shell
break;
}
readline_add_history($command);
readline_write_history($this->history);
if ($this->hasreadline) {
readline_add_history($command);
readline_write_history($this->history);
}
if (0 !== $ret = $this->application->run(new StringInput($command), $this->output)) {
$this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
@ -80,6 +82,25 @@ class Shell
}
}
/**
* 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;
}
/**
* Tries to return autocompletion for the current entered text.
*
@ -116,21 +137,33 @@ class Shell
}
/**
* Returns the shell header.
* Reads a single line from standard input.
*
* @return string The header string
* @return string The single line from standard input
*/
protected function getHeader()
private function readline()
{
return <<<EOF
$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;
}
Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>).
$line = rtrim($line);
$completeLine .= $line;
if (substr($line,-1) != '#') {
break;
} else {
$completeLine = substr($completeLine, 0, -1).PHP_EOL;
}
}
}
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;
return $completeLine;
}
}