Moving ShellDispatcher::getInput() into Shell as a protected method.

This commit is contained in:
mark_story 2010-10-04 23:26:38 -04:00
parent 67f03afa02
commit e816a49a6f
2 changed files with 41 additions and 5 deletions

View file

@ -19,6 +19,7 @@
*/ */
App::import('Shell', 'TaskCollection'); App::import('Shell', 'TaskCollection');
require_once CAKE . 'console' . DS . 'console_output.php'; require_once CAKE . 'console' . DS . 'console_output.php';
require_once CAKE . 'console' . DS . 'console_input.php';
/** /**
* Base class for command-line utilities for automating programmer chores. * Base class for command-line utilities for automating programmer chores.
@ -158,7 +159,7 @@ class Shell extends Object {
* Constructs this Shell instance. * Constructs this Shell instance.
* *
*/ */
function __construct(&$dispatch, $stdout = null, $stderr = null) { function __construct(&$dispatch, $stdout = null, $stderr = null, $stdin = null) {
$vars = array('params', 'args', 'shell', 'shellCommand' => 'command', 'shellPaths'); $vars = array('params', 'args', 'shell', 'shellCommand' => 'command', 'shellPaths');
foreach ($vars as $key => $var) { foreach ($vars as $key => $var) {
@ -182,12 +183,16 @@ class Shell extends Object {
$this->stdout = $stdout; $this->stdout = $stdout;
$this->stderr = $stderr; $this->stderr = $stderr;
$this->stdin = $stdin;
if ($this->stdout == null) { if ($this->stdout == null) {
$this->stdout = new ConsoleOutput('php://stdout'); $this->stdout = new ConsoleOutput('php://stdout');
} }
if ($this->stderr == null) { if ($this->stderr == null) {
$this->stderr = new ConsoleOutput('php://stderr'); $this->stderr = new ConsoleOutput('php://stderr');
} }
if ($this->stdin == null) {
$this->stdin = new ConsoleInput('php://stdin');
}
} }
/** /**
@ -305,7 +310,7 @@ class Shell extends Object {
if (!$this->interactive) { if (!$this->interactive) {
return $default; return $default;
} }
$in = $this->Dispatch->getInput($prompt, $options, $default); $in = $this->_getInput($prompt, $options, $default);
if ($options && is_string($options)) { if ($options && is_string($options)) {
if (strpos($options, ',')) { if (strpos($options, ',')) {
@ -318,7 +323,7 @@ class Shell extends Object {
} }
if (is_array($options)) { if (is_array($options)) {
while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) { while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
$in = $this->Dispatch->getInput($prompt, $options, $default); $in = $this->_getInput($prompt, $options, $default);
} }
} }
if ($in) { if ($in) {
@ -326,6 +331,39 @@ class Shell extends Object {
} }
} }
/**
* Prompts the user for input, and returns it.
*
* @param string $prompt Prompt text.
* @param mixed $options Array or string of options.
* @param string $default Default input value.
* @return Either the default value, or the user-provided input.
*/
protected function _getInput($prompt, $options, $default) {
if (!is_array($options)) {
$printOptions = '';
} else {
$printOptions = '(' . implode('/', $options) . ')';
}
if ($default === null) {
$this->stdout->write($prompt . " $printOptions \n" . '> ', 0);
} else {
$this->stdout->write($prompt . " $printOptions \n" . "[$default] > ", 0);
}
$result = $this->stdin->read();
if ($result === false) {
$this->_stop(1);
}
$result = trim($result);
if ($default != null && empty($result)) {
return $default;
}
return $result;
}
/** /**
* Outputs a single or multiple messages to stdout. If no parameters * Outputs a single or multiple messages to stdout. If no parameters
* are passed outputs just a newline. * are passed outputs just a newline.

View file

@ -167,8 +167,6 @@ class ShellDispatcher {
* *
*/ */
protected function _initEnvironment() { protected function _initEnvironment() {
$this->stdin = fopen('php://stdin', 'r');
if (!$this->__bootstrap()) { if (!$this->__bootstrap()) {
$message = "Unable to load CakePHP core.\nMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH; $message = "Unable to load CakePHP core.\nMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH;
throw new RuntimeException($message); throw new RuntimeException($message);