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\events\DeleteTemplateCachesEvent; |
17
|
|
|
use craft\events\ElementEvent; |
18
|
|
|
use craft\events\RegisterCacheOptionsEvent; |
19
|
|
|
use craft\services\Elements; |
20
|
|
|
use craft\services\TemplateCaches; |
21
|
|
|
use craft\utilities\ClearCaches; |
22
|
|
|
use nystudio107\fastcgicachebust\models\Settings; |
23
|
|
|
use nystudio107\fastcgicachebust\services\ServicesTrait; |
24
|
|
|
use yii\base\Event; |
25
|
|
|
use yii\base\Exception; |
26
|
|
|
use function get_class; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class FastcgiCacheBust |
30
|
|
|
* |
31
|
|
|
* @author nystudio107 |
|
|
|
|
32
|
|
|
* @package FastcgiCacheBust |
|
|
|
|
33
|
|
|
* @since 1.0.0 |
|
|
|
|
34
|
|
|
*/ |
|
|
|
|
35
|
|
|
class FastcgiCacheBust extends Plugin |
36
|
|
|
{ |
37
|
|
|
// Traits |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
use ServicesTrait; |
41
|
|
|
|
42
|
|
|
// Static Properties |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var FastcgiCacheBust |
47
|
|
|
*/ |
48
|
|
|
public static $plugin; |
49
|
|
|
|
50
|
|
|
// Public Properties |
51
|
|
|
// ========================================================================= |
52
|
|
|
|
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
public $schemaVersion = '1.0.0'; |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
public $hasCpSection = false; |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
public $hasCpSettings = true; |
67
|
|
|
|
68
|
|
|
// Public Methods |
69
|
|
|
// ========================================================================= |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
public function init() |
75
|
|
|
{ |
76
|
|
|
parent::init(); |
77
|
|
|
self::$plugin = $this; |
78
|
|
|
|
79
|
|
|
// Handler: Elements::EVENT_AFTER_SAVE_ELEMENT |
80
|
|
|
Event::on( |
81
|
|
|
Elements::class, |
82
|
|
|
Elements::EVENT_AFTER_SAVE_ELEMENT, |
83
|
|
|
function (ElementEvent $event) { |
84
|
|
|
Craft::debug( |
85
|
|
|
'Elements::EVENT_AFTER_SAVE_ELEMENT', |
86
|
|
|
__METHOD__ |
87
|
|
|
); |
88
|
|
|
/** @var Element $element */ |
|
|
|
|
89
|
|
|
$element = $event->element; |
90
|
|
|
// Only bust the cache if it's not certain excluded element types |
91
|
|
|
if (self::$plugin->cache->shouldBustCache($element)) { |
92
|
|
|
Craft::debug( |
93
|
|
|
'Cache busted due to saving: ' . get_class($element) . ' - ' . $element->title, |
94
|
|
|
__METHOD__ |
95
|
|
|
); |
96
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
); |
100
|
|
|
// Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES |
101
|
|
|
Event::on( |
102
|
|
|
TemplateCaches::class, |
103
|
|
|
TemplateCaches::EVENT_AFTER_DELETE_CACHES, |
|
|
|
|
104
|
|
|
static function (DeleteTemplateCachesEvent $event) { |
|
|
|
|
105
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
106
|
|
|
} |
107
|
|
|
); |
108
|
|
|
// Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
109
|
|
|
Event::on( |
110
|
|
|
ClearCaches::class, |
111
|
|
|
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
112
|
|
|
static function (RegisterCacheOptionsEvent $event) { |
113
|
|
|
$event->options[] = [ |
114
|
|
|
'key' => 'fastcgi-cache-bust', |
115
|
|
|
'label' => Craft::t('fastcgi-cache-bust', 'FastCGI Cache'), |
116
|
|
|
'action' => function () { |
117
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
118
|
|
|
}, |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
Craft::info( |
124
|
|
|
Craft::t( |
125
|
|
|
'fastcgi-cache-bust', |
126
|
|
|
'{name} plugin loaded', |
127
|
|
|
['name' => $this->name] |
128
|
|
|
), |
129
|
|
|
__METHOD__ |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Protected Methods |
134
|
|
|
// ========================================================================= |
135
|
|
|
|
136
|
|
|
/** |
|
|
|
|
137
|
|
|
* @inheritdoc |
138
|
|
|
*/ |
|
|
|
|
139
|
|
|
protected function createSettingsModel() |
140
|
|
|
{ |
141
|
|
|
return new Settings(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
|
|
|
|
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
|
|
|
|
147
|
|
|
protected function settingsHtml(): string |
148
|
|
|
{ |
149
|
|
|
try { |
150
|
|
|
return Craft::$app->view->renderTemplate( |
151
|
|
|
'fastcgi-cache-bust/settings', |
152
|
|
|
[ |
153
|
|
|
'settings' => $this->getSettings(), |
154
|
|
|
] |
155
|
|
|
); |
156
|
|
|
} catch (Exception $e) { |
157
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
158
|
|
|
return ''; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|