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