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 Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Purge handler for the symfony built-in HttpCache. |
19
|
|
|
* |
20
|
|
|
* @author David Buchmann <[email protected]> |
21
|
|
|
* |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
class PurgeListener extends AccessControlledListener |
25
|
|
|
{ |
26
|
|
|
const DEFAULT_PURGE_METHOD = 'PURGE'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The purge method to use. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $purgeMethod; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* When creating the purge listener, you can configure an additional option. |
37
|
|
|
* |
38
|
|
|
* - purge_method: HTTP method that identifies purge requests. |
39
|
|
|
* |
40
|
|
|
* @param array $options Options to overwrite the default options |
41
|
|
|
* |
42
|
|
|
* @throws \InvalidArgumentException if unknown keys are found in $options |
43
|
|
|
* |
44
|
|
|
* @see AccessControlledListener::__construct |
45
|
|
|
*/ |
46
|
8 |
|
public function __construct(array $options = []) |
47
|
|
|
{ |
48
|
8 |
|
parent::__construct($options); |
49
|
|
|
|
50
|
6 |
|
$this->purgeMethod = $this->getOptionsResolver()->resolve($options)['purge_method']; |
51
|
6 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
1 |
|
public static function getSubscribedEvents() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
1 |
|
Events::PRE_INVALIDATE => 'handlePurge', |
60
|
1 |
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Look at unsafe requests and handle purge requests. |
65
|
|
|
* |
66
|
|
|
* Prevents access when the request comes from a non-authorized client. |
67
|
|
|
* |
68
|
|
|
* @param CacheEvent $event |
69
|
|
|
*/ |
70
|
5 |
|
public function handlePurge(CacheEvent $event) |
71
|
|
|
{ |
72
|
5 |
|
$request = $event->getRequest(); |
73
|
5 |
|
if ($this->purgeMethod !== $request->getMethod()) { |
74
|
1 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
if (!$this->isRequestAllowed($request)) { |
78
|
2 |
|
$event->setResponse(new Response('', 400)); |
79
|
|
|
|
80
|
2 |
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
$response = new Response(); |
84
|
2 |
|
if ($event->getKernel()->getStore()->purge($request->getUri())) { |
85
|
1 |
|
$response->setStatusCode(200, 'Purged'); |
86
|
1 |
|
} else { |
87
|
1 |
|
$response->setStatusCode(200, 'Not found'); |
88
|
|
|
} |
89
|
2 |
|
$event->setResponse($response); |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add the purge_method option. |
94
|
|
|
* |
95
|
|
|
* @return OptionsResolver |
96
|
|
|
*/ |
97
|
8 |
|
protected function getOptionsResolver() |
98
|
|
|
{ |
99
|
8 |
|
$resolver = parent::getOptionsResolver(); |
100
|
8 |
|
$resolver->setDefault('purge_method', static::DEFAULT_PURGE_METHOD); |
101
|
8 |
|
$resolver->setAllowedTypes('purge_method', 'string'); |
102
|
|
|
|
103
|
8 |
|
return $resolver; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|