Console_CommandLine
[ class tree: Console_CommandLine ] [ index: Console_CommandLine ] [ all elements ]

Source for file Default.php

Documentation is available at Default.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Console_CommandLine package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Console
  15.  * @package   Console_CommandLine
  16.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  17.  * @copyright 2007 David JEAN LOUIS
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id$
  20.  * @link      http://pear.php.net/package/Console_CommandLine
  21.  * @since     File available since release 0.1.0
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * The message provider interface.
  27.  */
  28. require_once 'Console/CommandLine/MessageProvider.php';
  29.  
  30. /**
  31.  * The custom message provider interface.
  32.  */
  33. require_once 'Console/CommandLine/CustomMessageProvider.php';
  34.  
  35. /**
  36.  * Lightweight class that manages messages used by Console_CommandLine package,
  37.  * allowing the developper to customize these messages, for example to
  38.  * internationalize a command line frontend.
  39.  *
  40.  * @category  Console
  41.  * @package   Console_CommandLine
  42.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  43.  * @copyright 2007 David JEAN LOUIS
  44.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  45.  * @version   Release: 1.2.0
  46.  * @link      http://pear.php.net/package/Console_CommandLine
  47.  * @since     Class available since release 0.1.0
  48.  */
  49.     implements Console_CommandLine_MessageProvider,
  50.     Console_CommandLine_CustomMessageProvider
  51. {
  52.     // Properties {{{
  53.  
  54.     /**
  55.      * Associative array of messages
  56.      *
  57.      * @var array $messages 
  58.      */
  59.     protected $messages = array(
  60.         'OPTION_VALUE_REQUIRED'   => 'Option "{$name}" requires a value.',
  61.         'OPTION_VALUE_UNEXPECTED' => 'Option "{$name}" does not expect a value (got "{$value}").',
  62.         'OPTION_VALUE_NOT_VALID'  => 'Option "{$name}" must be one of the following: "{$choices}" (got "{$value}").',
  63.         'ARGUMENT_VALUE_NOT_VALID'=> 'Argument "{$name}" must be one of the following: "{$choices}" (got "{$value}").',
  64.         'OPTION_VALUE_TYPE_ERROR' => 'Option "{$name}" requires a value of type {$type} (got "{$value}").',
  65.         'OPTION_AMBIGUOUS'        => 'Ambiguous option "{$name}", can be one of the following: {$matches}.',
  66.         'OPTION_UNKNOWN'          => 'Unknown option "{$name}".',
  67.         'ARGUMENT_REQUIRED'       => 'You must provide at least {$argnum} argument{$plural}.',
  68.         'PROG_HELP_LINE'          => 'Type "{$progname} --help" to get help.',
  69.         'PROG_VERSION_LINE'       => '{$progname} version {$version}.',
  70.         'COMMAND_HELP_LINE'       => 'Type "{$progname} <command> --help" to get help on specific command.',
  71.         'USAGE_WORD'              => 'Usage',
  72.         'OPTION_WORD'             => 'Options',
  73.         'ARGUMENT_WORD'           => 'Arguments',
  74.         'COMMAND_WORD'            => 'Commands',
  75.         'PASSWORD_PROMPT'         => 'Password: ',
  76.         'PASSWORD_PROMPT_ECHO'    => 'Password (warning: will echo): ',
  77.         'INVALID_CUSTOM_INSTANCE' => 'Instance does not implement the required interface',
  78.         'LIST_OPTION_MESSAGE'     => 'lists valid choices for option {$name}',
  79.         'LIST_DISPLAYED_MESSAGE'  => 'Valid choices are: ',
  80.         'INVALID_SUBCOMMAND'      => 'Command "{$command}" is not valid.',
  81.         'SUBCOMMAND_REQUIRED'     => 'Please enter one of the following command: {$commands}.',
  82.     );
  83.  
  84.     // }}}
  85.     // get() {{{
  86.  
  87.     /**
  88.      * Retrieve the given string identifier corresponding message.
  89.      *
  90.      * @param string $code The string identifier of the message
  91.      * @param array  $vars An array of template variables
  92.      *
  93.      * @return string 
  94.      */
  95.     public function get($code$vars = array())
  96.     {
  97.         if (!isset($this->messages[$code])) {
  98.             return 'UNKNOWN';
  99.         }
  100.         return $this->replaceTemplateVars($this->messages[$code]$vars);
  101.     }
  102.  
  103.     // }}}
  104.     // getWithCustomMessages() {{{
  105.  
  106.     /**
  107.      * Retrieve the given string identifier corresponding message.
  108.      *
  109.      * @param string $code     The string identifier of the message
  110.      * @param array  $vars     An array of template variables
  111.      * @param array  $messages An optional array of messages to use. Array
  112.      *                          indexes are message codes.
  113.      *
  114.      * @return string 
  115.      */
  116.     public function getWithCustomMessages(
  117.         $code$vars = array()$messages = array()
  118.     {
  119.         // get message
  120.         if (isset($messages[$code])) {
  121.             $message $messages[$code];
  122.         elseif (isset($this->messages[$code])) {
  123.             $message $this->messages[$code];
  124.         else {
  125.             $message 'UNKNOWN';
  126.         }
  127.         return $this->replaceTemplateVars($message$vars);
  128.     }
  129.  
  130.     // }}}
  131.     // replaceTemplateVars() {{{
  132.  
  133.     /**
  134.      * Replaces template vars in a message
  135.      *
  136.      * @param string $message The message
  137.      * @param array  $vars    An array of template variables
  138.      *
  139.      * @return string 
  140.      */
  141.     protected function replaceTemplateVars($message$vars = array())
  142.     {
  143.         $tmpkeys array_keys($vars);
  144.         $keys    = array();
  145.         foreach ($tmpkeys as $key{
  146.             $keys['{$' $key '}';
  147.         }
  148.         return str_replace($keysarray_values($vars)$message);
  149.     }
  150.  
  151.     // }}}
  152. }

Documentation generated on Mon, 11 Mar 2019 15:51:31 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.