From e816a49a6f4bf67d0845c71de68e1f8c9715710a Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 4 Oct 2010 23:26:38 -0400 Subject: [PATCH] Moving ShellDispatcher::getInput() into Shell as a protected method. --- cake/console/libs/shell.php | 44 ++++++++++++++++++++++++++++--- cake/console/shell_dispatcher.php | 2 -- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index a7f52fa7249..fbf026981b4 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.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,7 +323,7 @@ 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) { @@ -326,6 +331,39 @@ public function in($prompt, $options = null, $default = null) { } } +/** + * 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. diff --git a/cake/console/shell_dispatcher.php b/cake/console/shell_dispatcher.php index c11f3988b8d..d01a72acfd8 100644 --- a/cake/console/shell_dispatcher.php +++ b/cake/console/shell_dispatcher.php @@ -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);