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\EventDispatcher\EventDispatcher; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait for enhanced Symfony reverse proxy based on the symfony kernel component. |
23
|
|
|
* |
24
|
|
|
* Your kernel needs to implement CacheInvalidatorInterface and redeclare the |
25
|
|
|
* fetch method as public. (The latter is needed because the trait declaring it |
26
|
|
|
* public does not satisfy the interface for whatever reason. See also |
27
|
|
|
* http://stackoverflow.com/questions/31877844/php-trait-exposing-a-method-and-interfaces ) |
28
|
|
|
* |
29
|
|
|
* CacheInvalidator kernels support event listeners that can act on the |
30
|
|
|
* events defined in FOS\HttpCache\SymfonyCache\Events and may alter the |
31
|
|
|
* request flow. |
32
|
|
|
* |
33
|
|
|
* If your kernel overwrites any of the methods defined in this trait, make |
34
|
|
|
* sure to also call the trait method. You might get into issues with the order |
35
|
|
|
* of events, in which case you will need to copy event triggering into your |
36
|
|
|
* kernel. |
37
|
|
|
* |
38
|
|
|
* @author Jérôme Vieilledent <[email protected]> (courtesy of eZ Systems AS) |
39
|
|
|
* @author David Buchmann <[email protected]> |
40
|
|
|
* |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
trait EventDispatchingHttpCache |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var EventDispatcherInterface |
47
|
|
|
*/ |
48
|
|
|
private $eventDispatcher; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get event dispatcher. |
52
|
|
|
* |
53
|
|
|
* @return EventDispatcherInterface |
54
|
|
|
*/ |
55
|
9 |
|
public function getEventDispatcher() |
56
|
|
|
{ |
57
|
9 |
|
if (null === $this->eventDispatcher) { |
58
|
9 |
|
$this->eventDispatcher = new EventDispatcher(); |
59
|
9 |
|
} |
60
|
|
|
|
61
|
9 |
|
return $this->eventDispatcher; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Add an event subscriber. |
66
|
|
|
* |
67
|
|
|
* @see EventDispatcherInterface::addSubscriber |
68
|
|
|
*/ |
69
|
9 |
|
public function addSubscriber(EventSubscriberInterface $subscriber) |
70
|
|
|
{ |
71
|
9 |
|
$this->getEventDispatcher()->addSubscriber($subscriber); |
72
|
9 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add an event listener to this HttpCache. |
76
|
|
|
* |
77
|
|
|
* @see EventDispatcherInterface::addListener |
78
|
|
|
*/ |
79
|
5 |
|
public function addListener($eventName, $listener, $priority = 0) |
80
|
|
|
{ |
81
|
|
|
$this->getEventDispatcher()->addListener($eventName, $listener, $priority); |
82
|
5 |
|
} |
83
|
|
|
|
84
|
5 |
|
/** |
85
|
2 |
|
* {@inheritdoc} |
86
|
|
|
* |
87
|
|
|
* Adding the Events::PRE_HANDLE and Events::POST_HANDLE events. |
88
|
3 |
|
*/ |
89
|
|
|
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) |
90
|
3 |
|
{ |
91
|
|
|
// trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used. |
92
|
|
|
class_exists(CacheEvent::class); |
93
|
|
|
|
94
|
|
|
if ($response = $this->dispatch(Events::PRE_HANDLE, $request)) { |
95
|
|
|
return $this->dispatch(Events::POST_HANDLE, $request, $response); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$response = parent::handle($request, $type, $catch); |
99
|
|
|
|
100
|
2 |
|
return $this->dispatch(Events::POST_HANDLE, $request, $response); |
101
|
|
|
} |
102
|
2 |
|
|
103
|
2 |
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
* |
106
|
|
|
* Trigger event to alter response before storing it in the cache. |
107
|
|
|
*/ |
108
|
|
|
protected function store(Request $request, Response $response) |
109
|
|
|
{ |
110
|
2 |
|
$response = $this->dispatch(Events::PRE_STORE, $request, $response); |
111
|
|
|
|
112
|
2 |
|
parent::store($request, $response); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
1 |
|
* {@inheritdoc} |
117
|
|
|
* |
118
|
|
|
* Adding the Events::PRE_INVALIDATE event. |
119
|
|
|
*/ |
120
|
|
|
protected function invalidate(Request $request, $catch = false) |
121
|
|
|
{ |
122
|
|
|
if ($response = $this->dispatch(Events::PRE_INVALIDATE, $request)) { |
123
|
|
|
return $response; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return parent::invalidate($request, $catch); |
127
|
|
|
} |
128
|
9 |
|
|
129
|
|
|
/** |
130
|
9 |
|
* Dispatch an event if needed. |
131
|
9 |
|
* |
132
|
9 |
|
* @param string $name Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events |
133
|
9 |
|
* @param Request $request |
134
|
9 |
|
* @param Response|null $response If already available |
135
|
|
|
* |
136
|
9 |
|
* @return Response The response to return, which might be provided/altered by a listener |
137
|
|
|
*/ |
138
|
|
|
protected function dispatch($name, Request $request, Response $response = null) |
139
|
|
|
{ |
140
|
|
|
if ($this->getEventDispatcher()->hasListeners($name)) { |
141
|
|
|
$event = new CacheEvent($this, $request, $response); |
142
|
|
|
$this->getEventDispatcher()->dispatch($name, $event); |
143
|
|
|
$response = $event->getResponse(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $response; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|