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 FOS\HttpCache\ProxyClient\Http\HttpDispatcher; |
15
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
16
|
|
|
use Http\Message\RequestFactory; |
17
|
|
|
use Psr\Http\Message\UriInterface; |
18
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Base class for HTTP based caching proxy client. |
22
|
|
|
* |
23
|
|
|
* @author David de Boer <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class HttpProxyClient implements ProxyClientInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Dispatcher for invalidation HTTP requests. |
29
|
|
|
* |
30
|
|
|
* @var HttpDispatcher |
31
|
|
|
*/ |
32
|
|
|
private $httpDispatcher; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var RequestFactory |
36
|
|
|
*/ |
37
|
|
|
private $requestFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The options configured in the constructor argument or default values. |
41
|
|
|
* |
42
|
|
|
* @var array The resolved options |
43
|
|
|
*/ |
44
|
|
|
protected $options; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* The base class has no options. |
50
|
|
|
* |
51
|
|
|
* @param HttpDispatcher $httpDispatcher Helper to send HTTP requests to caching proxy |
52
|
|
|
* @param array $options Options for this client |
53
|
|
|
* @param RequestFactory|null $messageFactory Factory for PSR-7 messages. If none supplied, |
54
|
|
|
* a default one is created |
55
|
|
|
*/ |
56
|
35 |
|
public function __construct( |
57
|
|
|
HttpDispatcher $httpDispatcher, |
58
|
|
|
array $options = [], |
59
|
|
|
RequestFactory $messageFactory = null |
60
|
|
|
) { |
61
|
35 |
|
$this->httpDispatcher = $httpDispatcher; |
62
|
35 |
|
$this->options = $this->configureOptions()->resolve($options); |
63
|
35 |
|
$this->requestFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
64
|
35 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
22 |
|
public function flush() |
70
|
|
|
{ |
71
|
22 |
|
return $this->httpDispatcher->flush(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get options resolver with default settings. |
76
|
|
|
* |
77
|
|
|
* @return OptionsResolver |
78
|
|
|
*/ |
79
|
35 |
|
protected function configureOptions() |
80
|
|
|
{ |
81
|
35 |
|
return new OptionsResolver(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a request and queue it with the HTTP dispatcher. |
86
|
|
|
* |
87
|
|
|
* @param string $method |
88
|
|
|
* @param string|UriInterface $url |
89
|
|
|
* @param array $headers |
90
|
|
|
*/ |
91
|
30 |
|
protected function queueRequest($method, $url, array $headers) |
92
|
|
|
{ |
93
|
30 |
|
$this->httpDispatcher->invalidate( |
94
|
30 |
|
$this->requestFactory->createRequest($method, $url, $headers) |
95
|
30 |
|
); |
96
|
30 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Make sure that the tags are valid. |
100
|
|
|
* |
101
|
|
|
* Reusable function for proxy clients. |
102
|
|
|
* Escapes `,` and `\n` (newline) characters. |
103
|
|
|
* |
104
|
|
|
* @param array $tags The tags to escape |
105
|
|
|
* |
106
|
|
|
* @return array Sane tags |
107
|
|
|
*/ |
108
|
|
|
protected function escapeTags(array $tags) |
109
|
|
|
{ |
110
|
5 |
|
array_walk($tags, function (&$tag) { |
111
|
5 |
|
$tag = str_replace([',', "\n"], ['_', '_'], $tag); |
112
|
5 |
|
}); |
113
|
|
|
|
114
|
5 |
|
return $tags; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|