Bearsampp 2026.9.23
Loading...
Searching...
No Matches
Common.php
Go to the documentation of this file.
1<?php
16
20require_once 'PEAR.php';
21
36{
49
54 var $ui;
55
56 var $_deps_rel_trans = array(
57 'lt' => '<',
58 'le' => '<=',
59 'eq' => '=',
60 'ne' => '!=',
61 'gt' => '>',
62 'ge' => '>=',
63 'has' => '=='
64 );
65
66 var $_deps_type_trans = array(
67 'pkg' => 'package',
68 'ext' => 'extension',
69 'php' => 'PHP',
70 'prog' => 'external program',
71 'ldlib' => 'external library for linking',
72 'rtlib' => 'external runtime library',
73 'os' => 'operating system',
74 'websrv' => 'web server',
75 'sapi' => 'SAPI backend'
76 );
77
83 function __construct(&$ui, &$config)
84 {
85 parent::__construct();
86 $this->config = &$config;
87 $this->ui = &$ui;
88 }
89
95 function getCommands()
96 {
97 $ret = array();
98 foreach (array_keys($this->commands) as $command) {
99 $ret[$command] = $this->commands[$command]['summary'];
100 }
101
102 return $ret;
103 }
104
110 function getShortcuts()
111 {
112 $ret = array();
113 foreach (array_keys($this->commands) as $command) {
114 if (isset($this->commands[$command]['shortcut'])) {
115 $ret[$this->commands[$command]['shortcut']] = $command;
116 }
117 }
118
119 return $ret;
120 }
121
123 {
124 $shortcuts = $this->getShortcuts();
125 if (isset($shortcuts[$command])) {
126 $command = $shortcuts[$command];
127 }
128
129 if (isset($this->commands[$command]) &&
130 isset($this->commands[$command]['options'])) {
131 return $this->commands[$command]['options'];
132 }
133
134 return null;
135 }
136
137 function getGetoptArgs($command, &$short_args, &$long_args)
138 {
139 $short_args = '';
140 $long_args = array();
141 if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) {
142 return;
143 }
144
145 reset($this->commands[$command]['options']);
146 foreach ($this->commands[$command]['options'] as $option => $info) {
147 $larg = $sarg = '';
148 if (isset($info['arg'])) {
149 if ($info['arg'][0] == '(') {
150 $larg = '==';
151 $sarg = '::';
152 $arg = substr($info['arg'], 1, -1);
153 } else {
154 $larg = '=';
155 $sarg = ':';
156 $arg = $info['arg'];
157 }
158 }
159
160 if (isset($info['shortopt'])) {
161 $short_args .= $info['shortopt'] . $sarg;
162 }
163
164 $long_args[] = $option . $larg;
165 }
166 }
167
177 {
178 $config = &PEAR_Config::singleton();
179 if (!isset($this->commands[$command])) {
180 return "No such command \"$command\"";
181 }
182
183 $help = null;
184 if (isset($this->commands[$command]['doc'])) {
185 $help = $this->commands[$command]['doc'];
186 }
187
188 if (empty($help)) {
189 // XXX (cox) Fallback to summary if there is no doc (show both?)
190 if (!isset($this->commands[$command]['summary'])) {
191 return "No help for command \"$command\"";
192 }
193 $help = $this->commands[$command]['summary'];
194 }
195
196 if (preg_match_all('/{config\s+([^\}]+)}/', $help, $matches)) {
197 foreach($matches[0] as $k => $v) {
198 $help = preg_replace("/$v/", $config->get($matches[1][$k]), $help);
199 }
200 }
201
202 return array($help, $this->getHelpArgs($command));
203 }
204
212 {
213 if (isset($this->commands[$command]['options']) &&
214 count($this->commands[$command]['options']))
215 {
216 $help = "Options:\n";
217 foreach ($this->commands[$command]['options'] as $k => $v) {
218 if (isset($v['arg'])) {
219 if ($v['arg'][0] == '(') {
220 $arg = substr($v['arg'], 1, -1);
221 $sapp = " [$arg]";
222 $lapp = "[=$arg]";
223 } else {
224 $sapp = " $v[arg]";
225 $lapp = "=$v[arg]";
226 }
227 } else {
228 $sapp = $lapp = "";
229 }
230
231 if (isset($v['shortopt'])) {
232 $s = $v['shortopt'];
233 $help .= " -$s$sapp, --$k$lapp\n";
234 } else {
235 $help .= " --$k$lapp\n";
236 }
237
238 $p = " ";
239 $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
240 $help .= " $doc\n";
241 }
242
243 return $help;
244 }
245
246 return null;
247 }
248
249 function run($command, $options, $params)
250 {
251 if (empty($this->commands[$command]['function'])) {
252 // look for shortcuts
253 foreach (array_keys($this->commands) as $cmd) {
254 if (isset($this->commands[$cmd]['shortcut']) && $this->commands[$cmd]['shortcut'] == $command) {
255 if (empty($this->commands[$cmd]['function'])) {
256 return $this->raiseError("unknown command `$command'");
257 } else {
258 $func = $this->commands[$cmd]['function'];
259 }
260 $command = $cmd;
261
262 //$command = $this->commands[$cmd]['function'];
263 break;
264 }
265 }
266 } else {
267 $func = $this->commands[$command]['function'];
268 }
269
270 return $this->$func($command, $options, $params);
271 }
272}
getGetoptArgs($command, &$short_args, &$long_args)
Definition Common.php:137
__construct(&$ui, &$config)
Definition Common.php:83
run($command, $options, $params)
Definition Common.php:249
getOptions($command)
Definition Common.php:122
getHelpArgs($command)
Definition Common.php:211
getHelp($command)
Definition Common.php:176
$options
Definition pearcmd.php:145
if( $raw) foreach($opts as $opt) if( $store_system_config) if($store_user_config) $command
Definition pearcmd.php:322