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\Cache as CacheService; |
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
|
|
|
* @property CacheService cache |
|
|
|
|
37
|
|
|
*/ |
|
|
|
|
38
|
|
|
class FastcgiCacheBust extends Plugin |
39
|
|
|
{ |
40
|
|
|
// Static Properties |
41
|
|
|
// ========================================================================= |
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* @var FastcgiCacheBust |
45
|
|
|
*/ |
46
|
|
|
public static $plugin; |
47
|
|
|
|
48
|
|
|
// Public Properties |
49
|
|
|
// ========================================================================= |
50
|
|
|
|
51
|
|
|
/** |
|
|
|
|
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $schemaVersion = '1.0.0'; |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
public $hasCpSection = false; |
60
|
|
|
|
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
public $hasCpSettings = true; |
65
|
|
|
|
66
|
|
|
// Public Methods |
67
|
|
|
// ========================================================================= |
68
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
|
public function __construct($id, $parent = null, array $config = []) |
73
|
|
|
{ |
74
|
|
|
$config['components'] = [ |
75
|
|
|
'cache' => CacheService::class, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
parent::__construct($id, $parent, $config); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
|
|
|
|
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public function init() |
85
|
|
|
{ |
86
|
|
|
parent::init(); |
87
|
|
|
self::$plugin = $this; |
88
|
|
|
|
89
|
|
|
// Handler: Elements::EVENT_AFTER_SAVE_ELEMENT |
90
|
|
|
Event::on( |
91
|
|
|
Elements::class, |
92
|
|
|
Elements::EVENT_AFTER_SAVE_ELEMENT, |
93
|
|
|
function (ElementEvent $event) { |
94
|
|
|
Craft::debug( |
95
|
|
|
'Elements::EVENT_AFTER_SAVE_ELEMENT', |
96
|
|
|
__METHOD__ |
97
|
|
|
); |
98
|
|
|
/** @var Element $element */ |
|
|
|
|
99
|
|
|
$element = $event->element; |
100
|
|
|
// Only bust the cache if it's not certain excluded element types |
101
|
|
|
if ($this->shouldBustCache($element)) { |
102
|
|
|
Craft::debug( |
103
|
|
|
'Cache busted due to saving: ' . get_class($element) . ' - ' . $element->title, |
104
|
|
|
__METHOD__ |
105
|
|
|
); |
106
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
); |
110
|
|
|
// Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES |
111
|
|
|
Event::on( |
112
|
|
|
TemplateCaches::class, |
113
|
|
|
TemplateCaches::EVENT_AFTER_DELETE_CACHES, |
|
|
|
|
114
|
|
|
static function (DeleteTemplateCachesEvent $event) { |
|
|
|
|
115
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
116
|
|
|
} |
117
|
|
|
); |
118
|
|
|
// Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
119
|
|
|
Event::on( |
120
|
|
|
ClearCaches::class, |
121
|
|
|
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
122
|
|
|
static function (RegisterCacheOptionsEvent $event) { |
123
|
|
|
$event->options[] = [ |
124
|
|
|
'key' => 'fastcgi-cache-bust', |
125
|
|
|
'label' => Craft::t('fastcgi-cache-bust', 'FastCGI Cache'), |
126
|
|
|
'action' => function () { |
127
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
128
|
|
|
}, |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
Craft::info( |
134
|
|
|
Craft::t( |
135
|
|
|
'fastcgi-cache-bust', |
136
|
|
|
'{name} plugin loaded', |
137
|
|
|
['name' => $this->name] |
138
|
|
|
), |
139
|
|
|
__METHOD__ |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Protected Methods |
144
|
|
|
// ========================================================================= |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Determine whether the cache should be busted or not based on the $element |
148
|
|
|
* |
149
|
|
|
* @param Element $element |
|
|
|
|
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
protected function shouldBustCache(Element $element): bool |
154
|
|
|
{ |
155
|
|
|
$bustCache = true; |
156
|
|
|
// Only bust the cache if the element is ENABLED or LIVE |
157
|
|
|
if (($element->getStatus() !== Element::STATUS_ENABLED) |
158
|
|
|
&& ($element->getStatus() !== Entry::STATUS_LIVE) |
159
|
|
|
) { |
160
|
|
|
$bustCache = false; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $bustCache; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
|
|
|
|
167
|
|
|
* @inheritdoc |
168
|
|
|
*/ |
|
|
|
|
169
|
|
|
protected function createSettingsModel() |
170
|
|
|
{ |
171
|
|
|
return new Settings(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
|
|
|
|
175
|
|
|
* @inheritdoc |
176
|
|
|
*/ |
|
|
|
|
177
|
|
|
protected function settingsHtml(): string |
178
|
|
|
{ |
179
|
|
|
try { |
180
|
|
|
return Craft::$app->view->renderTemplate( |
181
|
|
|
'fastcgi-cache-bust/settings', |
182
|
|
|
[ |
183
|
|
|
'settings' => $this->getSettings(), |
184
|
|
|
] |
185
|
|
|
); |
186
|
|
|
} catch (Exception $e) { |
187
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
188
|
|
|
return ''; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|