1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Resolver; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ApiResolver |
9
|
|
|
* |
10
|
|
|
* @internal |
11
|
|
|
* @codeCoverageIgnore |
12
|
|
|
* @package AlibabaCloud\Client\Resolver |
13
|
|
|
*/ |
14
|
|
|
abstract class ApiResolver |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param $name |
19
|
|
|
* @param $arguments |
20
|
|
|
* |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
|
|
public static function __callStatic($name, $arguments) |
24
|
|
|
{ |
25
|
|
|
return (new static())->__call($name, $arguments); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $api |
30
|
|
|
* @param $arguments |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
* @throws ClientException |
34
|
|
|
*/ |
35
|
|
|
public function __call($api, $arguments) |
36
|
|
|
{ |
37
|
|
|
$product_name = $this->getProductName(); |
38
|
|
|
$class = $this->getNamespace() . '\\' . \ucfirst($api); |
39
|
|
|
|
40
|
|
|
if (\class_exists($class)) { |
41
|
|
|
|
42
|
|
|
if (isset($arguments[0])) { |
43
|
|
|
return new $class($arguments[0]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new $class(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
throw new ClientException( |
50
|
|
|
"{$product_name} contains no $api", |
51
|
|
|
'SDK.ApiNotFound' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return mixed |
57
|
|
|
* @throws ClientException |
58
|
|
|
*/ |
59
|
|
|
private function getProductName() |
60
|
|
|
{ |
61
|
|
|
$array = \explode('\\', \get_class($this)); |
62
|
|
|
if (isset($array[3])) { |
63
|
|
|
return str_replace('ApiResolver', '', $array[3]); |
64
|
|
|
} |
65
|
|
|
throw new ClientException( |
66
|
|
|
'Service name not found.', |
67
|
|
|
'SDK.ServiceNotFound' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
* @throws ClientException |
74
|
|
|
*/ |
75
|
|
|
private function getNamespace() |
76
|
|
|
{ |
77
|
|
|
$array = \explode('\\', \get_class($this)); |
78
|
|
|
|
79
|
|
|
if (!isset($array[3])) { |
80
|
|
|
throw new ClientException( |
81
|
|
|
'Get namespace error.', |
82
|
|
|
'SDK.ParseError' |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
unset($array[3]); |
87
|
|
|
|
88
|
|
|
return \implode('\\', $array); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|