Permalink
Browse files

Refs #553 - use symfony/console to ask questions, add 'hidden' attribute

1 parent 9d19a84 commit 7bf11af662716686bac2db77df58dce36aab30cd @mrook mrook committed Feb 5, 2017
@@ -18,6 +18,8 @@
*/
use SebastianBergmann\Version;
+use Symfony\Component\Console\Input\StringInput;
+use Symfony\Component\Console\Output\ConsoleOutput;
require_once 'phing/Diagnostics.php';
require_once 'phing/Project.php';
@@ -829,7 +831,8 @@ private function addBuildListeners(Project $project)
private function addInputHandler(Project $project)
{
if ($this->inputHandlerClassname === null) {
- $handler = new DefaultInputHandler();
+// $handler = new DefaultInputHandler();
+ $handler = new ConsoleInputHandler(STDIN, new ConsoleOutput());
} else {
try {
$clz = Phing::import($this->inputHandlerClassname);
@@ -0,0 +1,104 @@
+<?php
+
+use Symfony\Component\Console\Helper\QuestionHelper;
+use Symfony\Component\Console\Input\StringInput;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Question\ChoiceQuestion;
+use Symfony\Component\Console\Question\ConfirmationQuestion;
+use Symfony\Component\Console\Question\Question;
+
+/**
+ * Uses Symfony Console to present questions
+ *
+ * @author Michiel Rook <mrook@php.net>
+ * @package phing.input
+ */
+class ConsoleInputHandler implements InputHandler
+{
+ /**
+ * @var resource
+ */
+ private $inputStream;
+
+ /**
+ * @var OutputInterface
+ */
+ private $output;
+
+ /**
+ * ConsoleInputHandler constructor.
+ * @param resource $inputStream
+ * @param OutputInterface $output
+ */
+ public function __construct($inputStream, OutputInterface $output)
+ {
+ $this->inputStream = $inputStream;
+ $this->output = $output;
+ }
+
+ /**
+ * Handle the request encapsulated in the argument.
+ *
+ * <p>Precondition: the request.getPrompt will return a non-null
+ * value.</p>
+ *
+ * <p>Postcondition: request.getInput will return a non-null
+ * value, request.isInputValid will return true.</p>
+ * @param InputRequest $request
+ * @return void
+ */
+ public function handleInput(InputRequest $request)
+ {
+ $questionHelper = new QuestionHelper();
+ $questionHelper->setInputStream($this->inputStream);
+
+ $question = $this->getQuestion($request);
+
+ if ($request->isHidden()) {
+ $question->setHidden(true);
+ }
+
+ $result = $questionHelper->ask(new StringInput(''), $this->output, $question);
+
+ $request->setInput($result);
+ }
+
+ /**
+ * @param InputRequest $inputRequest
+ * @return Question
+ */
+ protected function getQuestion(InputRequest $inputRequest)
+ {
+ $prompt = $this->getPrompt($inputRequest);
+
+ if ($inputRequest instanceof YesNoInputRequest) {
+ return new ConfirmationQuestion($prompt);
+ } else if ($inputRequest instanceof MultipleChoiceInputRequest) {
+ return new ChoiceQuestion($prompt, $inputRequest->getChoices(), $inputRequest->getDefaultValue());
+ } else {
+ return new Question($prompt, $inputRequest->getDefaultValue());
+ }
+ }
+
+ /**
+ * @param InputRequest $inputRequest
+ * @return string
+ */
+ protected function getPrompt(InputRequest $inputRequest)
+ {
+ $prompt = $inputRequest->getPrompt();
+ $defaultValue = $inputRequest->getDefaultValue();
+
+ if ($defaultValue !== null) {
+ if ($inputRequest instanceof YesNoInputRequest) {
+ $defaultValue = $inputRequest->getChoices()[$defaultValue];
+ }
+
+ $prompt .= ' [' . $defaultValue . ']';
+ }
+
+ $pchar = $inputRequest->getPromptChar();
+
+ return $prompt . ($pchar ? $pchar . ' ' : ' ');
+ }
+}
@@ -30,12 +30,32 @@
*/
class InputRequest
{
+ /**
+ * @var string
+ */
protected $prompt;
+
+ /**
+ * @var string
+ */
protected $input;
+
+ /**
+ * @var string
+ */
protected $defaultValue;
+
+ /**
+ * @var string
+ */
protected $promptChar;
/**
+ * @var bool
+ */
+ protected $hidden = false;
+
+ /**
* @param string $prompt The prompt to show to the user. Must not be null.
* @throws BuildException
*/
@@ -115,4 +135,20 @@ public function getPromptChar()
{
return $this->promptChar;
}
+
+ /**
+ * @return bool
+ */
+ public function isHidden()
+ {
+ return $this->hidden;
+ }
+
+ /**
+ * @param bool $hidden
+ */
+ public function setHidden($hidden)
+ {
+ $this->hidden = $hidden;
+ }
}
@@ -36,13 +36,37 @@
*/
class InputTask extends Task
{
+ /**
+ * @var string
+ */
private $validargs;
+
+ /**
+ * @var string
+ */
private $message = ""; // required
+
+ /**
+ * @var string
+ */
private $propertyName; // required
+
+ /**
+ * @var string
+ */
private $defaultValue;
+
+ /**
+ * @var string
+ */
private $promptChar;
/**
+ * @var bool
+ */
+ private $hidden = false;
+
+ /**
* Defines valid input parameters as comma separated strings. If set, input
* task will reject any input not defined as accepted and requires the user
* to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
@@ -76,7 +100,7 @@ public function setMessage($message)
/**
* Set a multiline message.
- * @param $msg
+ * @param string $msg
*/
public function addText($msg)
{
@@ -102,6 +126,14 @@ public function setPromptChar($c)
}
/**
+ * @param bool $hidden
+ */
+ public function setHidden($hidden)
+ {
+ $this->hidden = $hidden;
+ }
+
+ /**
* Actual method executed by phing.
* @throws BuildException
*/
@@ -140,8 +172,8 @@ public function main()
// default default is curr prop value
$request->setDefaultValue($this->project->getProperty($this->propertyName));
-
$request->setPromptChar($this->promptChar);
+ $request->setHidden($this->hidden);
// unless overridden...
if ($this->defaultValue !== null) {
View
@@ -24,6 +24,7 @@
"require": {
"php": ">=5.6",
"sebastian/version": "^2.0",
+ "symfony/console": "^2.8|^3.0",
"symfony/yaml": "^2.8|^3.1"
},
"require-dev": {
Oops, something went wrong.

0 comments on commit 7bf11af

Please sign in to comment.