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 \Exception \InvalidArgumentException ;
15
+ use FOS \HttpCache \ProxyClient \Http \HttpAdapter ;
16
+ use FOS \HttpCache \ProxyClient \Http \HttpAdapterInterface ;
17
+ use FOS \HttpCache \ProxyClient \Http \HttpAsyncAdapter ;
18
+ use Http \Client \HttpAsyncClient ;
19
+ use Http \Client \HttpClient ;
20
+ use Http \Discovery \HttpClientDiscovery ;
23
21
24
22
/**
25
23
* Abstract caching proxy client
@@ -31,124 +29,46 @@ abstract class AbstractProxyClient implements ProxyClientInterface
31
29
/**
32
30
* HTTP client
33
31
*
34
- * @var HttpAdapter
32
+ * @var HttpAdapterInterface
35
33
*/
36
- private $ httpAdapter ;
37
-
38
- /**
39
- * Request queue
40
- *
41
- * @var RequestQueue
42
- */
43
- protected $ queue ;
34
+ protected $ httpAdapter ;
44
35
45
36
/**
46
37
* Constructor
47
38
*
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.
39
+ * @param array $servers Caching proxy server hostnames or IP
40
+ * addresses, including port if not port 80.
41
+ * E.g. ['127.0.0.1:6081']
42
+ * @param string $baseUri Default application hostname, optionally
43
+ * including base URL, for purge and refresh
44
+ * requests (optional). This is required if
45
+ * you purge and refresh paths instead of
46
+ * absolute URLs.
47
+ * @param HttpClient|HttpAsyncClient|null $httpClient If no HTTP client is supplied, a default
48
+ * one will be created.
58
49
*/
59
50
public function __construct (
60
51
array $ servers ,
61
52
$ baseUri = null ,
62
- HttpAdapter $ httpAdapter = null
53
+ $ httpClient = null
63
54
) {
64
- $ this ->httpAdapter = $ httpAdapter ?: HttpAdapterDiscovery::find ();
65
- $ this ->initQueue ($ servers , $ baseUri );
55
+ if ($ httpClient instanceof HttpClient) {
56
+ $ this ->httpAdapter = new HttpAdapter ($ servers , $ baseUri , $ httpClient );
57
+ } elseif ($ httpClient instanceof HttpAsyncClient) {
58
+ $ this ->httpAdapter = new HttpAsyncAdapter ($ servers , $ baseUri , $ httpClient );
59
+ } elseif (null === $ httpClient ) {
60
+ $ this ->httpAdapter = new HttpAdapter ($ servers , $ baseUri , HttpClientDiscovery::find ());
61
+ } else {
62
+ throw new InvalidArgumentException ('client must either be null or implement HttpClient or HttpAsyncClient ' );
63
+ }
66
64
}
67
65
68
66
/**
69
67
* {@inheritdoc}
70
68
*/
71
69
public function flush ()
72
70
{
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 );
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
- protected function queueRequest ($ method , $ url , array $ headers = [])
119
- {
120
- $ this ->queue ->add (new InvalidationRequest ($ method , $ url , $ headers ));
121
- }
122
-
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 );
132
- }
133
-
134
- /**
135
- * @param ResponseInterface[] $responses
136
- *
137
- * @return ProxyResponseException[]
138
- */
139
- private function handleErrorResponses (array $ responses )
140
- {
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 ;
71
+ return $ this ->httpAdapter ->flush ();
152
72
}
153
73
154
74
/**
0 commit comments