Passed
Push — develop-v4 ( 23557d...e25dc6 )
by Andrew
03:43
created

Vite::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Vite plugin for Craft CMS 3.x
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\pluginvite\services\ViteService;
22
use nystudio107\vite\helpers\PluginConfig as PluginConfigHelper;
23
use nystudio107\vite\models\Settings;
24
use nystudio107\vite\services\Helper as HelperService;
25
use nystudio107\vite\variables\ViteVariable;
26
use yii\base\Event;
27
28
/**
29
 * Class Vite
30
 *
31
 * @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
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
32
 * @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...
33
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
34
 *
35
 * @property  ViteService $vite
0 ignored issues
show
Coding Style introduced by
Tag value for @property tag indented incorrectly; expected 1 spaces but found 2
Loading history...
36
 * @property  HelperService $helper
0 ignored issues
show
Coding Style introduced by
Tag value for @property tag indented incorrectly; expected 1 spaces but found 2
Loading history...
37
 */
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...
38
class Vite extends Plugin
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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
66
     * @var bool
67
     */
68
    public bool $hasCpSection = false;
69
70
    // Public Static Methods
71
    // =========================================================================
72
73
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
74
     * @inheritdoc
75
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
76
    public static function config(): array
77
    {
78
        return [
79
            'components' => [
80
                'helper' => HelperService::class,
81
                'vite' => PluginConfigHelper::serviceDefinitionFromConfig('vite', ViteService::class)
82
            ]
83
        ];
84
    }
85
86
    // Public Methods
87
    // =========================================================================
88
89
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
90
     * @inheritdoc
91
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
92
    public function init(): void
93
    {
94
        parent::init();
95
        self::$plugin = $this;
96
        // Add our event listeners
97
        $this->installEventListeners();
98
        // Log that the plugin has loaded
99
        Craft::info(
100
            Craft::t(
101
                'vite',
102
                '{name} plugin loaded',
103
                ['name' => $this->name]
104
            ),
105
            __METHOD__
106
        );
107
    }
108
109
    /**
110
     * Clear all the caches!
111
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
112
    public function clearAllCaches(): void
113
    {
114
        // Clear all of Vite's caches
115
        $this->vite->invalidateCaches();
116
    }
117
118
    // Protected Methods
119
    // =========================================================================
120
121
    /**
122
     * Install our event listeners.
123
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
124
    protected function installEventListeners(): void
125
    {
126
        // Register our variable
127
        Event::on(
128
            CraftVariable::class,
129
            CraftVariable::EVENT_INIT,
130
            function (Event $event) {
131
                /** @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
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
132
                $variable = $event->sender;
133
                $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...
134
                    'class' => ViteVariable::class,
135
                    'viteService' => $this->vite,
136
                ]);
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...
137
            }
138
        );
139
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
140
        Event::on(
141
            ClearCaches::class,
142
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
143
            function (RegisterCacheOptionsEvent $event) {
144
                Craft::debug(
145
                    'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
146
                    __METHOD__
147
                );
148
                // Register our caches for the Clear Cache Utility
149
                $event->options = array_merge(
150
                    $event->options,
151
                    $this->customAdminCpCacheOptions()
152
                );
153
            }
154
        );
155
        // Remember the name of the currently rendering template
156
        // Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE
157
        Event::on(
158
            View::class,
159
            View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
160
            static function (TemplateEvent $event) {
161
                self::$templateName = $event->template;
162
            }
163
        );
164
165
    }
166
167
    /**
168
     * Returns the custom Control Panel cache options.
169
     *
170
     * @return array
171
     */
172
    protected function customAdminCpCacheOptions(): array
173
    {
174
        return [
175
            // Manifest cache
176
            [
177
                'key' => 'vite-file-cache',
178
                'label' => Craft::t('vite', 'Vite Cache'),
179
                'action' => [$this, 'clearAllCaches'],
180
            ],
181
        ];
182
    }
183
184
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
185
     * @inheritdoc
186
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
187
    protected function createSettingsModel(): ?Model
188
    {
189
        return new Settings();
190
    }
191
}
192