├── CHANGELOG.md ├── phpstan.neon ├── src ├── assetbundles │ └── fastcgicachebust │ │ ├── dist │ │ ├── css │ │ │ └── FastcgiCacheBust.css │ │ └── js │ │ │ └── FastcgiCacheBust.js │ │ └── FastcgiCacheBustAsset.php ├── templates │ ├── _includes │ │ └── macros.twig │ └── settings.twig ├── translations │ └── en │ │ └── fastcgi-cache-bust.php ├── config.php ├── models │ └── Settings.php ├── services │ ├── ServicesTrait.php │ └── Cache.php ├── icon.svg └── FastcgiCacheBust.php ├── ecs.php ├── LICENSE.md ├── composer.json └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # FastCGI Cache Bust Changelog 2 | 3 | ## 5.0.0 - 2024.04.26 4 | ### Added 5 | * Stable release for Craft CMS 5 6 | -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon 3 | 4 | parameters: 5 | level: 5 6 | paths: 7 | - src 8 | -------------------------------------------------------------------------------- /src/assetbundles/fastcgicachebust/dist/css/FastcgiCacheBust.css: -------------------------------------------------------------------------------- 1 | /** 2 | * FastCGI Cache Bust plugin for Craft CMS 3 | * 4 | * FastCGI Cache Bust CSS 5 | * 6 | * @author nystudio107 7 | * @copyright Copyright (c) nystudio107 8 | * @link https://nystudio107.com 9 | * @package FastcgiCacheBust 10 | * @since 1.0.0 11 | */ 12 | -------------------------------------------------------------------------------- /src/assetbundles/fastcgicachebust/dist/js/FastcgiCacheBust.js: -------------------------------------------------------------------------------- 1 | /** 2 | * FastCGI Cache Bust plugin for Craft CMS 3 | * 4 | * FastCGI Cache Bust JS 5 | * 6 | * @author nystudio107 7 | * @copyright Copyright (c) nystudio107 8 | * @link https://nystudio107.com 9 | * @package FastcgiCacheBust 10 | * @since 1.0.0 11 | */ 12 | -------------------------------------------------------------------------------- /ecs.php: -------------------------------------------------------------------------------- 1 | paths([ 8 | __DIR__ . '/src', 9 | __FILE__, 10 | ]); 11 | $ecsConfig->parallel(); 12 | $ecsConfig->sets([SetList::CRAFT_CMS_4]); 13 | }; 14 | -------------------------------------------------------------------------------- /src/templates/_includes/macros.twig: -------------------------------------------------------------------------------- 1 | {% macro configWarning(setting, file) -%} 2 | {%- set configArray = craft.app.config.getConfigFromFile(file) -%} 3 | {%- if configArray[setting] is defined -%} 4 | {{- "This is being overridden by the `#{setting}` setting in the `config/#{file}.php` file." |raw }} 5 | {%- else -%} 6 | {{ false }} 7 | {%- endif -%} 8 | {%- endmacro %} 9 | -------------------------------------------------------------------------------- /src/translations/en/fastcgi-cache-bust.php: -------------------------------------------------------------------------------- 1 | 'FastCGI Cache', 18 | '{name} plugin loaded' => '{name} plugin loaded', 19 | ]; 20 | -------------------------------------------------------------------------------- /src/templates/settings.twig: -------------------------------------------------------------------------------- 1 | {# 2 | /** 3 | * FastCGI Cache Bust plugin for Craft CMS 4 | * 5 | * FastCGI Cache Bust Settings.twig 6 | * 7 | * @author nystudio107 8 | * @copyright Copyright (c) nystudio107 9 | * @link https://nystudio107.com 10 | * @package FastcgiCacheBust 11 | * @since 1.0.0 12 | */ 13 | #} 14 | 15 | {% import "_includes/forms" as forms %} 16 | 17 | {% from "fastcgi-cache-bust/_includes/macros.twig" import configWarning %} 18 | 19 | {% do view.registerAssetBundle("nystudio107\\fastcgicachebust\\assetbundles\\fastcgicachebust\\FastcgiCacheBustAsset") %} 20 | 21 | {{ forms.textField({ 22 | label: 'FastCGI Cache Path', 23 | instructions: 'Enter the full absolute path to the FastCGI Cache directory. If you require more than one FastCGI Cache directory cleared, separate the paths with a comma (,).', 24 | id: 'cachePath', 25 | name: 'cachePath', 26 | value: settings['cachePath'], 27 | warning: configWarning("cachePath", "fastcgi-cache-bust") 28 | }) }} 29 | -------------------------------------------------------------------------------- /src/config.php: -------------------------------------------------------------------------------- 1 | '', 30 | 31 | ]; 32 | -------------------------------------------------------------------------------- /src/models/Settings.php: -------------------------------------------------------------------------------- 1 | ''], 41 | ]; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) nystudio107 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /src/assetbundles/fastcgicachebust/FastcgiCacheBustAsset.php: -------------------------------------------------------------------------------- 1 | sourcePath = '@nystudio107/fastcgicachebust/assetbundles/fastcgicachebust/dist'; 32 | 33 | $this->depends = [ 34 | CpAsset::class, 35 | ]; 36 | 37 | $this->js = [ 38 | 'js/FastcgiCacheBust.js', 39 | ]; 40 | 41 | $this->css = [ 42 | 'css/FastcgiCacheBust.css', 43 | ]; 44 | 45 | parent::init(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/services/ServicesTrait.php: -------------------------------------------------------------------------------- 1 | [ 35 | 'cache' => CacheService::class, 36 | ], 37 | ]; 38 | } 39 | 40 | // Public Methods 41 | // ========================================================================= 42 | 43 | /** 44 | * Returns the cache service 45 | * 46 | * @return CacheService The cache service 47 | * @throws InvalidConfigException 48 | */ 49 | public function getCache(): CacheService 50 | { 51 | return $this->get('cache'); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nystudio107/craft-fastcgicachebust", 3 | "description": "Bust the Nginx FastCGI Cache when entries are saved or created.", 4 | "type": "craft-plugin", 5 | "version": "5.0.0", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "fastcgi cache bust" 12 | ], 13 | "support": { 14 | "docs": "https://nystudio107.com/docs/fastcgi-cache-bust/", 15 | "issues": "https://nystudio107.com/plugins/fastcgi-cache-bust/support", 16 | "source": "https://github.com/nystudio107/craft-fastcgicachebust" 17 | }, 18 | "license": "MIT", 19 | "authors": [ 20 | { 21 | "name": "nystudio107", 22 | "homepage": "https://nystudio107.com" 23 | } 24 | ], 25 | "require": { 26 | "php": "^8.2", 27 | "craftcms/cms": "^5.0.0" 28 | }, 29 | "require-dev": { 30 | "craftcms/ecs": "dev-main", 31 | "craftcms/phpstan": "dev-main", 32 | "craftcms/rector": "dev-main" 33 | }, 34 | "scripts": { 35 | "phpstan": "phpstan --ansi --memory-limit=1G", 36 | "check-cs": "ecs check --ansi", 37 | "fix-cs": "ecs check --fix --ansi" 38 | }, 39 | "config": { 40 | "allow-plugins": { 41 | "craftcms/plugin-installer": true, 42 | "yiisoft/yii2-composer": true 43 | }, 44 | "optimize-autoloader": true, 45 | "sort-packages": true 46 | }, 47 | "autoload": { 48 | "psr-4": { 49 | "nystudio107\\fastcgicachebust\\": "src/" 50 | } 51 | }, 52 | "extra": { 53 | "class": "nystudio107\\fastcgicachebust\\FastcgiCacheBust", 54 | "handle": "fastcgi-cache-bust", 55 | "name": "FastCGI Cache Bust" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-fastcgicachebust/badges/quality-score.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-fastcgicachebust/?branch=v5) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-fastcgicachebust/badges/coverage.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-fastcgicachebust/?branch=v5) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-fastcgicachebust/badges/build.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-fastcgicachebust/build-status/v5) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-fastcgicachebust/badges/code-intelligence.svg?b=v5)](https://scrutinizer-ci.com/code-intelligence) 2 | 3 | # FastCGI Cache Bust plugin for Craft CMS 5.x 4 | 5 | Bust the Nginx FastCGI Cache when entries are saved or created. 6 | 7 | ![Screenshot](./docs/docs/resources/img/plugin-logo.png) 8 | 9 | ## Requirements 10 | 11 | This plugin requires Craft CMS 5.0.0 or later. 12 | 13 | ## Installation 14 | 15 | To install FastCGI Cache Bust, follow these steps: 16 | 17 | 1. Install with Composer via `composer require nystudio107/craft-fastcgicachebust` from your project directory 18 | 2. Install the plugin via `./craft install/plugin fastcgi-cache-bust` via the CLI, or in the Control Panel, go to Settings → Plugins and click the “Install” button for FastCGI Cache Bust. 19 | 20 | You can also install FastCGI Cache Bust via the **Plugin Store** in the Craft Control Panel. 21 | 22 | ## Documentation 23 | 24 | Click here -> [FastCGI Cache Bust Documentation](https://nystudio107.com/plugins/fastcgi-cache-bust/documentation) 25 | 26 | Brought to you by [nystudio107](https://nystudio107.com) 27 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 12 | 13 | 18 | 23 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/services/Cache.php: -------------------------------------------------------------------------------- 1 | getSettings(); 38 | if ($settings !== null && !empty($settings->cachePath)) { 39 | $cacheDirs = explode(',', $settings->cachePath); 40 | foreach ($cacheDirs as $cacheDir) { 41 | $cacheDir = Craft::parseEnv($cacheDir); 42 | $cacheDir = trim($cacheDir); 43 | try { 44 | FileHelper::clearDirectory($cacheDir); 45 | } catch (ErrorException $errorException) { 46 | Craft::error($errorException->getMessage(), __METHOD__); 47 | } 48 | 49 | Craft::info( 50 | Craft::t( 51 | 'fastcgi-cache-bust', 52 | 'FastCGI Cache busted: `' . $cacheDir 53 | ), 54 | __METHOD__ 55 | ); 56 | } 57 | } 58 | } 59 | 60 | /** 61 | * Determine whether the cache should be busted or not based on the $element 62 | * 63 | * @param Element $element 64 | * 65 | * @return bool 66 | */ 67 | public function shouldBustCache(Element $element): bool 68 | { 69 | // Don't bust the cache if the element isn't ENABLED or LIVE 70 | if (($element->getStatus() !== Element::STATUS_ENABLED) && ($element->getStatus() !== Entry::STATUS_LIVE)) { 71 | return false; 72 | } 73 | // Don't bust the cache if the element is a draft or revision 74 | if (ElementHelper::isDraftOrRevision($element)) { 75 | return false; 76 | } 77 | 78 | return true; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/FastcgiCacheBust.php: -------------------------------------------------------------------------------- 1 | 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 { 102 | $event->options[] = [ 103 | 'key' => 'fastcgi-cache-bust', 104 | 'label' => Craft::t('fastcgi-cache-bust', 'FastCGI Cache'), 105 | 'action' => function(): void { 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 | /** 126 | * @inheritdoc 127 | */ 128 | protected function createSettingsModel(): Settings 129 | { 130 | return new Settings(); 131 | } 132 | 133 | /** 134 | * @inheritdoc 135 | */ 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 | --------------------------------------------------------------------------------