Skip to content

Commit

Permalink
Moving ShellDispatcher::getInput() into Shell as a protected method.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 67f03af commit e816a49
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
44 changes: 41 additions & 3 deletions cake/console/libs/shell.php
Expand Up @@ -19,6 +19,7 @@
*/
App::import('Shell', 'TaskCollection');
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.
Expand Down Expand Up @@ -158,7 +159,7 @@ class Shell extends Object {
* 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');

foreach ($vars as $key => $var) {
Expand All @@ -182,12 +183,16 @@ function __construct(&$dispatch, $stdout = null, $stderr = null) {

$this->stdout = $stdout;
$this->stderr = $stderr;
$this->stdin = $stdin;
if ($this->stdout == null) {
$this->stdout = new ConsoleOutput('php://stdout');
}
if ($this->stderr == null) {
$this->stderr = new ConsoleOutput('php://stderr');
}
if ($this->stdin == null) {
$this->stdin = new ConsoleInput('php://stdin');
}
}

/**
Expand Down Expand Up @@ -305,7 +310,7 @@ public function in($prompt, $options = null, $default = null) {
if (!$this->interactive) {
return $default;
}
$in = $this->Dispatch->getInput($prompt, $options, $default);
$in = $this->_getInput($prompt, $options, $default);

if ($options && is_string($options)) {
if (strpos($options, ',')) {
Expand All @@ -318,14 +323,47 @@ public function in($prompt, $options = null, $default = null) {
}
if (is_array($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) {
return $in;
}
}

/**
* 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
* are passed outputs just a newline.
Expand Down
2 changes: 0 additions & 2 deletions cake/console/shell_dispatcher.php
Expand Up @@ -167,8 +167,6 @@ function __initConstants() {
*
*/
protected function _initEnvironment() {
$this->stdin = fopen('php://stdin', 'r');

if (!$this->__bootstrap()) {
$message = "Unable to load CakePHP core.\nMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH;
throw new RuntimeException($message);
Expand Down

0 comments on commit e816a49

Please sign in to comment.