1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCacheBundle 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\HttpCacheBundle\SymfonyCache; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\SymfonyCache\CacheEvent; |
15
|
|
|
use FOS\HttpCache\SymfonyCache\Events; |
16
|
|
|
use FOS\HttpCache\SymfonyCache\PurgeSubscriber; |
17
|
|
|
use FOS\HttpCache\SymfonyCache\RefreshSubscriber; |
18
|
|
|
use FOS\HttpCache\SymfonyCache\UserContextSubscriber; |
19
|
|
|
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Base class for enhanced Symfony reverse proxy based on the symfony FrameworkBundle HttpCache. |
28
|
|
|
* |
29
|
|
|
* This kernel supports event subscribers that can act on the events defined in |
30
|
|
|
* FOS\HttpCache\SymfonyCache\Events and may alter the request flow. |
31
|
|
|
* |
32
|
|
|
* This kernel can auto-register subscribers. Overwrite `getOptions()` in your cache kernel to |
33
|
|
|
* return an array with the key "fos_default_subscribers". The value is a bit mask to define which |
34
|
|
|
* subscribers should be added with default options. If nothing is specified, the value is |
35
|
|
|
* self::SUBSCRIBER_ALL. |
36
|
|
|
* |
37
|
|
|
* Examples: |
38
|
|
|
* - `self::SUBSCRIBER_USER_CONTEXT` (**only** user context). |
39
|
|
|
* - `self::SUBSCRIBER_NONE` (**no** native subscriber). |
40
|
|
|
* - `self::SUBSCRIBER_ALL & ~self::SUBSCRIBER_USER_CONTEXT` (**all** native ones **except** the user context one). |
41
|
|
|
* |
42
|
|
|
* Note: This class looks very similar to the FOSHttpCache library event |
43
|
|
|
* dispatching kernel, but extends the FrameworkBundle HttpCache instead of the |
44
|
|
|
* one from the HttpKernel component. |
45
|
|
|
* |
46
|
|
|
* @author Jérôme Vieilledent <[email protected]> (courtesy of eZ Systems AS) |
47
|
|
|
* @author David Buchmann <[email protected]> |
48
|
|
|
* |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
abstract class EventDispatchingHttpCache extends HttpCache |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Option for the "fos_default_subscribers" that enables all subscribers. |
55
|
|
|
* |
56
|
|
|
* See class phpdoc for examples. |
57
|
|
|
*/ |
58
|
|
|
const SUBSCRIBER_ALL = -1; // Equals to ~0 |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Option for the "fos_default_subscribers" that enables no subscribers. |
62
|
|
|
* |
63
|
|
|
* See class phpdoc for examples. |
64
|
|
|
*/ |
65
|
|
|
const SUBSCRIBER_NONE = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Option for the "fos_default_subscribers" that enables the user context subscriber. |
69
|
|
|
* |
70
|
|
|
* See class phpdoc for examples. |
71
|
|
|
*/ |
72
|
|
|
const SUBSCRIBER_USER_CONTEXT = 1; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Option for the "fos_default_subscribers" that enables the purge subscriber. |
76
|
|
|
* |
77
|
|
|
* See class phpdoc for examples. |
78
|
|
|
*/ |
79
|
|
|
const SUBSCRIBER_PURGE = 2; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Option for the "fos_default_subscribers" that enables the refresh subscriber. |
83
|
|
|
* |
84
|
|
|
* See class phpdoc for examples. |
85
|
|
|
*/ |
86
|
|
|
const SUBSCRIBER_REFRESH = 4; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var EventDispatcherInterface |
90
|
|
|
*/ |
91
|
|
|
private $eventDispatcher; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
* |
96
|
|
|
* Adding the default subscribers to the event dispatcher. |
97
|
|
|
*/ |
98
|
|
|
public function __construct(HttpKernelInterface $kernel, $cacheDir = null) |
99
|
|
|
{ |
100
|
|
|
parent::__construct($kernel, $cacheDir); |
101
|
|
|
|
102
|
|
|
foreach ($this->getDefaultSubscribers() as $subscriber) { |
103
|
|
|
$this->addSubscriber($subscriber); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
* |
110
|
|
|
* Adding the Events::PRE_HANDLE event. |
111
|
|
|
*/ |
112
|
2 |
View Code Duplication |
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) |
|
|
|
|
113
|
|
|
{ |
114
|
2 |
|
if ($this->getEventDispatcher()->hasListeners(Events::PRE_HANDLE)) { |
115
|
2 |
|
$event = new CacheEvent($this, $request); |
116
|
2 |
|
$this->getEventDispatcher()->dispatch(Events::PRE_HANDLE, $event); |
117
|
2 |
|
if ($event->getResponse()) { |
118
|
1 |
|
return $event->getResponse(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
return parent::handle($request, $type, $catch); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Add a cache kernel event subscriber that listens to events listed in |
127
|
|
|
* FOS\HttpCache\SymfonyCache\Event. |
128
|
|
|
* |
129
|
|
|
* @param EventSubscriberInterface $subscriber |
130
|
|
|
*/ |
131
|
2 |
|
public function addSubscriber(EventSubscriberInterface $subscriber) |
132
|
|
|
{ |
133
|
2 |
|
$this->getEventDispatcher()->addSubscriber($subscriber); |
134
|
2 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Made public to allow event subscribers to do refresh operations. |
138
|
|
|
* |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function fetch(Request $request, $catch = false) |
142
|
|
|
{ |
143
|
|
|
return parent::fetch($request, $catch); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
* |
149
|
|
|
* Adding the Events::PRE_INVALIDATE event. |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
protected function invalidate(Request $request, $catch = false) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
if ($this->getEventDispatcher()->hasListeners(Events::PRE_INVALIDATE)) { |
154
|
|
|
$event = new CacheEvent($this, $request); |
155
|
|
|
$this->getEventDispatcher()->dispatch(Events::PRE_INVALIDATE, $event); |
156
|
|
|
if ($event->getResponse()) { |
157
|
|
|
return $event->getResponse(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return parent::invalidate($request, $catch); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return the subscribers to be added to the event dispatcher, according to the |
166
|
|
|
* fos_default_subscribers option in `getOptions()`. |
167
|
|
|
* |
168
|
|
|
* Override this method if you want to customize subscribers or add your own subscribers. |
169
|
|
|
* |
170
|
|
|
* @return EventSubscriberInterface[] |
171
|
|
|
*/ |
172
|
4 |
|
protected function getDefaultSubscribers() |
173
|
|
|
{ |
174
|
4 |
|
$options = $this->getOptions(); |
175
|
4 |
|
$subscribers = array(); |
176
|
4 |
|
$defaultSubscribersOption = isset($options['fos_default_subscribers']) ? $options['fos_default_subscribers'] : self::SUBSCRIBER_ALL; |
177
|
4 |
|
if ($defaultSubscribersOption & self::SUBSCRIBER_USER_CONTEXT) { |
178
|
3 |
|
$subscribers[] = new UserContextSubscriber(); |
179
|
|
|
} |
180
|
4 |
|
if ($defaultSubscribersOption & self::SUBSCRIBER_PURGE) { |
181
|
2 |
|
$subscribers[] = new PurgeSubscriber(); |
182
|
|
|
} |
183
|
4 |
|
if ($defaultSubscribersOption & self::SUBSCRIBER_REFRESH) { |
184
|
2 |
|
$subscribers[] = new RefreshSubscriber(); |
185
|
|
|
} |
186
|
|
|
|
187
|
4 |
|
return $subscribers; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get event dispatcher. |
192
|
|
|
* |
193
|
|
|
* @return EventDispatcherInterface |
194
|
|
|
*/ |
195
|
2 |
|
private function getEventDispatcher() |
196
|
|
|
{ |
197
|
2 |
|
if (null === $this->eventDispatcher) { |
198
|
2 |
|
$this->eventDispatcher = new EventDispatcher(); |
199
|
|
|
} |
200
|
|
|
|
201
|
2 |
|
return $this->eventDispatcher; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.