1 | <?php |
||
29 | abstract class AbstractProxyClient implements ProxyClientInterface |
||
30 | { |
||
31 | /** |
||
32 | * HTTP client |
||
33 | * |
||
34 | * @var HttpAdapter |
||
35 | */ |
||
36 | private $httpAdapter; |
||
37 | |||
38 | /** |
||
39 | * Request queue |
||
40 | * |
||
41 | * @var RequestQueue |
||
42 | */ |
||
43 | protected $queue; |
||
44 | |||
45 | /** |
||
46 | * Constructor |
||
47 | * |
||
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. |
||
58 | */ |
||
59 | 46 | public function __construct( |
|
60 | array $servers, |
||
61 | $baseUri = null, |
||
62 | HttpAdapter $httpAdapter = null |
||
63 | ) { |
||
64 | 46 | $this->httpAdapter = $httpAdapter ?: HttpAdapterDiscovery::find(); |
|
65 | 46 | $this->initQueue($servers, $baseUri); |
|
66 | 43 | } |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 39 | public function flush() |
|
72 | { |
||
73 | 39 | if (0 === $this->queue->count()) { |
|
74 | 1 | return 0; |
|
75 | } |
||
76 | |||
77 | 38 | $queue = clone $this->queue; |
|
78 | 38 | $this->queue->clear(); |
|
79 | |||
80 | 1 | try { |
|
81 | 38 | $responses = $this->httpAdapter->sendRequests($queue->all()); |
|
82 | 38 | } catch (MultiHttpAdapterException $e) { |
|
83 | // Handle all networking errors: php-http only throws an exception |
||
84 | // if no response is available. |
||
85 | 1 | $collection = new ExceptionCollection(); |
|
86 | 1 | foreach ($e->getExceptions() as $exception) { |
|
87 | // php-http only throws an exception if no response is available |
||
88 | 1 | if (!$exception->getResponse()) { |
|
89 | // Assume networking error if no response was returned. |
||
90 | 1 | $collection->add( |
|
91 | 1 | ProxyUnreachableException::proxyUnreachable($exception) |
|
92 | 1 | ); |
|
93 | 1 | } |
|
94 | 1 | } |
|
95 | |||
96 | 1 | foreach ($this->handleErrorResponses($e->getResponses()) as $exception) { |
|
97 | $collection->add($exception); |
||
98 | 1 | } |
|
99 | |||
100 | 1 | throw $collection; |
|
101 | } |
||
102 | |||
103 | 38 | $exceptions = $this->handleErrorResponses($responses); |
|
104 | 38 | if (count($exceptions) > 0) { |
|
105 | 1 | throw new ExceptionCollection($exceptions); |
|
106 | } |
||
107 | |||
108 | 37 | return count($queue); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Add invalidation reqest to the queue |
||
113 | * |
||
114 | * @param string $method HTTP method |
||
115 | * @param string $url HTTP URL |
||
116 | * @param array $headers HTTP headers |
||
117 | */ |
||
118 | 38 | protected function queueRequest($method, $url, array $headers = []) |
|
122 | |||
123 | /** |
||
124 | * Initialize the request queue |
||
125 | * |
||
126 | * @param array $servers |
||
127 | * @param string $baseUri |
||
128 | */ |
||
129 | 46 | protected function initQueue(array $servers, $baseUri) |
|
133 | |||
134 | /** |
||
135 | * @param ResponseInterface[] $responses |
||
136 | * |
||
137 | * @return ProxyResponseException[] |
||
138 | */ |
||
139 | 38 | private function handleErrorResponses(array $responses) |
|
153 | |||
154 | /** |
||
155 | * Make sure that the tags are valid. |
||
156 | * |
||
157 | * Reusable function for proxy clients. |
||
158 | * Escapes `,` and `\n` (newline) characters. |
||
159 | * |
||
160 | * @param array $tags The tags to escape. |
||
161 | * |
||
162 | * @return array Sane tags. |
||
163 | */ |
||
164 | protected function escapeTags(array $tags) |
||
172 | } |
||
173 |