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: Default.php 282427 2009-06-19 10:22:48Z izi $
  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.1.3
  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.         'OPTION_VALUE_TYPE_ERROR' => 'Option "{$name}" requires a value of type {$type} (got "{$value}").',
  64.         'OPTION_AMBIGUOUS'        => 'Ambiguous option "{$name}", can be one of the following: {$matches}.',
  65.         'OPTION_UNKNOWN'          => 'Unknown option "{$name}".',
  66.         'ARGUMENT_REQUIRED'       => 'You must provide at least {$argnum} argument{$plural}.',
  67.         'PROG_HELP_LINE'          => 'Type "{$progname} --help" to get help.',
  68.         'PROG_VERSION_LINE'       => '{$progname} version {$version}.',
  69.         'COMMAND_HELP_LINE'       => 'Type "{$progname} <command> --help" to get help on specific command.',
  70.         'USAGE_WORD'              => 'Usage',
  71.         'OPTION_WORD'             => 'Options',
  72.         'ARGUMENT_WORD'           => 'Arguments',
  73.         'COMMAND_WORD'            => 'Commands',
  74.         'PASSWORD_PROMPT'         => 'Password: ',
  75.         'PASSWORD_PROMPT_ECHO'    => 'Password (warning: will echo): ',
  76.         'INVALID_CUSTOM_INSTANCE' => 'Instance does not implement the required interface',
  77.         'LIST_OPTION_MESSAGE'     => 'lists valid choices for option {$name}',
  78.         'LIST_DISPLAYED_MESSAGE'  => 'Valid choices are: ',
  79.         'INVALID_SUBCOMMAND'      => 'Command "{$command}" is not valid.',
  80.     );
  81.  
  82.     // }}}
  83.     // get() {{{
  84.  
  85.     /**
  86.      * Retrieve the given string identifier corresponding message.
  87.      *
  88.      * @param string $code The string identifier of the message
  89.      * @param array  $vars An array of template variables
  90.      *
  91.      * @return string 
  92.      */
  93.     public function get($code$vars = array())
  94.     {
  95.         if (!isset($this->messages[$code])) {
  96.             return 'UNKNOWN';
  97.         }
  98.         return $this->replaceTemplateVars($this->messages[$code]$vars);
  99.     }
  100.  
  101.     // }}}
  102.     // getWithCustomMessages() {{{
  103.  
  104.     /**
  105.      * Retrieve the given string identifier corresponding message.
  106.      *
  107.      * @param string $code     The string identifier of the message
  108.      * @param array  $vars     An array of template variables
  109.      * @param array  $messages An optional array of messages to use. Array
  110.      *                          indexes are message codes.
  111.      *
  112.      * @return string 
  113.      */
  114.     public function getWithCustomMessages(
  115.         $code$vars = array()$messages = array()
  116.     {
  117.         // get message
  118.         if (isset($messages[$code])) {
  119.             $message $messages[$code];
  120.         elseif (isset($this->messages[$code])) {
  121.             $message $this->messages[$code];
  122.         else {
  123.             $message 'UNKNOWN';
  124.         }
  125.         return $this->replaceTemplateVars($message$vars);
  126.     }
  127.  
  128.     // }}}
  129.     // replaceTemplateVars() {{{
  130.  
  131.     /**
  132.      * Replaces template vars in a message
  133.      *
  134.      * @param string $message The message
  135.      * @param array  $vars    An array of template variables
  136.      *
  137.      * @return string 
  138.      */
  139.     protected function replaceTemplateVars($message$vars = array())
  140.     {
  141.         $tmpkeys array_keys($vars);
  142.         $keys    = array();
  143.         foreach ($tmpkeys as $key{
  144.             $keys['{$' $key '}';
  145.         }
  146.         return str_replace($keysarray_values($vars)$message);
  147.     }
  148.  
  149.     // }}}
  150. }

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