[Console] fixed CS and simplified code

This commit is contained in:
Martin Hason 2011-07-01 10:40:43 +02:00
parent a5ff6352c5
commit 060d75de17

View File

@ -26,7 +26,7 @@ class Shell
private $application; private $application;
private $history; private $history;
private $output; private $output;
private $hasreadline; private $hasReadline;
private $prompt; private $prompt;
/** /**
@ -41,7 +41,7 @@ class Shell
*/ */
public function __construct(Application $application) public function __construct(Application $application)
{ {
$this->hasreadline = function_exists('readline') ? true : false; $this->hasReadline = function_exists('readline') ? true : false;
$this->application = $application; $this->application = $application;
$this->history = getenv('HOME').'/.history_'.$application->getName(); $this->history = getenv('HOME').'/.history_'.$application->getName();
$this->output = new ConsoleOutput(); $this->output = new ConsoleOutput();
@ -56,7 +56,7 @@ class Shell
$this->application->setAutoExit(false); $this->application->setAutoExit(false);
$this->application->setCatchExceptions(true); $this->application->setCatchExceptions(true);
if ($this->hasreadline) { if ($this->hasReadline) {
readline_read_history($this->history); readline_read_history($this->history);
readline_completion_function(array($this, 'autocompleter')); readline_completion_function(array($this, 'autocompleter'));
} }
@ -71,7 +71,7 @@ class Shell
break; break;
} }
if ($this->hasreadline) { if ($this->hasReadline) {
readline_add_history($command); readline_add_history($command);
readline_write_history($this->history); readline_write_history($this->history);
} }
@ -143,27 +143,14 @@ EOF;
*/ */
private function readline() private function readline()
{ {
$completeLine = ''; if ($this->hasReadline) {
if ($this->hasreadline) { $line = readline($this->prompt);
$completeLine = readline($this->prompt);
} else { } else {
$this->output->write($this->prompt); $this->output->write($this->prompt);
while (true) {
$line = fgets(STDIN, 1024); $line = fgets(STDIN, 1024);
if (!$line && strlen($line) == 0) { $line = (!$line && strlen($line) == 0) ? false : rtrim($line);
return false;
} }
$line = rtrim($line); return $line;
$completeLine .= $line;
if (substr($line,-1) != '#') {
break;
} else {
$completeLine = substr($completeLine, 0, -1).PHP_EOL;
}
}
}
return $completeLine;
} }
} }