/build/ext/phing/classes/phing/input/DefaultInputHandler.php

http://github.com/alexgorbatchev/SyntaxHighlighter · PHP · 81 lines · 34 code · 6 blank · 41 comment · 5 complexity · cb658f5a44f6feb26bb4fd82e5ad348f MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: DefaultInputHandler.php 293 2007-11-04 16:51:45Z hans $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/input/InputHandler.php';
  22. include_once 'phing/system/io/ConsoleReader.php';
  23. /**
  24. * Prompts using print(); reads input from Console.
  25. *
  26. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  27. * @author Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
  28. * @version $Revision: 1.6 $
  29. * @package phing.input
  30. */
  31. class DefaultInputHandler implements InputHandler {
  32. /**
  33. * Prompts and requests input. May loop until a valid input has
  34. * been entered.
  35. * @throws BuildException
  36. */
  37. public function handleInput(InputRequest $request) {
  38. $prompt = $this->getPrompt($request);
  39. $in = new ConsoleReader();
  40. do {
  41. print $prompt;
  42. try {
  43. $input = $in->readLine();
  44. if ($input === "" && ($request->getDefaultValue() !== null) ) {
  45. $input = $request->getDefaultValue();
  46. }
  47. $request->setInput($input);
  48. } catch (Exception $e) {
  49. throw new BuildException("Failed to read input from Console.", $e);
  50. }
  51. } while (!$request->isInputValid());
  52. }
  53. /**
  54. * Constructs user prompt from a request.
  55. *
  56. * <p>This implementation adds (choice1,choice2,choice3,...) to the
  57. * prompt for <code>MultipleChoiceInputRequest</code>s.</p>
  58. *
  59. * @param $request the request to construct the prompt for.
  60. * Must not be <code>null</code>.
  61. */
  62. protected function getPrompt(InputRequest $request) {
  63. $prompt = $request->getPrompt();
  64. if ($request instanceof YesNoInputRequest) {
  65. $prompt .= '(' . implode('/', $request->getChoices()) .')';
  66. } elseif ($request instanceof MultipleChoiceInputRequest) { // (a,b,c,d)
  67. $prompt .= '(' . implode(',', $request->getChoices()) . ')';
  68. }
  69. if ($request->getDefaultValue() !== null) {
  70. $prompt .= ' ['.$request->getDefaultValue().']';
  71. }
  72. $pchar = $request->getPromptChar();
  73. return $prompt . ($pchar ? $pchar . ' ' : ' ');
  74. }
  75. }