1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cache Flag plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Flag and clear template caches. |
6
|
|
|
* |
7
|
|
|
* @link https://vaersaagod.no |
8
|
|
|
* @copyright Copyright (c) 2018 Mats Mikkel Rummelhoff |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace mmikkel\cacheflag\controllers; |
12
|
|
|
|
13
|
|
|
use mmikkel\cacheflag\CacheFlag; |
14
|
|
|
|
15
|
|
|
use Craft; |
16
|
|
|
use craft\web\Controller; |
17
|
|
|
use yii\web\BadRequestHttpException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Mats Mikkel Rummelhoff |
21
|
|
|
* @package CacheFlag |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
class DefaultController extends Controller |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
// Public Methods |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return \yii\web\Response |
32
|
|
|
* @throws BadRequestHttpException |
33
|
|
|
* @throws \yii\base\InvalidConfigException |
34
|
|
|
*/ |
35
|
|
|
public function actionSaveFlags() |
36
|
|
|
{ |
37
|
|
|
$this->requirePostRequest(); |
38
|
|
|
$this->requireAcceptsJson(); |
39
|
|
|
|
40
|
|
|
$params = Craft::$app->getRequest()->getBodyParams(); |
41
|
|
|
$cacheFlags = $params['cacheflags'] ?? null; |
42
|
|
|
|
43
|
|
|
$error = null; |
44
|
|
|
|
45
|
|
|
foreach ($cacheFlags as $source => $flags) { |
46
|
|
|
|
47
|
|
|
$sourceArray = \explode(':', $source); |
48
|
|
|
$sourceColumn = $sourceArray[0] ?? null; |
49
|
|
|
$sourceId = $sourceArray[1] ?? null; |
50
|
|
|
|
51
|
|
|
if (!$sourceColumn || !$sourceId) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$flags = \preg_replace('/\s+/', '', $flags); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
if (!$flags) { |
59
|
|
|
CacheFlag::getInstance()->cacheFlag->deleteFlagsBySource($sourceColumn, $sourceId); |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
CacheFlag::getInstance()->cacheFlag->saveFlags($flags, $sourceColumn, $sourceId); |
63
|
|
|
} catch (\Throwable $e) { |
64
|
|
|
$error = $e->getMessage(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($error) { |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($error) { |
74
|
|
|
return $this->asJson([ |
75
|
|
|
'success' => false, |
76
|
|
|
'message' => $error, |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->asJson([ |
81
|
|
|
'success' => true, |
82
|
|
|
'message' => $this->_getRandomSuccessMessage(), |
83
|
|
|
'flags' => CacheFlag::getInstance()->cacheFlag->getAllFlags(), |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \yii\web\Response |
90
|
|
|
* @throws BadRequestHttpException |
91
|
|
|
* @throws \yii\base\InvalidConfigException |
92
|
|
|
* @deprecated since 1.1.0 |
93
|
|
|
*/ |
94
|
|
|
public function actionDeleteFlaggedCachesByFlags() |
95
|
|
|
{ |
96
|
|
|
return $this->actionInvalidateFlaggedCachesByFlags(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return \yii\web\Response |
101
|
|
|
* @throws BadRequestHttpException |
102
|
|
|
* @throws \yii\base\InvalidConfigException |
103
|
|
|
*/ |
104
|
|
|
public function actionInvalidateFlaggedCachesByFlags() |
105
|
|
|
{ |
106
|
|
|
$this->requirePostRequest(); |
107
|
|
|
$this->requireAcceptsJson(); |
108
|
|
|
|
109
|
|
|
$params = Craft::$app->getRequest()->getBodyParams(); |
110
|
|
|
$flags = $params['flags'] ?? null; |
111
|
|
|
|
112
|
|
|
if (!$flags) { |
113
|
|
|
return $this->asJson([ |
114
|
|
|
'success' => false, |
115
|
|
|
'message' => Craft::t('cache-flag', 'No flags to invalidate caches for'), |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$error = null; |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
CacheFlag::getInstance()->cacheFlag->deleteFlaggedCachesByFlags($flags); |
|
|
|
|
123
|
|
|
} catch (\Throwable $e) { |
124
|
|
|
$error = $e->getMessage(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($error) { |
128
|
|
|
return $this->asJson([ |
129
|
|
|
'success' => false, |
130
|
|
|
'message' => $error, |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->asJson([ |
135
|
|
|
'success' => true, |
136
|
|
|
'message' => Craft::t('cache-flag', 'Caches invalidated'), |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return \yii\web\Response |
142
|
|
|
* @throws BadRequestHttpException |
143
|
|
|
* @throws \craft\errors\MissingComponentException |
144
|
|
|
* @deprecated since 1.1.0 |
145
|
|
|
*/ |
146
|
|
|
public function actionDeleteAllFlaggedCaches() |
147
|
|
|
{ |
148
|
|
|
return $this->actionInvalidateAllFlaggedCaches(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return \yii\web\Response |
153
|
|
|
* @throws BadRequestHttpException |
154
|
|
|
* @throws \craft\errors\MissingComponentException |
155
|
|
|
*/ |
156
|
|
|
public function actionInvalidateAllFlaggedCaches() |
157
|
|
|
{ |
158
|
|
|
$this->requirePostRequest(); |
159
|
|
|
|
160
|
|
|
$error = null; |
161
|
|
|
|
162
|
|
|
try { |
163
|
|
|
CacheFlag::getInstance()->cacheFlag->deleteAllFlaggedCaches(); |
|
|
|
|
164
|
|
|
} catch (\Throwable $e) { |
165
|
|
|
$error = $e; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($error) { |
169
|
|
|
Craft::$app->getSession()->setError($error); |
170
|
|
|
} else { |
171
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('cache-flag', 'All flagged caches invalidated')); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $this->redirectToPostedUrl(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
private function _getRandomSuccessMessage(): string |
181
|
|
|
{ |
182
|
|
|
$messages = [ |
183
|
|
|
"Flags saved! Good day to you.", |
184
|
|
|
"Ahoy! All flags saved.", |
185
|
|
|
"Flags. Saved. High five.", |
186
|
|
|
"Flags! We haz them.", |
187
|
|
|
"Cache flags saved, friend.", |
188
|
|
|
"Yarr! Yer flags be safe.", |
189
|
|
|
"Dun dun dun. All flags saved.", |
190
|
|
|
"You are awesome. And so are your flags.", |
191
|
|
|
"Flags, so many flags. All mine!", |
192
|
|
|
"Flags flags flags flags", |
193
|
|
|
"You say save, I say sure.", |
194
|
|
|
"Before, your flags were lost. But now they are saved.", |
195
|
|
|
"My, what beautiful flags you have.", |
196
|
|
|
"Your flags are safe with me.", |
197
|
|
|
"I'll just hang on to these flags for you.", |
198
|
|
|
"If you want a symbolic gesture, don't burn the flag; save it.", |
199
|
|
|
'Those are the second biggest flags I\'ve ever seen!', |
200
|
|
|
'Put a flag on it.', |
201
|
|
|
'No worries, your flags are totally saved.', |
202
|
|
|
'I just can\'t believe Cache Flag is finally running on Craft 3', |
203
|
|
|
'Current state: flags saved.', |
204
|
|
|
'Mhmm, yummy flags been saved.', |
205
|
|
|
'Much flags. So saved.', |
206
|
|
|
'Your Cache Flags (tm) are safely saved.', |
207
|
|
|
'Flags saved! Go forth and conquer!', |
208
|
|
|
'Saving flags is my favorite thing in the world', |
209
|
|
|
'Safe and sound.', |
210
|
|
|
'It\'s getting hot in here', |
211
|
|
|
'Flags. Saved. Profit.', |
212
|
|
|
'These might just be the prettiest lil\' flags I\'ve ever saved', |
213
|
|
|
'Saving cache flags sure is fun, huh', |
214
|
|
|
'Come on! Moar flags!', |
215
|
|
|
'Thank you for these wonderful flags', |
216
|
|
|
'Cache Flags! Exactly what I needed right now!', |
217
|
|
|
'Nice one! Successfully saved your flags.', |
218
|
|
|
'Oh man, just wait until people see these flags', |
219
|
|
|
'Not gonna lie. Those are some fine flags.', |
220
|
|
|
'Your flags are safe and sound.', |
221
|
|
|
'That templatecaches table won\'t know what hit it!', |
222
|
|
|
'Tweet it out?', |
223
|
|
|
]; |
224
|
|
|
return $messages[\array_rand($messages)]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.