1 | <?php |
||
24 | abstract class HttpProxyClient implements ProxyClient |
||
25 | { |
||
26 | /** |
||
27 | * Dispatcher for invalidation HTTP requests. |
||
28 | * |
||
29 | * @var HttpDispatcher |
||
30 | */ |
||
31 | private $httpDispatcher; |
||
32 | |||
33 | /** |
||
34 | * @var RequestFactory |
||
35 | */ |
||
36 | private $requestFactory; |
||
37 | |||
38 | /** |
||
39 | * The options configured in the constructor argument or default values. |
||
40 | * |
||
41 | * @var array The resolved options |
||
42 | */ |
||
43 | protected $options; |
||
44 | |||
45 | /** |
||
46 | * Constructor. |
||
47 | * |
||
48 | * The base class has no options. |
||
49 | * |
||
50 | * @param HttpDispatcher $httpDispatcher Helper to send HTTP requests to caching proxy |
||
51 | * @param array $options Options for this client |
||
52 | * @param RequestFactory|null $messageFactory Factory for PSR-7 messages. If none supplied, |
||
53 | * a default one is created |
||
54 | */ |
||
55 | 33 | public function __construct( |
|
56 | HttpDispatcher $httpDispatcher, |
||
57 | array $options = [], |
||
58 | 1 | RequestFactory $messageFactory = null |
|
59 | 1 | ) { |
|
60 | 33 | $this->httpDispatcher = $httpDispatcher; |
|
61 | 33 | $this->options = $this->configureOptions()->resolve($options); |
|
62 | 33 | $this->requestFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
|
63 | 33 | } |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 21 | public function flush() |
|
72 | |||
73 | /** |
||
74 | * Get options resolver with default settings. |
||
75 | * |
||
76 | * @return OptionsResolver |
||
77 | */ |
||
78 | 33 | protected function configureOptions() |
|
82 | |||
83 | /** |
||
84 | * Create a request and queue it with the HTTP dispatcher. |
||
85 | * |
||
86 | * @param string $method |
||
87 | * @param string|UriInterface $url |
||
88 | * @param array $headers |
||
89 | * @param bool $validateHost see HttpDispatcher::invalidate |
||
90 | */ |
||
91 | 31 | protected function queueRequest($method, $url, array $headers, $validateHost = true) |
|
92 | { |
||
93 | 31 | $this->httpDispatcher->invalidate( |
|
94 | 31 | $this->requestFactory->createRequest($method, $url, $headers), |
|
95 | $validateHost |
||
96 | 31 | ); |
|
97 | 31 | } |
|
98 | |||
99 | /** |
||
100 | * Make sure that the tags are valid. |
||
101 | * |
||
102 | * Reusable function for proxy clients. |
||
103 | * Escapes `,` and `\n` (newline) characters. |
||
104 | * |
||
105 | * @param array $tags The tags to escape |
||
106 | * |
||
107 | * @return array Sane tags |
||
108 | */ |
||
109 | protected function escapeTags(array $tags) |
||
117 | |||
118 | /** |
||
119 | * Splits a header value into an array of values. E.g. useful for tag |
||
120 | * invalidation requests where you might need multiple requests if you |
||
121 | * want to invalidate too many cache tags (so the header would get too long). |
||
122 | * |
||
123 | * @param string $value |
||
124 | * @param int $length |
||
125 | * @param string $delimiter |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 2 | protected function splitLongHeaderValue($value, $length = 7500, $delimiter = ',') |
|
160 | } |
||
161 |