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\SymfonyCache; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\Exception\ExceptionCollection; |
15
|
|
|
use FOS\HttpCache\ProxyClient\Dispatcher; |
16
|
|
|
use Psr\Http\Message\RequestInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This Dispatcher directly calls the Symfony HttpCache kernel without |
22
|
|
|
* executing any actual HTTP requests. |
23
|
|
|
* |
24
|
|
|
* This dispatcher can only be used if your application runs on one single |
25
|
|
|
* server. |
26
|
|
|
* |
27
|
|
|
* If you use Varnish/Nginx or have multiple servers, this client can not be |
28
|
|
|
* used. |
29
|
|
|
* |
30
|
|
|
* @author Yanick Witschi <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class KernelDispatcher implements Dispatcher |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var HttpCacheProvider |
36
|
|
|
*/ |
37
|
|
|
private $httpCacheProvider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $queue = []; |
43
|
|
|
|
44
|
|
|
public function __construct(HttpCacheProvider $httpCacheProvider) |
45
|
|
|
{ |
46
|
|
|
$this->httpCacheProvider = $httpCacheProvider; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function invalidate(RequestInterface $invalidationRequest, $validateHost = true) |
53
|
|
|
{ |
54
|
|
|
$request = Request::create( |
55
|
|
|
$invalidationRequest->getUri(), |
56
|
|
|
$invalidationRequest->getMethod(), |
57
|
|
|
[], |
58
|
|
|
[], |
59
|
|
|
[], |
60
|
|
|
['REMOTE_ADDR' => '127.0.0.1'], |
61
|
|
|
$invalidationRequest->getBody()->getContents() |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
// Add headers |
65
|
|
|
$headers = $invalidationRequest->getHeaders(); |
66
|
|
|
foreach ($headers as $name => $values) { |
67
|
|
|
$name = strtolower($name); |
68
|
|
|
|
69
|
|
|
// symfony won't look at the cookies header but parses it when creating the request |
70
|
|
|
if ('cookie' === $name) { |
71
|
|
|
foreach ($values as $value) { |
72
|
|
|
foreach (explode(';', $value) as $cookieString) { |
73
|
|
|
$chunks = explode('=', $cookieString, 2); |
74
|
|
|
$request->cookies->add([trim($chunks[0]) => trim($chunks[1])]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$request->headers->set($name, $values); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->queue[sha1($request)] = $request; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function flush() |
91
|
|
|
{ |
92
|
|
|
$queue = $this->queue; |
93
|
|
|
$this->queue = []; |
94
|
|
|
|
95
|
|
|
$exceptions = new ExceptionCollection(); |
96
|
|
|
|
97
|
|
|
$httpCache = $this->httpCacheProvider->getHttpCache(); |
98
|
|
|
|
99
|
|
|
foreach ($queue as $request) { |
100
|
|
|
try { |
101
|
|
|
$httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, false); |
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
$exceptions->add($e); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (count($exceptions)) { |
108
|
|
|
throw $exceptions; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return count($queue); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|