|
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\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\RequestMatcher; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Purge handler for the symfony built-in HttpCache. |
|
21
|
|
|
* |
|
22
|
|
|
* @author David Buchmann <[email protected]> |
|
23
|
|
|
* |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
class PurgeSubscriber extends AccessControlledSubscriber |
|
27
|
|
|
{ |
|
28
|
|
|
const DEFAULT_PURGE_METHOD = 'PURGE'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The options configured in the constructor argument or default values. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $options = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* When creating this subscriber, you can configure a number of options. |
|
39
|
|
|
* |
|
40
|
|
|
* - purge_method: HTTP method that identifies purge requests. |
|
41
|
|
|
* - purge_client_matcher: RequestMatcher to identify valid purge clients. |
|
42
|
|
|
* - purge_client_ips: IP or array of IPs that are allowed to purge. |
|
43
|
|
|
* |
|
44
|
|
|
* Only set one of purge_client_ips and purge_client_matcher. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $options Options to overwrite the default options |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \InvalidArgumentException if unknown keys are found in $options |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function __construct(array $options = []) |
|
51
|
|
|
{ |
|
52
|
6 |
|
$resolver = new OptionsResolver(); |
|
53
|
6 |
|
if (method_exists($resolver, 'setDefined')) { |
|
54
|
|
|
// this was only added in symfony 2.6 |
|
55
|
6 |
|
$resolver->setDefined(['purge_client_matcher', 'purge_client_ips', 'purge_method']); |
|
56
|
6 |
|
} else { |
|
57
|
|
|
$resolver->setOptional(['purge_client_matcher', 'purge_client_ips', 'purge_method']); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
6 |
|
$resolver->setDefaults([ |
|
60
|
6 |
|
'purge_client_matcher' => null, |
|
61
|
6 |
|
'purge_client_ips' => null, |
|
62
|
6 |
|
'purge_method' => static::DEFAULT_PURGE_METHOD, |
|
63
|
6 |
|
]); |
|
64
|
|
|
|
|
65
|
6 |
|
$this->options = $resolver->resolve($options); |
|
66
|
|
|
|
|
67
|
5 |
|
parent::__construct($this->options['purge_client_matcher'], $this->options['purge_client_ips']); |
|
68
|
5 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getSubscribedEvents() |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
Events::PRE_INVALIDATE => 'handlePurge', |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Look at unsafe requests and handle purge requests. |
|
82
|
|
|
* |
|
83
|
|
|
* Prevents access when the request comes from a non-authorized client. |
|
84
|
|
|
* |
|
85
|
|
|
* @param CacheEvent $event |
|
86
|
|
|
*/ |
|
87
|
5 |
|
public function handlePurge(CacheEvent $event) |
|
88
|
|
|
{ |
|
89
|
5 |
|
$request = $event->getRequest(); |
|
90
|
5 |
|
if ($this->options['purge_method'] !== $request->getMethod()) { |
|
91
|
1 |
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
if (!$this->isRequestAllowed($request)) { |
|
95
|
2 |
|
$event->setResponse(new Response('', 400)); |
|
96
|
|
|
|
|
97
|
2 |
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
$response = new Response(); |
|
101
|
2 |
|
if ($event->getKernel()->getStore()->purge($request->getUri())) { |
|
102
|
1 |
|
$response->setStatusCode(200, 'Purged'); |
|
103
|
1 |
|
} else { |
|
104
|
1 |
|
$response->setStatusCode(200, 'Not found'); |
|
105
|
|
|
} |
|
106
|
2 |
|
$event->setResponse($response); |
|
107
|
2 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.