This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathEventRegistrar.php
270 lines (217 loc) · 9.07 KB
/
EventRegistrar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php namespace ostark\upper;
use craft\base\Element;
use craft\elements\db\ElementQuery;
use craft\events\ElementEvent;
use craft\events\ElementStructureEvent;
use craft\events\MoveElementEvent;
use craft\events\PopulateElementEvent;
use craft\events\RegisterCacheOptionsEvent;
use craft\events\SectionEvent;
use craft\events\TemplateEvent;
use craft\helpers\ElementHelper;
use craft\services\Elements;
use craft\services\Sections;
use craft\services\Structures;
use craft\utilities\ClearCaches;
use craft\web\Response;
use craft\web\View;
use ostark\upper\events\CacheResponseEvent;
use ostark\upper\events\PurgeEvent;
use ostark\upper\jobs\PurgeCacheJob;
use yii\base\Event;
/**
* Class EventRegistrar
*
* @package ostark\upper
*/
class EventRegistrar
{
public static function registerUpdateEvents()
{
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function ($event) {
static::handleUpdateEvent($event);
});
Event::on(Element::class, Element::EVENT_AFTER_MOVE_IN_STRUCTURE, function ($event) {
static::handleUpdateEvent($event);
});
Event::on(Elements::class, Elements::EVENT_AFTER_DELETE_ELEMENT, function ($event) {
static::handleUpdateEvent($event);
});
Event::on(Structures::class, Structures::EVENT_AFTER_MOVE_ELEMENT, function ($event) {
static::handleUpdateEvent($event);
});
Event::on(Sections::class, Sections::EVENT_AFTER_SAVE_SECTION, function ($event) {
static::handleUpdateEvent($event);
});
}
public static function registerFrontendEvents()
{
// No need to continue when in cli mode
if (\Craft::$app instanceof \craft\console\Application) {
return false;
}
// HTTP request object
$request = \Craft::$app->getRequest();
// Don't cache CP, LivePreview, Action, Non-GET requests
if ($request->getIsCpRequest() ||
$request->getIsLivePreview() ||
$request->getIsActionRequest() ||
!$request->getIsGet()
) {
$response = \Craft::$app->getResponse();
$response->addCacheControlDirective('private');
$response->addCacheControlDirective('no-cache');
return false;
}
// Collect tags
Event::on(ElementQuery::class, ElementQuery::EVENT_AFTER_POPULATE_ELEMENT, function (PopulateElementEvent $event) {
// Don't collect MatrixBlock and User elements for now
if (!Plugin::getInstance()->getSettings()->isCachableElement(get_class($event->element))) {
return;
}
// Tag with GlobalSet handle
if ($event->element instanceof \craft\elements\GlobalSet) {
Plugin::getInstance()->getTagCollection()->add($event->element->handle);
}
// Add to collection
Plugin::getInstance()->getTagCollection()->addTagsFromElement($event->row);
});
// Add the tags to the response header
Event::on(View::class, View::EVENT_AFTER_RENDER_PAGE_TEMPLATE, function (TemplateEvent $event) {
/** @var \yii\web\Response $response */
$response = \Craft::$app->getResponse();
$plugin = Plugin::getInstance();
$tags = $plugin->getTagCollection()->getAll();
$settings = $plugin->getSettings();
$headers = $response->getHeaders();
// Make existing cache-control headers accessible
$response->setCacheControlDirectiveFromString($headers->get('cache-control'));
// Don't cache if private | no-cache set already
if ($response->hasCacheControlDirective('private') || $response->hasCacheControlDirective('no-cache')) {
$headers->set(Plugin::INFO_HEADER_NAME, 'BYPASS');
return;
}
// MaxAge or defaultMaxAge?
$maxAge = $response->getMaxAge() ?? $settings->defaultMaxAge;
// Set Headers
$response->setTagHeader($settings->getTagHeaderName(), $tags, $settings->getHeaderTagDelimiter());
$response->setSharedMaxAge($maxAge);
$headers->set(Plugin::INFO_HEADER_NAME, "CACHED: " . date(\DateTime::ISO8601));
$plugin->trigger($plugin::EVENT_AFTER_SET_TAG_HEADER, new CacheResponseEvent([
'tags' => $tags,
'maxAge' => $maxAge,
'requestUrl' => \Craft::$app->getRequest()->getUrl(),
'headers' => $response->getHeaders()->toArray()
]
));
});
}
public static function registerCpEvents()
{
// Register cache purge checkbox
Event::on(
ClearCaches::class,
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
function (RegisterCacheOptionsEvent $event) {
$driver = ucfirst(Plugin::getInstance()->getSettings()->driver);
$event->options[] = [
'key' => 'upper-purge-all',
'label' => \Craft::t('upper', 'Upper ({driver})', ['driver' => $driver]),
'action' => function () {
Plugin::getInstance()->getPurger()->purgeAll();
},
];
}
);
}
public static function registerFallback()
{
Event::on(Plugin::class, Plugin::EVENT_AFTER_SET_TAG_HEADER, function (CacheResponseEvent $event) {
// not tagged?
if (0 == count($event->tags)) {
return;
}
// fulltext or array
$tags = \Craft::$app->getDb()->getIsMysql()
? implode(" ", $event->tags)
: str_replace(['[', ']'], ['{', '}'], json_encode($event->tags) ?: '[]');
// in order to have a unique (collitions are possible) identifier by url with a fixed length
$urlHash = md5($event->requestUrl);
try {
// Insert item
\Craft::$app->getDb()->createCommand()
->upsert(
// Table
Plugin::CACHE_TABLE,
// Identifier
['urlHash' => $urlHash],
// Data
[
'urlHash' => $urlHash,
'url' => $event->requestUrl,
'tags' => $tags,
'headers' => json_encode($event->headers),
'siteId' => \Craft::$app->getSites()->currentSite->id
]
)
->execute();
} catch (\Exception $e) {
\Craft::warning("Failed to register fallback.", "upper");
}
});
}
/**
* @param \yii\base\Event $event
*/
protected static function handleUpdateEvent(Event $event)
{
$tags = [];
if ($event instanceof ElementEvent) {
if (!Plugin::getInstance()->getSettings()->isCachableElement(get_class($event->element))) {
return;
}
// Prevent purge on updates of drafts or revisions
if (ElementHelper::isDraftOrRevision($event->element)) {
return;
}
// Prevent purge on resaving
if (property_exists($event->element, 'resaving') && $event->element->resaving === true) {
return;
}
if ($event->element instanceof \craft\elements\GlobalSet && is_string($event->element->handle)) {
$tags[] = $event->element->handle;
} elseif ($event->element instanceof \craft\elements\Asset && $event->isNew) {
$tags[] = (string)$event->element->volumeId;
} else {
if (isset($event->element->sectionId)) {
$tags[] = Plugin::TAG_PREFIX_SECTION . $event->element->sectionId;
}
if (!$event->isNew) {
$tags[] = Plugin::TAG_PREFIX_ELEMENT . $event->element->getId();
}
}
}
if ($event instanceof SectionEvent) {
$tags[] = Plugin::TAG_PREFIX_SECTION . $event->section->id;
}
if ($event instanceof MoveElementEvent or $event instanceof ElementStructureEvent) {
$tags[] = Plugin::TAG_PREFIX_STRUCTURE . $event->structureId;
}
if (count($tags) === 0) {
return;
}
foreach ($tags as $tag) {
$tag = Plugin::getInstance()->getTagCollection()->prepareTag($tag);
$purgeEvent = new PurgeEvent([
'tag' => $tag,
]);
Plugin::getInstance()->trigger(Plugin::EVENT_BEFORE_PURGE, $purgeEvent);
// Push to queue
\Craft::$app->getQueue()->push(new PurgeCacheJob([
'tag' => $purgeEvent->tag
]
));
Plugin::getInstance()->trigger(Plugin::EVENT_AFTER_PURGE, $purgeEvent);
}
}
}