|
11 | 11 |
|
12 | 12 | namespace FOS\HttpCache\ProxyClient;
|
13 | 13 |
|
14 |
| -use FOS\HttpCache\Exception\ExceptionCollection; |
15 |
| -use FOS\HttpCache\Exception\ProxyResponseException; |
16 |
| -use FOS\HttpCache\Exception\ProxyUnreachableException; |
17 |
| -use FOS\HttpCache\ProxyClient\Request\InvalidationRequest; |
18 |
| -use FOS\HttpCache\ProxyClient\Request\RequestQueue; |
19 |
| -use Http\Adapter\Exception\MultiHttpAdapterException; |
20 |
| -use Http\Adapter\HttpAdapter; |
21 |
| -use Http\Discovery\HttpAdapterDiscovery; |
22 |
| -use Psr\Http\Message\ResponseInterface; |
| 14 | +use FOS\HttpCache\ProxyClient\Http\HttpAdapter; |
| 15 | +use Http\Client\HttpAsyncClient; |
| 16 | +use Http\Discovery\HttpAsyncClientDiscovery; |
| 17 | +use Http\Discovery\MessageFactoryDiscovery; |
| 18 | +use Http\Message\MessageFactory; |
| 19 | +use Psr\Http\Message\UriInterface; |
| 20 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
23 | 21 |
|
24 | 22 | /**
|
25 |
| - * Abstract caching proxy client |
| 23 | + * Abstract HTTP based caching proxy client. |
26 | 24 | *
|
27 | 25 | * @author David de Boer <[email protected]>
|
28 | 26 | */
|
29 | 27 | abstract class AbstractProxyClient implements ProxyClientInterface
|
30 | 28 | {
|
31 | 29 | /**
|
32 |
| - * HTTP client |
| 30 | + * HTTP client adapter. |
33 | 31 | *
|
34 | 32 | * @var HttpAdapter
|
35 | 33 | */
|
36 |
| - private $httpAdapter; |
| 34 | + protected $httpAdapter; |
37 | 35 |
|
38 | 36 | /**
|
39 |
| - * Request queue |
| 37 | + * @var MessageFactory |
| 38 | + */ |
| 39 | + protected $messageFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * The options configured in the constructor argument or default values. |
40 | 43 | *
|
41 |
| - * @var RequestQueue |
| 44 | + * @var array The resolved options. |
42 | 45 | */
|
43 |
| - protected $queue; |
| 46 | + protected $options; |
44 | 47 |
|
45 | 48 | /**
|
46 | 49 | * Constructor
|
47 | 50 | *
|
48 |
| - * @param array $servers Caching proxy server hostnames or IP |
49 |
| - * addresses, including port if not port 80. |
50 |
| - * E.g. ['127.0.0.1:6081'] |
51 |
| - * @param string $baseUri Default application hostname, optionally |
52 |
| - * including base URL, for purge and refresh |
53 |
| - * requests (optional). This is required if |
54 |
| - * you purge and refresh paths instead of |
55 |
| - * absolute URLs. |
56 |
| - * @param HttpAdapter $httpAdapter If no HTTP adapter is supplied, a default |
57 |
| - * one will be created. |
| 51 | + * Supported options: |
| 52 | + * |
| 53 | + * - base_uri Default application hostname, optionally including base URL, |
| 54 | + * for purge and refresh requests (optional). This is required if you |
| 55 | + * purge and refresh paths instead of absolute URLs. |
| 56 | + * |
| 57 | + * @param array $servers Caching proxy server hostnames or IP |
| 58 | + * addresses, including port if not port 80. |
| 59 | + * E.g. ['127.0.0.1:6081'] |
| 60 | + * @param array $options List of options for the client. |
| 61 | + * @param HttpAsyncClient|null $httpClient Client capable of sending HTTP requests. If no |
| 62 | + * client is supplied, a default one is created. |
| 63 | + * @param MessageFactory|null $messageFactory Factory for PSR-7 messages. If none supplied, |
| 64 | + * a default one is created. |
58 | 65 | */
|
59 | 66 | public function __construct(
|
60 | 67 | array $servers,
|
61 |
| - $baseUri = null, |
62 |
| - HttpAdapter $httpAdapter = null |
| 68 | + array $options = [], |
| 69 | + HttpAsyncClient $httpClient = null, |
| 70 | + MessageFactory $messageFactory = null |
63 | 71 | ) {
|
64 |
| - $this->httpAdapter = $httpAdapter ?: HttpAdapterDiscovery::find(); |
65 |
| - $this->initQueue($servers, $baseUri); |
| 72 | + if (!$httpClient) { |
| 73 | + $httpClient = HttpAsyncClientDiscovery::find(); |
| 74 | + } |
| 75 | + $this->options = $this->getDefaultOptions()->resolve($options); |
| 76 | + $this->httpAdapter = new HttpAdapter($servers, $this->options['base_uri'], $httpClient); |
| 77 | + $this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
66 | 78 | }
|
67 | 79 |
|
68 | 80 | /**
|
69 | 81 | * {@inheritdoc}
|
70 | 82 | */
|
71 | 83 | public function flush()
|
72 | 84 | {
|
73 |
| - if (0 === $this->queue->count()) { |
74 |
| - return 0; |
75 |
| - } |
76 |
| - |
77 |
| - $queue = clone $this->queue; |
78 |
| - $this->queue->clear(); |
79 |
| - |
80 |
| - try { |
81 |
| - $responses = $this->httpAdapter->sendRequests($queue->all()); |
82 |
| - } catch (MultiHttpAdapterException $e) { |
83 |
| - // Handle all networking errors: php-http only throws an exception |
84 |
| - // if no response is available. |
85 |
| - $collection = new ExceptionCollection(); |
86 |
| - foreach ($e->getExceptions() as $exception) { |
87 |
| - // php-http only throws an exception if no response is available |
88 |
| - if (!$exception->getResponse()) { |
89 |
| - // Assume networking error if no response was returned. |
90 |
| - $collection->add( |
91 |
| - ProxyUnreachableException::proxyUnreachable($exception) |
92 |
| - ); |
93 |
| - } |
94 |
| - } |
95 |
| - |
96 |
| - foreach ($this->handleErrorResponses($e->getResponses()) as $exception) { |
97 |
| - $collection->add($exception); |
98 |
| - } |
99 |
| - |
100 |
| - throw $collection; |
101 |
| - } |
102 |
| - |
103 |
| - $exceptions = $this->handleErrorResponses($responses); |
104 |
| - if (count($exceptions) > 0) { |
105 |
| - throw new ExceptionCollection($exceptions); |
106 |
| - } |
107 |
| - |
108 |
| - return count($queue); |
| 85 | + return $this->httpAdapter->flush(); |
109 | 86 | }
|
110 | 87 |
|
111 | 88 | /**
|
112 |
| - * Add invalidation reqest to the queue |
| 89 | + * Get options resolver with default settings. |
113 | 90 | *
|
114 |
| - * @param string $method HTTP method |
115 |
| - * @param string $url HTTP URL |
116 |
| - * @param array $headers HTTP headers |
| 91 | + * @return OptionsResolver |
117 | 92 | */
|
118 |
| - protected function queueRequest($method, $url, array $headers = []) |
| 93 | + protected function getDefaultOptions() |
119 | 94 | {
|
120 |
| - $this->queue->add(new InvalidationRequest($method, $url, $headers)); |
121 |
| - } |
| 95 | + $resolver = new OptionsResolver(); |
| 96 | + $resolver->setDefault('base_uri', null); |
122 | 97 |
|
123 |
| - /** |
124 |
| - * Initialize the request queue |
125 |
| - * |
126 |
| - * @param array $servers |
127 |
| - * @param string $baseUri |
128 |
| - */ |
129 |
| - protected function initQueue(array $servers, $baseUri) |
130 |
| - { |
131 |
| - $this->queue = new RequestQueue($servers, $baseUri); |
| 98 | + return $resolver; |
132 | 99 | }
|
133 | 100 |
|
134 | 101 | /**
|
135 |
| - * @param ResponseInterface[] $responses |
| 102 | + * Create a request and queue it with the HttpAdapter. |
136 | 103 | *
|
137 |
| - * @return ProxyResponseException[] |
| 104 | + * @param string $method |
| 105 | + * @param string|UriInterface $url |
| 106 | + * @param array $headers |
138 | 107 | */
|
139 |
| - private function handleErrorResponses(array $responses) |
| 108 | + protected function queueRequest($method, $url, array $headers) |
140 | 109 | {
|
141 |
| - $exceptions = []; |
142 |
| - |
143 |
| - foreach ($responses as $response) { |
144 |
| - if ($response->getStatusCode() >= 400 |
145 |
| - && $response->getStatusCode() < 600 |
146 |
| - ) { |
147 |
| - $exceptions[] = ProxyResponseException::proxyResponse($response); |
148 |
| - } |
149 |
| - } |
150 |
| - |
151 |
| - return $exceptions; |
| 110 | + $this->httpAdapter->invalidate( |
| 111 | + $this->messageFactory->createRequest($method, $url, $headers) |
| 112 | + ); |
152 | 113 | }
|
153 | 114 |
|
154 | 115 | /**
|
|
0 commit comments