33 static $version = null;
36 if (null !== $version) {
41 if (!strncmp(
'phar://', __FILE__, 7)) {
42 $phar = new \Phar(dirname(__DIR__));
43 $md = $phar->getMetadata();
44 $version = $md[
'version'];
47 $getver = dirname(__DIR__) .
48 DIRECTORY_SEPARATOR .
'vendor' .
49 DIRECTORY_SEPARATOR .
'erebot' .
50 DIRECTORY_SEPARATOR .
'buildenv' .
51 DIRECTORY_SEPARATOR .
'get_version.php';
52 if (file_exists($getver)) {
53 $version = trim(shell_exec($getver));
95 $usageFile = dirname(__DIR__) .
96 DIRECTORY_SEPARATOR .
'data' .
97 DIRECTORY_SEPARATOR .
'usage.txt';
98 $usage = @file_get_contents($usageFile);
99 $usage = str_replace(array(
"\r\n",
"\r"),
"\n", $usage);
100 $usage = trim($usage);
101 $output->write($usage, $prog,
'http://xmlrpc.example.com/');
123 $value = strtolower($value);
124 if (in_array($value, array(
'0',
'off',
'false'))) {
127 if (in_array($value, array(
'1',
'on',
'true'))) {
130 throw new \Exception(
'Invalid value "'.$value.
'" for type "bool"');
148 $content = @file_get_contents($value);
149 if ($content ===
false) {
150 throw new \Exception(
'Could not read content of "'.$value.
'"');
176 $result = new \DateTime($value);
180 throw new \Exception(
'Invalid datetime value "'.$value.
'"');
208 protected function parseParam(array &$args, \DateTimeZone $timezone)
211 throw new \Exception(
'Not enough arguments.');
214 $type = strtolower(array_shift($args));
226 $parseFunc = array($this,
'parseBool');
233 $parseFunc =
'intval';
239 $parseFunc =
'floatval';
245 $parseFunc =
'strval';
250 $parseFunc = array($this,
'parseFile');
261 $parseFunc = array($this,
'parseTimestamp');
269 throw new \Exception(
'Not enough arguments for "hash".');
272 if (in_array(strtolower($args[0]), array(
'endhash',
'eh'))) {
277 if (!is_int($key) && !is_string($key)) {
278 throw new \Exception(
279 'Invalid type "'.gettype($key).
'" for hash key. '.
280 'Only integer and string keys may be used.' 284 $result[$key] = $value;
295 throw new \Exception(
'Not enough arguments for "list".');
298 if (in_array(strtolower($args[0]), array(
'endlist',
'el'))) {
302 $result[] = $this->
parseParam($args, $timezone);
309 throw new \Exception(
'Unknown type "'.$type.
'".');
313 throw new \Exception(
'Not enough arguments.');
316 $value = array_shift($args);
317 $value = call_user_func($parseFunc, $value);
318 if ($value instanceof \DateTime) {
319 $value->setTimezone($timezone);
340 protected function parse(array $args)
345 'additional' => array(),
351 't' =>
new \DateTimeZone(@date_default_timezone_get()),
357 while (count($args)) {
358 $v = array_shift($args);
360 if ($params[
'serverURL'] === null) {
361 if (substr($v, 0, 1) ==
'-') {
363 $v = (string) substr($v, 1);
364 foreach (str_split($v) as $o) {
365 if (!array_key_exists($o, $options)) {
366 throw new \Exception(
367 'Unknown option "'.$o.
'". '.
368 'Use -h to get help.' 372 if (is_bool($options[$o])) {
374 } elseif (is_int($options[$o])) {
383 throw new \Exception(
384 'Not enough arguments for option "'.$o.
'".' 387 $v = array_shift($args);
388 if (!($options[$o] instanceof \DateTimeZone)) {
389 $options[$o] = new \DateTimeZone($v);
393 $params[
'serverURL'] = $v;
398 if ($params[
'procedure'] === null) {
399 $params[
'procedure'] = $v;
404 while (count($args)) {
405 $params[
'additional'][] = $this->
parseParam($args, $options[
't']);
408 return array($options, $params);
426 public function run(array $args)
428 $prog = array_shift($args);
430 list($options, $params) = $this->
parse($args);
432 fprintf(STDERR,
'%s: %s' . PHP_EOL, $prog, $e->getMessage());
444 $version = self::getVersion();
445 $license = self::getCopyrightAndLicense();
446 echo
'XRL version ' . $version . PHP_EOL;
447 echo PHP_EOL . $license . PHP_EOL;
448 echo
'Visit https://github.com/fpoirotte/XRL for more!' . PHP_EOL;
453 if ($params[
'serverURL'] === null || $params[
'procedure'] === null) {
459 $encoder = new \fpoirotte\XRL\NativeEncoder(
new \
fpoirotte\XRL\
Encoder($options[
't'],
true));
460 $decoder = new \fpoirotte\XRL\NativeDecoder(
new \
fpoirotte\XRL\
Decoder($options[
't'], $options[
'x']));
461 $request = new \fpoirotte\XRL\Request($params[
'procedure'], $params[
'additional']);
464 if (class_exists(
'\\Plop\\Plop')) {
465 $logging = \Plop\Plop::getInstance();
466 $logging->getLogger()->setLevel(40 - min(4, $options[
'v']) * 10);
472 $xml = $encoder->encodeRequest($request);
473 $logging and $logging->debug(
474 "Request:\n%(request)s",
475 array(
'request' => $xml)
478 echo
'Not sending the actual query due to dry run mode.' . PHP_EOL;
483 'Content-Type: text/xml',
484 'User-Agent: XRL/' . static::getVersion(),
492 'header' => $headers,
495 $context = stream_context_create($ctxOptions);
496 libxml_set_streams_context($context);
500 $result = $decoder->decodeResponse($params[
'serverURL']);
505 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.