|
2 | 2 |
|
3 | 3 | namespace Http\Client\Plugin;
|
4 | 4 |
|
5 |
| -use Http\Client\Common\EmulatedHttpAsyncClient; |
6 | 5 | use Http\Client\Exception;
|
7 |
| -use Http\Client\HttpAsyncClient; |
8 |
| -use Http\Client\HttpClient; |
9 | 6 | use Http\Client\Plugin\Exception\LoopException;
|
10 |
| -use Http\Promise\FulfilledPromise; |
11 |
| -use Http\Promise\RejectedPromise; |
12 | 7 | use Psr\Http\Message\RequestInterface;
|
13 |
| -use Symfony\Component\OptionsResolver\OptionsResolver; |
14 | 8 |
|
15 | 9 | /**
|
16 |
| - * The client managing plugins and providing a decorator around HTTP Clients. |
17 |
| - * |
18 | 10 | * @author Joel Wurtz <joel.wurtz@gmail.com>
|
| 11 | + * |
| 12 | + * @deprecated since version 1.1, to be removed in 2.0. Use {@link \Http\Client\Common\PluginClient} instead. |
19 | 13 | */
|
20 |
| -final class PluginClient implements HttpClient, HttpAsyncClient |
| 14 | +final class PluginClient extends \Http\Client\Common\PluginClient |
21 | 15 | {
|
22 |
| - /** |
23 |
| - * An HTTP async client. |
24 |
| - * |
25 |
| - * @var HttpAsyncClient |
26 |
| - */ |
27 |
| - private $client; |
28 |
| - |
29 |
| - /** |
30 |
| - * The plugin chain. |
31 |
| - * |
32 |
| - * @var Plugin[] |
33 |
| - */ |
34 |
| - private $plugins; |
35 |
| - |
36 |
| - /** |
37 |
| - * A list of options. |
38 |
| - * |
39 |
| - * @var array |
40 |
| - */ |
41 |
| - private $options; |
42 |
| - |
43 |
| - /** |
44 |
| - * @param HttpClient|HttpAsyncClient $client |
45 |
| - * @param Plugin[] $plugins |
46 |
| - * @param array $options { |
47 |
| - * |
48 |
| - * @var int $max_restarts |
49 |
| - * } |
50 |
| - * |
51 |
| - * @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient |
52 |
| - */ |
53 |
| - public function __construct($client, array $plugins = [], array $options = []) |
54 |
| - { |
55 |
| - if ($client instanceof HttpAsyncClient) { |
56 |
| - $this->client = $client; |
57 |
| - } elseif ($client instanceof HttpClient) { |
58 |
| - $this->client = new EmulatedHttpAsyncClient($client); |
59 |
| - } else { |
60 |
| - throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient'); |
61 |
| - } |
62 |
| - |
63 |
| - $this->plugins = $plugins; |
64 |
| - $this->options = $this->configure($options); |
65 |
| - } |
66 |
| - |
67 | 16 | /**
|
68 | 17 | * {@inheritdoc}
|
69 |
| - */ |
70 |
| - public function sendRequest(RequestInterface $request) |
71 |
| - { |
72 |
| - // If we don't have an http client, use the async call |
73 |
| - if (!($this->client instanceof HttpClient)) { |
74 |
| - return $this->sendAsyncRequest($request)->wait(); |
75 |
| - } |
76 |
| - |
77 |
| - // Else we want to use the synchronous call of the underlying client, and not the async one in the case |
78 |
| - // we have both an async and sync call |
79 |
| - $pluginChain = $this->createPluginChain($this->plugins, function (RequestInterface $request) { |
80 |
| - try { |
81 |
| - return new FulfilledPromise($this->client->sendRequest($request)); |
82 |
| - } catch (Exception $exception) { |
83 |
| - return new RejectedPromise($exception); |
84 |
| - } |
85 |
| - }); |
86 |
| - |
87 |
| - return $pluginChain($request)->wait(); |
88 |
| - } |
89 |
| - |
90 |
| - /** |
91 |
| - * {@inheritdoc} |
92 |
| - */ |
93 |
| - public function sendAsyncRequest(RequestInterface $request) |
94 |
| - { |
95 |
| - $pluginChain = $this->createPluginChain($this->plugins, function (RequestInterface $request) { |
96 |
| - return $this->client->sendAsyncRequest($request); |
97 |
| - }); |
98 |
| - |
99 |
| - return $pluginChain($request); |
100 |
| - } |
101 |
| - |
102 |
| - /** |
103 |
| - * Configure the plugin client. |
104 | 18 | *
|
105 |
| - * @param array $options |
106 |
| - * |
107 |
| - * @return array |
| 19 | + * Throw the correct loop exception. |
108 | 20 | */
|
109 |
| - private function configure(array $options = []) |
| 21 | + protected function createLoopException(RequestInterface $request) |
110 | 22 | {
|
111 |
| - $resolver = new OptionsResolver(); |
112 |
| - $resolver->setDefaults([ |
113 |
| - 'max_restarts' => 10, |
114 |
| - ]); |
115 |
| - |
116 |
| - return $resolver->resolve($options); |
117 |
| - } |
118 |
| - |
119 |
| - /** |
120 |
| - * Create the plugin chain. |
121 |
| - * |
122 |
| - * @param Plugin[] $pluginList A list of plugins |
123 |
| - * @param callable $clientCallable Callable making the HTTP call |
124 |
| - * |
125 |
| - * @return callable |
126 |
| - */ |
127 |
| - private function createPluginChain($pluginList, callable $clientCallable) |
128 |
| - { |
129 |
| - $firstCallable = $lastCallable = $clientCallable; |
130 |
| - |
131 |
| - while ($plugin = array_pop($pluginList)) { |
132 |
| - $lastCallable = function (RequestInterface $request) use ($plugin, $lastCallable, &$firstCallable) { |
133 |
| - return $plugin->handleRequest($request, $lastCallable, $firstCallable); |
134 |
| - }; |
135 |
| - |
136 |
| - $firstCallable = $lastCallable; |
137 |
| - } |
138 |
| - |
139 |
| - $firstCalls = 0; |
140 |
| - $firstCallable = function (RequestInterface $request) use ($lastCallable, &$firstCalls) { |
141 |
| - if ($firstCalls > $this->options['max_restarts']) { |
142 |
| - throw new LoopException('Too many restarts in plugin client', $request); |
143 |
| - } |
144 |
| - |
145 |
| - ++$firstCalls; |
146 |
| - |
147 |
| - return $lastCallable($request); |
148 |
| - }; |
149 |
| - |
150 |
| - return $firstCallable; |
| 23 | + return new LoopException('Too many restarts in plugin client', $request); |
151 | 24 | }
|
152 | 25 | }
|
0 commit comments