Blame view

vendor/alibabacloud/client/src/Resolver/ApiResolver.php 2.67 KB
0c34aba8   xu   更新阿里云短信接口
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

namespace AlibabaCloud\Client\Resolver;

use ReflectionObject;
use AlibabaCloud\Client\Request\Request;
use AlibabaCloud\Client\Exception\ClientException;

/**
 * Class ApiResolver
 *
 * @codeCoverageIgnore
 * @package AlibabaCloud\Client\Resolver
 */
abstract class ApiResolver
{

    /**
     * @param $name
     * @param $arguments
     *
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        return (new static())->__call($name, $arguments);
    }

    /**
     * @param $api
     * @param $arguments
     *
     * @return mixed
     * @throws ClientException
     */
    public function __call($api, $arguments)
    {
        $product_name = $this->getProductName();
        $class        = $this->getNamespace() . '\\' . \ucfirst($api);

        if (\class_exists($class)) {
            if (isset($arguments[0])) {
                return $this->warpEndpoint(new $class($arguments[0]));
            }

            return $this->warpEndpoint(new $class());
        }

        throw new ClientException(
            "{$product_name} contains no $api",
            'SDK.ApiNotFound'
        );
    }

    /**
     * @param Request $request
     *
     * @return Request
     */
    public function warpEndpoint(Request $request)
    {
        $reflect        = new ReflectionObject($request);
        $product_dir    = dirname(dirname($reflect->getFileName()));
        $endpoints_json = "$product_dir/endpoints.json";
        if (file_exists($endpoints_json)) {
            $endpoints = json_decode(file_get_contents($endpoints_json), true);
            if (isset($endpoints['endpoint_map'])) {
                $request->endpointMap = $endpoints['endpoint_map'];
            }
            if (isset($endpoints['endpoint_regional'])) {
                $request->endpointRegional = $endpoints['endpoint_regional'];
            }
        }

        return $request;
    }

    /**
     * @return mixed
     * @throws ClientException
     */
    private function getProductName()
    {
        $array = \explode('\\', \get_class($this));
        if (isset($array[3])) {
            return str_replace('ApiResolver', '', $array[3]);
        }
        throw new ClientException(
            'Service name not found.',
            'SDK.ServiceNotFound'
        );
    }

    /**
     * @return string
     * @throws ClientException
     */
    private function getNamespace()
    {
        $array = \explode('\\', \get_class($this));

        if (!isset($array[3])) {
            throw new ClientException(
                'Get namespace error.',
                'SDK.ParseError'
            );
        }

        unset($array[3]);

        return \implode('\\', $array);
    }
}