0
|
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: @package_version@
|
|
46 * @link http://pear.php.net/package/Console_CommandLine
|
|
47 * @since Class available since release 0.1.0
|
|
48 */
|
|
49 class Console_CommandLine_MessageProvider_Default
|
|
50 implements Console_CommandLine_MessageProvider,
|
|
51 Console_CommandLine_CustomMessageProvider
|
|
52 {
|
|
53 // Properties {{{
|
|
54
|
|
55 /**
|
|
56 * Associative array of messages
|
|
57 *
|
|
58 * @var array $messages
|
|
59 */
|
|
60 protected $messages = array(
|
|
61 'OPTION_VALUE_REQUIRED' => 'Option "{$name}" requires a value.',
|
|
62 'OPTION_VALUE_UNEXPECTED' => 'Option "{$name}" does not expect a value (got "{$value}").',
|
|
63 'OPTION_VALUE_NOT_VALID' => 'Option "{$name}" must be one of the following: "{$choices}" (got "{$value}").',
|
|
64 'ARGUMENT_VALUE_NOT_VALID'=> 'Argument "{$name}" must be one of the following: "{$choices}" (got "{$value}").',
|
|
65 'OPTION_VALUE_TYPE_ERROR' => 'Option "{$name}" requires a value of type {$type} (got "{$value}").',
|
|
66 'OPTION_AMBIGUOUS' => 'Ambiguous option "{$name}", can be one of the following: {$matches}.',
|
|
67 'OPTION_UNKNOWN' => 'Unknown option "{$name}".',
|
|
68 'ARGUMENT_REQUIRED' => 'You must provide at least {$argnum} argument{$plural}.',
|
|
69 'PROG_HELP_LINE' => 'Type "{$progname} --help" to get help.',
|
|
70 'PROG_VERSION_LINE' => '{$progname} version {$version}.',
|
|
71 'COMMAND_HELP_LINE' => 'Type "{$progname} <command> --help" to get help on specific command.',
|
|
72 'USAGE_WORD' => 'Usage',
|
|
73 'OPTION_WORD' => 'Options',
|
|
74 'ARGUMENT_WORD' => 'Arguments',
|
|
75 'COMMAND_WORD' => 'Commands',
|
|
76 'PASSWORD_PROMPT' => 'Password: ',
|
|
77 'PASSWORD_PROMPT_ECHO' => 'Password (warning: will echo): ',
|
|
78 'INVALID_CUSTOM_INSTANCE' => 'Instance does not implement the required interface',
|
|
79 'LIST_OPTION_MESSAGE' => 'lists valid choices for option {$name}',
|
|
80 'LIST_DISPLAYED_MESSAGE' => 'Valid choices are: ',
|
|
81 'INVALID_SUBCOMMAND' => 'Command "{$command}" is not valid.',
|
|
82 'SUBCOMMAND_REQUIRED' => 'Please enter one of the following command: {$commands}.',
|
|
83 );
|
|
84
|
|
85 // }}}
|
|
86 // get() {{{
|
|
87
|
|
88 /**
|
|
89 * Retrieve the given string identifier corresponding message.
|
|
90 *
|
|
91 * @param string $code The string identifier of the message
|
|
92 * @param array $vars An array of template variables
|
|
93 *
|
|
94 * @return string
|
|
95 */
|
|
96 public function get($code, $vars = array())
|
|
97 {
|
|
98 if (!isset($this->messages[$code])) {
|
|
99 return 'UNKNOWN';
|
|
100 }
|
|
101 return $this->replaceTemplateVars($this->messages[$code], $vars);
|
|
102 }
|
|
103
|
|
104 // }}}
|
|
105 // getWithCustomMessages() {{{
|
|
106
|
|
107 /**
|
|
108 * Retrieve the given string identifier corresponding message.
|
|
109 *
|
|
110 * @param string $code The string identifier of the message
|
|
111 * @param array $vars An array of template variables
|
|
112 * @param array $messages An optional array of messages to use. Array
|
|
113 * indexes are message codes.
|
|
114 *
|
|
115 * @return string
|
|
116 */
|
|
117 public function getWithCustomMessages(
|
|
118 $code, $vars = array(), $messages = array()
|
|
119 ) {
|
|
120 // get message
|
|
121 if (isset($messages[$code])) {
|
|
122 $message = $messages[$code];
|
|
123 } elseif (isset($this->messages[$code])) {
|
|
124 $message = $this->messages[$code];
|
|
125 } else {
|
|
126 $message = 'UNKNOWN';
|
|
127 }
|
|
128 return $this->replaceTemplateVars($message, $vars);
|
|
129 }
|
|
130
|
|
131 // }}}
|
|
132 // replaceTemplateVars() {{{
|
|
133
|
|
134 /**
|
|
135 * Replaces template vars in a message
|
|
136 *
|
|
137 * @param string $message The message
|
|
138 * @param array $vars An array of template variables
|
|
139 *
|
|
140 * @return string
|
|
141 */
|
|
142 protected function replaceTemplateVars($message, $vars = array())
|
|
143 {
|
|
144 $tmpkeys = array_keys($vars);
|
|
145 $keys = array();
|
|
146 foreach ($tmpkeys as $key) {
|
|
147 $keys[] = '{$' . $key . '}';
|
|
148 }
|
|
149 return str_replace($keys, array_values($vars), $message);
|
|
150 }
|
|
151
|
|
152 // }}}
|
|
153 }
|