forked from EverexIO/JSON-RPC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPC.php
More file actions
87 lines (82 loc) · 2.24 KB
/
RPC.php
File metadata and controls
87 lines (82 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* @package AmiLabs/JSONRPC
*/
namespace AmiLabs\JSONRPC;
use InvalidArgumentException;
use RuntimeException;
/**
* Remote Procedure Call client/server implementation.
*
* Factory, returnining RPC layers.
* Example:
* <code>
* use AmiLabs\JSONRPC\RPC;
*
* $client = RPC::getLayer(
* 'JSON',
* // or class implementing AmiLabs\JSONRPC\RPC\ClientInterface interface:
* // '\\My\\Namespace\\JSON',
* RPC::TYPE_CLIENT,
* array(
* CURLOPT_SSL_VERIFYPEER => FALSE,
* CURLOPT_SSL_VERIFYHOST => FALSE
* )
* );
* $client->open('https://user:password@domain:port/');
* $response = $client->execute(
* 'methodName',
* array(
* 'param1' => 'value1',
* 'param2' => 'value2',
* // ...
* )
* );
* </code>
*
* @package AmiLabs/JSONRPC
* @author deepeloper ({@see https://github.com/deepeloper})
*/
class RPC
{
const TYPE_CLIENT = 1;
const TYPE_SERVER = 2;
/**
* Returns RPC layer.
*
* @param string $layer RPC layer, for exaple 'JSON'
* @param int $type self::TYPE_CLIENT | self::TYPE_SERVER
* @param array $options Options passing to the layer
* @return \AmiLabs\JSONRPC\RPC\Layer
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public static function getLayer($layer, $type, array $options = array())
{
switch ($type) {
case self::TYPE_CLIENT:
$type = 'Client';
break;
case self::TYPE_SERVER:
$type = 'Server';
break;
default:
throw new InvalidArgumentException(
sprintf('Invalid layer type %s', $type)
);
}
if (FALSE === mb_strpos($layer, '\\', NULL, 'ASCII')) {
$class = "AmiLabs\\JSONRPC\\RPC\\{$type}\\{$layer}";
} else {
$class = $layer;
}
$layer = new $class($options);
$interface = "AmiLabs\\JSONRPC\\RPC\\{$type}Layer";
if (!($layer instanceof $interface)) {
throw new RuntimeException(
sprintf('Class %s does not implement %s interface', $class, $interface)
);
}
return $layer;
}
}