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
|
|
|
* An implementation of Dispatcher that allows direct calling of the |
22
|
|
|
* Symfony HttpCache kernel without executing a real HTTP request. |
23
|
|
|
* It can only be used if you have a single node setup of Symfony and serves |
24
|
|
|
* as kind of a shortcut for easier configuration. |
25
|
|
|
* If you use Varnish or have a multiple node Symfony setup, this client is entirely |
26
|
|
|
* useless and cannot be used. |
27
|
|
|
* |
28
|
|
|
* @author Yanick Witschi <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class KernelDispatcher implements Dispatcher |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var HttpCacheProvider |
34
|
|
|
*/ |
35
|
|
|
private $kernel; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $queue = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* KernelClient constructor. |
44
|
|
|
* |
45
|
|
|
* @param HttpCacheProvider $kernel |
46
|
|
|
*/ |
47
|
|
|
public function __construct(HttpCacheProvider $kernel) |
48
|
|
|
{ |
49
|
|
|
$this->kernel = $kernel; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function invalidate(RequestInterface $invalidationRequest, $validateHost = true) |
56
|
|
|
{ |
57
|
|
|
$request = Request::create( |
58
|
|
|
$invalidationRequest->getUri(), |
59
|
|
|
$invalidationRequest->getMethod(), |
60
|
|
|
[], |
61
|
|
|
[], |
62
|
|
|
[], |
63
|
|
|
['REMOTE_ADDR' => '127.0.0.1'], |
64
|
|
|
$invalidationRequest->getBody()->getContents() |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
// Add headers |
68
|
|
|
$headers = $invalidationRequest->getHeaders(); |
69
|
|
|
foreach ($headers as $name => $values) { |
70
|
|
|
$name = strtolower($name); |
71
|
|
|
|
72
|
|
|
if ('cookie' === $name) { |
73
|
|
|
foreach ($values as $value) { |
74
|
|
|
foreach (explode(';', $value) as $cookieString) { |
75
|
|
|
$chunks = explode('=', $cookieString, 2); |
76
|
|
|
$request->cookies->add([trim($chunks[0]) => trim($chunks[1])]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$request->headers->set($name, $values); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->queue[sha1($request)] = $request; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function flush() |
93
|
|
|
{ |
94
|
|
|
$queue = $this->queue; |
95
|
|
|
$this->queue = []; |
96
|
|
|
|
97
|
|
|
$exceptions = new ExceptionCollection(); |
98
|
|
|
|
99
|
|
|
$httpCache = $this->kernel->getHttpCache(); |
100
|
|
|
|
101
|
|
|
foreach ($queue as $request) { |
102
|
|
|
try { |
103
|
|
|
$httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, false); |
104
|
|
|
} catch (\Exception $e) { |
105
|
|
|
$exceptions->add($e); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (count($exceptions)) { |
110
|
|
|
throw $exceptions; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return count($queue); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|