Passed
Push — v4 ( afc75e...d63094 )
by Andrew
22:07 queued 16:19
created

Vite::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 15
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
/**
3
 * Vite plugin for Craft CMS
4
 *
5
 * Allows the use of the Vite.js next generation frontend tooling with Craft CMS
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2021 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\vite;
12
13
use Craft;
14
use craft\base\Model;
15
use craft\base\Plugin;
16
use craft\events\RegisterCacheOptionsEvent;
17
use craft\events\TemplateEvent;
18
use craft\utilities\ClearCaches;
19
use craft\web\twig\variables\CraftVariable;
20
use craft\web\View;
21
use nystudio107\vite\models\Settings;
22
use nystudio107\vite\services\ServicesTrait;
23
use nystudio107\vite\variables\ViteVariable;
24
use yii\base\Event;
25
26
/**
27
 * Class Vite
28
 *
29
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
30
 * @package   Vite
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
31
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
32
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
33
class Vite extends Plugin
34
{
35
    // Traits
36
    // =========================================================================
37
38
    use ServicesTrait;
39
40
    // Static Properties
41
    // =========================================================================
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @var Vite
45
     */
46
    public static Plugin $plugin;
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @var string
50
     */
51
    public static string $templateName;
52
53
    // Public Properties
54
    // =========================================================================
55
56
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @var string
58
     */
59
    public string $schemaVersion = '1.0.0';
60
61
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
62
     * @var bool
63
     */
64
    public bool $hasCpSettings = false;
65
66
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
67
     * @var bool
68
     */
69
    public bool $hasCpSection = false;
70
71
    // Public Methods
72
    // =========================================================================
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @inheritdoc
76
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
77
    public function init(): void
78
    {
79
        parent::init();
80
        self::$plugin = $this;
81
        // Add our event listeners
82
        $this->installEventListeners();
83
        // Log that the plugin has loaded
84
        Craft::info(
85
            Craft::t(
86
                'vite',
87
                '{name} plugin loaded',
88
                ['name' => $this->name]
89
            ),
90
            __METHOD__
91
        );
92
    }
93
94
    /**
95
     * Clear all the caches!
96
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
97
    public function clearAllCaches(): void
98
    {
99
        // Clear all of Vite's caches
100
        $this->vite->invalidateCaches();
101
    }
102
103
    // Protected Methods
104
    // =========================================================================
105
106
    /**
107
     * Install our event listeners.
108
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
109
    protected function installEventListeners(): void
110
    {
111
        // Register our variable
112
        Event::on(
113
            CraftVariable::class,
114
            CraftVariable::EVENT_INIT,
115
            function (Event $event) {
116
                /** @var CraftVariable $variable */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
117
                $variable = $event->sender;
118
                $variable->set('vite', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
119
                    'class' => ViteVariable::class,
120
                    'viteService' => $this->vite,
121
                ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
122
            }
123
        );
124
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
125
        Event::on(
126
            ClearCaches::class,
127
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
128
            function (RegisterCacheOptionsEvent $event) {
129
                Craft::debug(
130
                    'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
131
                    __METHOD__
132
                );
133
                // Register our caches for the Clear Cache Utility
134
                $event->options = array_merge(
135
                    $event->options,
136
                    $this->customAdminCpCacheOptions()
137
                );
138
            }
139
        );
140
        // Remember the name of the currently rendering template
141
        // Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE
142
        Event::on(
143
            View::class,
144
            View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
145
            static function (TemplateEvent $event) {
146
                self::$templateName = $event->template;
147
            }
148
        );
149
150
    }
151
152
    /**
153
     * Returns the custom Control Panel cache options.
154
     *
155
     * @return array
156
     */
157
    protected function customAdminCpCacheOptions(): array
158
    {
159
        return [
160
            // Manifest cache
161
            [
162
                'key' => 'vite-file-cache',
163
                'label' => Craft::t('vite', 'Vite Cache'),
164
                'action' => [$this, 'clearAllCaches'],
165
            ],
166
        ];
167
    }
168
169
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
170
     * @inheritdoc
171
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
172
    protected function createSettingsModel(): ?Model
173
    {
174
        return new Settings();
175
    }
176
}
177