1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FastCGI Cache Bust plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Bust the Nginx FastCGI Cache when entries are saved or created. |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\fastcgicachebust; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Element; |
15
|
|
|
use craft\base\Plugin; |
16
|
|
|
use craft\elements\Entry; |
17
|
|
|
use craft\events\DeleteTemplateCachesEvent; |
18
|
|
|
use craft\events\ElementEvent; |
19
|
|
|
use craft\events\RegisterCacheOptionsEvent; |
20
|
|
|
use craft\services\Elements; |
21
|
|
|
use craft\services\TemplateCaches; |
22
|
|
|
use craft\utilities\ClearCaches; |
23
|
|
|
use nystudio107\fastcgicachebust\models\Settings; |
24
|
|
|
use nystudio107\fastcgicachebust\services\ServicesTrait; |
25
|
|
|
use yii\base\Event; |
26
|
|
|
use yii\base\Exception; |
27
|
|
|
use function get_class; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class FastcgiCacheBust |
31
|
|
|
* |
32
|
|
|
* @author nystudio107 |
|
|
|
|
33
|
|
|
* @package FastcgiCacheBust |
|
|
|
|
34
|
|
|
* @since 1.0.0 |
|
|
|
|
35
|
|
|
*/ |
|
|
|
|
36
|
|
|
class FastcgiCacheBust extends Plugin |
37
|
|
|
{ |
38
|
|
|
// Traits |
39
|
|
|
// ========================================================================= |
40
|
|
|
|
41
|
|
|
use ServicesTrait; |
42
|
|
|
|
43
|
|
|
// Static Properties |
44
|
|
|
// ========================================================================= |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @var FastcgiCacheBust |
48
|
|
|
*/ |
49
|
|
|
public static $plugin; |
50
|
|
|
|
51
|
|
|
// Public Properties |
52
|
|
|
// ========================================================================= |
53
|
|
|
|
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public $schemaVersion = '1.0.0'; |
58
|
|
|
|
59
|
|
|
/** |
|
|
|
|
60
|
|
|
* @var bool |
61
|
|
|
*/ |
62
|
|
|
public $hasCpSection = false; |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
public $hasCpSettings = true; |
68
|
|
|
|
69
|
|
|
// Public Methods |
70
|
|
|
// ========================================================================= |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
|
|
|
|
75
|
|
|
public function init() |
76
|
|
|
{ |
77
|
|
|
parent::init(); |
78
|
|
|
self::$plugin = $this; |
79
|
|
|
|
80
|
|
|
// Handler: Elements::EVENT_AFTER_SAVE_ELEMENT |
81
|
|
|
Event::on( |
82
|
|
|
Elements::class, |
83
|
|
|
Elements::EVENT_AFTER_SAVE_ELEMENT, |
84
|
|
|
function (ElementEvent $event) { |
85
|
|
|
Craft::debug( |
86
|
|
|
'Elements::EVENT_AFTER_SAVE_ELEMENT', |
87
|
|
|
__METHOD__ |
88
|
|
|
); |
89
|
|
|
/** @var Element $element */ |
|
|
|
|
90
|
|
|
$element = $event->element; |
91
|
|
|
// Only bust the cache if it's not certain excluded element types |
92
|
|
|
if ($this->shouldBustCache($element)) { |
93
|
|
|
Craft::debug( |
94
|
|
|
'Cache busted due to saving: ' . get_class($element) . ' - ' . $element->title, |
95
|
|
|
__METHOD__ |
96
|
|
|
); |
97
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
); |
101
|
|
|
// Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES |
102
|
|
|
Event::on( |
103
|
|
|
TemplateCaches::class, |
104
|
|
|
TemplateCaches::EVENT_AFTER_DELETE_CACHES, |
|
|
|
|
105
|
|
|
static function (DeleteTemplateCachesEvent $event) { |
|
|
|
|
106
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
107
|
|
|
} |
108
|
|
|
); |
109
|
|
|
// Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
110
|
|
|
Event::on( |
111
|
|
|
ClearCaches::class, |
112
|
|
|
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
113
|
|
|
static function (RegisterCacheOptionsEvent $event) { |
114
|
|
|
$event->options[] = [ |
115
|
|
|
'key' => 'fastcgi-cache-bust', |
116
|
|
|
'label' => Craft::t('fastcgi-cache-bust', 'FastCGI Cache'), |
117
|
|
|
'action' => function () { |
118
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
119
|
|
|
}, |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
Craft::info( |
125
|
|
|
Craft::t( |
126
|
|
|
'fastcgi-cache-bust', |
127
|
|
|
'{name} plugin loaded', |
128
|
|
|
['name' => $this->name] |
129
|
|
|
), |
130
|
|
|
__METHOD__ |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Protected Methods |
135
|
|
|
// ========================================================================= |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Determine whether the cache should be busted or not based on the $element |
139
|
|
|
* |
140
|
|
|
* @param Element $element |
|
|
|
|
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
protected function shouldBustCache(Element $element): bool |
145
|
|
|
{ |
146
|
|
|
$bustCache = true; |
147
|
|
|
// Only bust the cache if the element is ENABLED or LIVE |
148
|
|
|
if (($element->getStatus() !== Element::STATUS_ENABLED) |
149
|
|
|
&& ($element->getStatus() !== Entry::STATUS_LIVE) |
150
|
|
|
) { |
151
|
|
|
$bustCache = false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $bustCache; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
|
|
|
|
158
|
|
|
* @inheritdoc |
159
|
|
|
*/ |
|
|
|
|
160
|
|
|
protected function createSettingsModel() |
161
|
|
|
{ |
162
|
|
|
return new Settings(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
|
|
|
|
166
|
|
|
* @inheritdoc |
167
|
|
|
*/ |
|
|
|
|
168
|
|
|
protected function settingsHtml(): string |
169
|
|
|
{ |
170
|
|
|
try { |
171
|
|
|
return Craft::$app->view->renderTemplate( |
172
|
|
|
'fastcgi-cache-bust/settings', |
173
|
|
|
[ |
174
|
|
|
'settings' => $this->getSettings(), |
175
|
|
|
] |
176
|
|
|
); |
177
|
|
|
} catch (Exception $e) { |
178
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
179
|
|
|
return ''; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|