1 | <?php |
||
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 = []) |
|
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() |
|
105 | } |
||
106 |