Issues (134)

src/FastcgiCacheBust.php (32 issues)

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
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
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
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
29
 * @package   FastcgiCacheBust
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
30
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
31
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
32
class FastcgiCacheBust extends Plugin
33
{
34
    // Traits
35
    // =========================================================================
36
37
    use ServicesTrait;
38
39
    // Static Properties
40
    // =========================================================================
41
42
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
43
     * @var ?FastcgiCacheBust
44
     */
45
    public static ?FastcgiCacheBust $plugin;
46
47
    // Public Properties
48
    // =========================================================================
49
50
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
51
     * @var string
52
     */
53
    public string $schemaVersion = '1.0.0';
54
55
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
56
     * @var bool
57
     */
58
    public bool $hasCpSection = false;
59
60
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
61
     * @var bool
62
     */
63
    public bool $hasCpSettings = true;
64
65
    // Public Methods
66
    // =========================================================================
67
68
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
69
     * @inheritdoc
70
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
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
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
81
                Craft::debug(
82
                    'Elements::EVENT_AFTER_SAVE_ELEMENT',
83
                    __METHOD__
84
                );
85
                /** @var Element $element */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
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
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
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
Missing short description in doc comment
Loading history...
126
     * @inheritdoc
127
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
128
    protected function createSettingsModel(): Settings
129
    {
130
        return new Settings();
131
    }
132
133
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
134
     * @inheritdoc
135
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
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