-
Notifications
You must be signed in to change notification settings - Fork 611
/
ClearCaches.php
263 lines (239 loc) · 8.73 KB
/
ClearCaches.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\utilities;
use Craft;
use craft\base\Utility;
use craft\db\Table;
use craft\events\RegisterCacheOptionsEvent;
use craft\helpers\ArrayHelper;
use craft\helpers\FileHelper;
use craft\web\assets\clearcaches\ClearCachesAsset;
use Exception;
use yii\base\Event;
use yii\base\InvalidArgumentException;
/**
* ClearCaches represents a ClearCaches dashboard widget.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class ClearCaches extends Utility
{
/**
* @event RegisterCacheOptionsEvent The event that is triggered when registering cache options.
*
* Each option added to [[RegisterCacheOptionsEvent::$options]] should be an array that has the following keys:
*
* - `key` – An identifying key for the cache option.
* - `label` – A human-facing label for the cache option.
* - `action` – Either the path to a folder that should be cleared, or a callable that should handle the cache clearing.
* - `info` _(optional)_ – A description of the cache option.
*
* @see cacheOptions()
*/
public const EVENT_REGISTER_CACHE_OPTIONS = 'registerCacheOptions';
/**
* @event RegisterCacheOptionsEvent The event that is triggered when registering cache tag invalidation options.
*
* Each option added to [[RegisterCacheOptionsEvent::$options]] should be an array that has the following keys:
*
* - `tag` – The cache tag name that sholud be cleared.
* - `label` – A human-facing label for the cache tag option.
*
* @see tagOptions()
* @since 3.5.0
*/
public const EVENT_REGISTER_TAG_OPTIONS = 'registerTagOptions';
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Caches');
}
/**
* @inheritdoc
*/
public static function id(): string
{
return 'clear-caches';
}
/**
* @inheritdoc
*/
public static function icon(): ?string
{
return 'trash';
}
/**
* @inheritdoc
*/
public static function contentHtml(): string
{
$cacheOptions = [];
$tagOptions = [];
foreach (self::cacheOptions() as $cacheOption) {
$cacheOptions[] = [
'label' => $cacheOption['label'],
'value' => $cacheOption['key'],
'info' => $cacheOption['info'] ?? null,
];
}
foreach (self::tagOptions() as $tagOption) {
$tagOptions[] = [
'label' => $tagOption['label'],
'value' => $tagOption['tag'],
];
}
ArrayHelper::multisort($cacheOptions, 'label');
$view = Craft::$app->getView();
$view->registerAssetBundle(ClearCachesAsset::class);
$view->registerJs('new Craft.ClearCachesUtility(\'clear-caches\');');
return $view->renderTemplate('_components/utilities/ClearCaches.twig', [
'cacheOptions' => $cacheOptions,
'tagOptions' => $tagOptions,
]);
}
/**
* Returns all cache options
*
* @return array
*/
public static function cacheOptions(): array
{
$pathService = Craft::$app->getPath();
$options = [
[
'key' => 'data',
'label' => Craft::t('app', 'Data caches'),
'info' => Craft::t('app', 'Anything cached with `Craft::$app->cache->set()`'),
'action' => [Craft::$app->getCache(), 'flush'],
],
[
'key' => 'asset',
'label' => Craft::t('app', 'Asset caches'),
'info' => Craft::t('app', 'Local copies of remote images, generated thumbnails'),
'action' => function() use ($pathService) {
$dirs = [
$pathService->getAssetSourcesPath(false),
$pathService->getAssetsIconsPath(false),
$pathService->getImageTransformsPath(false),
];
foreach ($dirs as $dir) {
try {
FileHelper::clearDirectory($dir);
} catch (InvalidArgumentException) {
// the directory doesn't exist
}
}
},
],
[
'key' => 'compiled-templates',
'label' => Craft::t('app', 'Compiled templates'),
'info' => Craft::t('app', 'Contents of {path}', [
'path' => '`storage/runtime/compiled_templates/`',
]),
'action' => $pathService->getCompiledTemplatesPath(false),
],
[
'key' => 'compiled-classes',
'label' => Craft::t('app', 'Compiled classes'),
'info' => Craft::t('app', 'Contents of {path}', [
'path' => '`storage/runtime/compiled_classes/`',
]),
'action' => $pathService->getCompiledClassesPath(false),
],
[
'key' => 'cp-resources',
'label' => Craft::t('app', 'Control panel resources'),
'info' => Craft::t('app', 'Contents of {path}', [
'path' => '`web/cpresources/`',
]),
'action' => function() {
$basePath = Craft::$app->getConfig()->getGeneral()->resourceBasePath;
$request = Craft::$app->getRequest();
if (
$request->getIsConsoleRequest() &&
$request->isWebrootAliasSetDynamically &&
str_starts_with($basePath, '@webroot')
) {
throw new Exception("Unable to clear control panel resources because the location isn't known for console commands.\n" .
"Explicitly set the @webroot alias in config/general.php to avoid this error.\n" .
'See https://craftcms.com/docs/4.x/config/#aliases for more info.');
}
$basePath = Craft::getAlias($basePath);
if ($basePath !== false) {
FileHelper::clearDirectory($basePath, [
'except' => ['.gitignore'],
]);
}
},
],
[
'key' => 'temp-files',
'label' => Craft::t('app', 'Temp files'),
'info' => Craft::t('app', 'Contents of {path}', [
'path' => '`storage/runtime/temp/`',
]),
'action' => $pathService->getTempPath(),
],
[
'key' => 'transform-indexes',
'label' => Craft::t('app', 'Asset transform index'),
'info' => Craft::t('app', 'Record of generated image transforms'),
'action' => function() {
Craft::$app->getDb()->createCommand()
->truncateTable(Table::IMAGETRANSFORMINDEX)
->execute();
},
],
[
'key' => 'asset-indexing-data',
'label' => Craft::t('app', 'Asset indexing data'),
'action' => function() {
Craft::$app->getDb()->createCommand()
->truncateTable(Table::ASSETINDEXDATA)
->execute();
},
],
];
$event = new RegisterCacheOptionsEvent([
'options' => $options,
]);
Event::trigger(self::class, self::EVENT_REGISTER_CACHE_OPTIONS, $event);
ArrayHelper::multisort($event->options, 'label');
return $event->options;
}
/**
* Returns all cache tag invalidation options.
*
* @return array
* @since 3.5.0
*/
public static function tagOptions(): array
{
$options = [
[
'tag' => 'template',
'label' => Craft::t('app', 'Template caches'),
],
];
if (Craft::$app->getConfig()->getGeneral()->enableGql) {
$options[] = [
'tag' => 'graphql',
'label' => Craft::t('app', 'GraphQL queries'),
];
}
$event = new RegisterCacheOptionsEvent([
'options' => $options,
]);
Event::trigger(self::class, self::EVENT_REGISTER_TAG_OPTIONS, $event);
ArrayHelper::multisort($event->options, 'label');
return $event->options;
}
}