1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Resolver; |
4
|
|
|
|
5
|
|
|
use ReflectionObject; |
6
|
|
|
use AlibabaCloud\Client\Request\Request; |
7
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ApiResolver |
11
|
|
|
* |
12
|
|
|
* @codeCoverageIgnore |
13
|
|
|
* @package AlibabaCloud\Client\Resolver |
14
|
|
|
*/ |
15
|
|
|
abstract class ApiResolver |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param $name |
20
|
|
|
* @param $arguments |
21
|
|
|
* |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
|
|
public static function __callStatic($name, $arguments) |
25
|
|
|
{ |
26
|
|
|
return (new static())->__call($name, $arguments); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $api |
31
|
|
|
* @param $arguments |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
* @throws ClientException |
35
|
|
|
*/ |
36
|
|
|
public function __call($api, $arguments) |
37
|
|
|
{ |
38
|
|
|
$product_name = $this->getProductName(); |
39
|
|
|
$class = $this->getNamespace() . '\\' . \ucfirst($api); |
40
|
|
|
|
41
|
|
|
if (\class_exists($class)) { |
42
|
|
|
if (isset($arguments[0])) { |
43
|
|
|
return $this->warpEndpoint(new $class($arguments[0])); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->warpEndpoint(new $class()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
throw new ClientException( |
50
|
|
|
"{$product_name} contains no $api", |
51
|
|
|
'SDK.ApiNotFound' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Request $request |
57
|
|
|
* |
58
|
|
|
* @return Request |
59
|
|
|
*/ |
60
|
|
|
public function warpEndpoint(Request $request) |
61
|
|
|
{ |
62
|
|
|
$reflect = new ReflectionObject($request); |
63
|
|
|
$product_dir = dirname(dirname($reflect->getFileName())); |
64
|
|
|
$endpoints_json = "$product_dir/endpoints.json"; |
65
|
|
|
if (file_exists($endpoints_json)) { |
66
|
|
|
$endpoints = json_decode(file_get_contents($endpoints_json), true); |
67
|
|
|
if (isset($endpoints['endpoint_map'])) { |
68
|
|
|
$request->endpointMap = $endpoints['endpoint_map']; |
69
|
|
|
} |
70
|
|
|
if (isset($endpoints['endpoint_regional'])) { |
71
|
|
|
$request->endpointRegional = $endpoints['endpoint_regional']; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $request; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return mixed |
80
|
|
|
* @throws ClientException |
81
|
|
|
*/ |
82
|
|
|
private function getProductName() |
83
|
|
|
{ |
84
|
|
|
$array = \explode('\\', \get_class($this)); |
85
|
|
|
if (isset($array[3])) { |
86
|
|
|
return str_replace('ApiResolver', '', $array[3]); |
87
|
|
|
} |
88
|
|
|
throw new ClientException( |
89
|
|
|
'Service name not found.', |
90
|
|
|
'SDK.ServiceNotFound' |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
* @throws ClientException |
97
|
|
|
*/ |
98
|
|
|
private function getNamespace() |
99
|
|
|
{ |
100
|
|
|
$array = \explode('\\', \get_class($this)); |
101
|
|
|
|
102
|
|
|
if (!isset($array[3])) { |
103
|
|
|
throw new ClientException( |
104
|
|
|
'Get namespace error.', |
105
|
|
|
'SDK.ParseError' |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
unset($array[3]); |
110
|
|
|
|
111
|
|
|
return \implode('\\', $array); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|