Skip to content
Permalink
Browse files

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

  • Loading branch information...
markstory committed Oct 5, 2010
1 parent 67f03af commit e816a49a6f4bf67d0845c71de68e1f8c9715710a
Showing with 41 additions and 5 deletions.
  1. +41 −3 cake/console/libs/shell.php
  2. +0 −2 cake/console/shell_dispatcher.php
@@ -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.
@@ -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) {
@@ -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');
}
}
/**
@@ -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, ',')) {
@@ -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.
@@ -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);

0 comments on commit e816a49

Please sign in to comment.
You can’t perform that action at this time.