1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\ProxyClient; |
13
|
|
|
|
14
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
15
|
|
|
use Http\Message\RequestFactory; |
16
|
|
|
use Psr\Http\Message\UriInterface; |
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Base class for HTTP based caching proxy client. |
21
|
|
|
* |
22
|
|
|
* @author David de Boer <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class HttpProxyClient implements ProxyClientInterface |
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
|
35 |
|
public function __construct( |
56
|
1 |
|
HttpDispatcher $httpDispatcher, |
57
|
|
|
array $options = [], |
58
|
|
|
RequestFactory $messageFactory = null |
59
|
|
|
) { |
60
|
35 |
|
$this->httpDispatcher = $httpDispatcher; |
61
|
35 |
|
$this->options = $this->configureOptions()->resolve($options); |
62
|
35 |
|
$this->requestFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
63
|
35 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
22 |
|
public function flush() |
69
|
|
|
{ |
70
|
22 |
|
return $this->httpDispatcher->flush(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get options resolver with default settings. |
75
|
|
|
* |
76
|
|
|
* @return OptionsResolver |
77
|
|
|
*/ |
78
|
35 |
|
protected function configureOptions() |
79
|
|
|
{ |
80
|
35 |
|
return new OptionsResolver(); |
81
|
|
|
} |
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
|
|
|
*/ |
90
|
30 |
|
protected function queueRequest($method, $url, array $headers) |
91
|
|
|
{ |
92
|
30 |
|
$this->httpDispatcher->invalidate( |
93
|
30 |
|
$this->requestFactory->createRequest($method, $url, $headers) |
94
|
30 |
|
); |
95
|
30 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Make sure that the tags are valid. |
99
|
|
|
* |
100
|
|
|
* Reusable function for proxy clients. |
101
|
|
|
* Escapes `,` and `\n` (newline) characters. |
102
|
|
|
* |
103
|
|
|
* @param array $tags The tags to escape |
104
|
|
|
* |
105
|
|
|
* @return array Sane tags |
106
|
|
|
*/ |
107
|
|
|
protected function escapeTags(array $tags) |
108
|
|
|
{ |
109
|
5 |
|
array_walk($tags, function (&$tag) { |
110
|
5 |
|
$tag = str_replace([',', "\n"], ['_', '_'], $tag); |
111
|
5 |
|
}); |
112
|
|
|
|
113
|
5 |
|
return $tags; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|