1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin; |
4
|
|
|
|
5
|
1 |
|
@trigger_error('The '.__NAMESPACE__.'\PluginClient class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\PluginClient instead.', E_USER_DEPRECATED); |
|
|
|
|
6
|
|
|
|
7
|
|
|
use Http\Client\HttpAsyncClient; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use Http\Client\Plugin\Exception\LoopException; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The client managing plugins and providing a decorator around HTTP Clients. |
14
|
|
|
* |
15
|
|
|
* @author Joel Wurtz <[email protected]> |
16
|
|
|
* |
17
|
|
|
* @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\PluginClient} instead. |
18
|
|
|
*/ |
19
|
|
|
final class PluginClient implements HttpClient, HttpAsyncClient |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \Http\Client\Common\PluginClient |
23
|
|
|
*/ |
24
|
|
|
private $pluginClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param HttpClient|HttpAsyncClient $client |
28
|
|
|
* @param Plugin[] $plugins |
29
|
|
|
* @param array $options { |
30
|
|
|
* |
31
|
|
|
* @var int $max_restarts |
32
|
|
|
* } |
33
|
|
|
* |
34
|
|
|
* @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient |
35
|
|
|
*/ |
36
|
8 |
|
public function __construct($client, array $plugins = [], array $options = []) |
37
|
|
|
{ |
38
|
8 |
|
$this->pluginClient = new \Http\Client\Common\PluginClient($client, $plugins, $options); |
39
|
8 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
4 |
View Code Duplication |
public function sendRequest(RequestInterface $request) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
try { |
47
|
4 |
|
return $this->pluginClient->sendRequest($request); |
48
|
1 |
|
} catch (\Http\Client\Common\Exception\LoopException $e) { |
49
|
1 |
|
throw new LoopException($e->getMessage(), $e->getRequest(), $e); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
1 |
View Code Duplication |
public function sendAsyncRequest(RequestInterface $request) |
|
|
|
|
57
|
1 |
|
{ |
58
|
|
|
try { |
59
|
1 |
|
return $this->pluginClient->sendAsyncRequest($request); |
60
|
|
|
} catch (\Http\Client\Common\Exception\LoopException $e) { |
61
|
|
|
throw new LoopException($e->getMessage(), $e->getRequest(), $e); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.