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