1 | <?php |
||
19 | final class PluginClient implements HttpClient, HttpAsyncClient |
||
20 | { |
||
21 | /** |
||
22 | * An HTTP async client. |
||
23 | * |
||
24 | * @var HttpAsyncClient |
||
25 | */ |
||
26 | private $client; |
||
27 | |||
28 | /** |
||
29 | * The plugin chain. |
||
30 | * |
||
31 | * @var Plugin[] |
||
32 | */ |
||
33 | private $plugins; |
||
34 | |||
35 | /** |
||
36 | * A list of options. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $options; |
||
41 | |||
42 | /** |
||
43 | * @param HttpClient|HttpAsyncClient $client |
||
44 | * @param Plugin[] $plugins |
||
45 | * @param array $options { |
||
46 | * |
||
47 | * @var int $max_restarts |
||
48 | * @var Plugin[] $debug_plugins an array of plugins that are injected between each normal plugin |
||
49 | * } |
||
50 | * |
||
51 | * @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient |
||
52 | */ |
||
53 | 9 | public function __construct($client, array $plugins = [], array $options = []) |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 5 | public function sendRequest(RequestInterface $request) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 2 | public function sendAsyncRequest(RequestInterface $request) |
|
101 | |||
102 | /** |
||
103 | * Configure the plugin client. |
||
104 | * |
||
105 | * @param array $options |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 9 | private function configure(array $options = []) |
|
110 | { |
||
111 | 9 | if (isset($options['debug_plugins'])) { |
|
112 | 1 | @trigger_error('The "debug_plugins" option is deprecated and will be removed in 2.0.', E_USER_DEPRECATED); |
|
|
|||
113 | 1 | } |
|
114 | |||
115 | 9 | $resolver = new OptionsResolver(); |
|
116 | 9 | $resolver->setDefaults([ |
|
117 | 9 | 'max_restarts' => 10, |
|
118 | 9 | 'debug_plugins' => [], |
|
119 | 9 | ]); |
|
120 | |||
121 | $resolver |
||
122 | 9 | ->setAllowedTypes('debug_plugins', 'array') |
|
123 | ->setAllowedValues('debug_plugins', function (array $plugins) { |
||
124 | 9 | foreach ($plugins as $plugin) { |
|
125 | // Make sure each object passed with the `debug_plugins` is an instance of Plugin. |
||
126 | 1 | if (!$plugin instanceof Plugin) { |
|
127 | return false; |
||
128 | } |
||
129 | 9 | } |
|
130 | |||
131 | 9 | return true; |
|
132 | 9 | }); |
|
133 | |||
134 | 9 | return $resolver->resolve($options); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Create the plugin chain. |
||
139 | * |
||
140 | * @param Plugin[] $pluginList A list of plugins |
||
141 | * @param callable $clientCallable Callable making the HTTP call |
||
142 | * |
||
143 | * @return callable |
||
144 | */ |
||
145 | 6 | private function createPluginChain($pluginList, callable $clientCallable) |
|
179 | } |
||
180 |
If you suppress an error, we recommend checking for the error condition explicitly: