home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / input / DefaultInputHandler.php next >
Encoding:
PHP Script  |  2006-09-14  |  3.2 KB  |  83 lines

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