12 namespace fpoirotte\XRL;
34 if (!strncmp(
'phar://', __FILE__, 7)) {
35 $phar = new \Phar(__FILE__);
36 $md = $phar->getMetadata();
37 return $md[
'version'];
41 $getver = dirname(__DIR__) .
42 DIRECTORY_SEPARATOR .
'vendor' .
43 DIRECTORY_SEPARATOR .
'erebot' .
44 DIRECTORY_SEPARATOR .
'buildenv' .
45 DIRECTORY_SEPARATOR .
'get_version.php';
46 if (file_exists($getver)) {
47 return trim(shell_exec($getver));
86 $usageFile = dirname(__DIR__) .
87 DIRECTORY_SEPARATOR .
'data' .
88 DIRECTORY_SEPARATOR .
'usage.txt';
89 $usage = @file_get_contents($usageFile);
90 $usage = str_replace(array(
"\r\n",
"\r"),
"\n", $usage);
91 $usage = trim($usage);
92 $output->write($usage, $prog,
'http://xmlrpc.example.com/');
114 $value = strtolower($value);
115 if (in_array($value, array(
'0',
'off',
'false'))) {
118 if (in_array($value, array(
'1',
'on',
'true'))) {
121 throw new \Exception(
'Invalid value "'.$value.
'" for type "bool"');
139 $content = @file_get_contents($value);
140 if ($content ===
false) {
141 throw new \Exception(
'Could not read content of "'.$value.
'"');
167 $result = new \DateTime($value);
171 throw new \Exception(
'Invalid datetime value "'.$value.
'"');
199 protected function parseParam(array &$args, \DateTimeZone $timezone)
202 throw new \Exception(
'Not enough arguments.');
205 $type = strtolower(array_shift($args));
217 $parseFunc = array($this,
'parseBool');
224 $parseFunc =
'intval';
230 $parseFunc =
'floatval';
236 $parseFunc =
'strval';
241 $parseFunc = array($this,
'parseFile');
252 $parseFunc = array($this,
'parseTimestamp');
260 throw new \Exception(
'Not enough arguments for "hash".');
263 if (in_array(strtolower($args[0]), array(
'endhash',
'eh'))) {
268 if (!is_int($key) && !is_string($key)) {
269 throw new \Exception(
270 'Invalid type "'.gettype($key).
'" for hash key. '.
271 'Only integer and string keys may be used.'
275 $result[$key] = $value;
286 throw new \Exception(
'Not enough arguments for "list".');
289 if (in_array(strtolower($args[0]), array(
'endlist',
'el'))) {
293 $result[] = $this->
parseParam($args, $timezone);
300 throw new \Exception(
'Unknown type "'.$type.
'".');
304 throw new \Exception(
'Not enough arguments.');
307 $value = array_shift($args);
308 $value = call_user_func($parseFunc, $value);
309 if ($value instanceof \DateTime) {
310 $value->setTimezone($timezone);
331 protected function parse(array $args)
336 'additional' => array(),
342 't' =>
new \DateTimeZone(@date_default_timezone_get()),
348 while (count($args)) {
349 $v = array_shift($args);
351 if ($params[
'serverURL'] === null) {
352 if (substr($v, 0, 1) ==
'-') {
354 $v = (string) substr($v, 1);
355 foreach (str_split($v) as $o) {
356 if (!array_key_exists($o, $options)) {
357 throw new \Exception(
358 'Unknown option "'.$o.
'". '.
359 'Use -h to get help.'
363 if (is_bool($options[$o])) {
365 } elseif (is_int($options[$o])) {
374 throw new \Exception(
375 'Not enough arguments for option "'.$o.
'".'
378 $v = array_shift($args);
379 if (!($options[$o] instanceof \DateTimeZone)) {
380 $options[$o] = new \DateTimeZone($v);
384 $params[
'serverURL'] = $v;
389 if ($params[
'procedure'] === null) {
390 $params[
'procedure'] = $v;
395 while (count($args)) {
396 $params[
'additional'][] = $this->
parseParam($args, $options[
't']);
399 return array($options, $params);
417 public function run(array $args)
419 $prog = array_shift($args);
421 list($options, $params) = $this->
parse($args);
423 fprintf(STDERR,
'%s: %s' . PHP_EOL, $prog, $e->getMessage());
435 $version = self::getVersion();
436 $license = self::getCopyrightAndLicense();
437 echo
'XRL version ' . $version . PHP_EOL;
438 echo PHP_EOL . $license . PHP_EOL;
439 echo
'Visit https://github.com/fpoirotte/XRL for more!' . PHP_EOL;
444 if ($params[
'serverURL'] === null || $params[
'procedure'] === null) {
450 $encoder = new \fpoirotte\XRL\NativeEncoder(
new \fpoirotte\XRL\
Encoder($options[
't'],
true));
451 $decoder = new \fpoirotte\XRL\NativeDecoder(
new \fpoirotte\XRL\
Decoder($options[
't'], $options[
'x']));
452 $request = new \fpoirotte\XRL\Request($params[
'procedure'], $params[
'additional']);
455 if (class_exists(
'\\Plop\\Plop')) {
456 $logging = \Plop\Plop::getInstance();
457 $logging->getLogger()->setLevel(40 - max(4, $options[
'v']) * 10);
463 $xml = $encoder->encodeRequest($request);
464 $logging and $logging->debug(
465 "Request:\n%(request)s",
466 array(
'request' => $xml)
469 echo
'Not sending the actual query due to dry run mode.' . PHP_EOL;
478 'header' =>
'Content-Type: text/xml',
481 $context = stream_context_create($ctxOptions);
482 libxml_set_streams_context($context);
486 $result = $decoder->decodeResponse($params[
'serverURL']);
491 echo
'Result:' . PHP_EOL . print_r($result,
true) . PHP_EOL;
parseParam(array &$args,\DateTimeZone $timezone)
An XML-RPC encoder that can produce either compact documents or pretty documents. ...
An exception that is used to represent XML-RPC errors.
static getCopyrightAndLicense()
printUsage($output, $prog)
A decoder that can process XML-RPC requests and responses, with optional XML validation.
A class that implements a simple CLI script to send XML-RPC queries and to display their result...
A class that formats messages before sending them to a stream.