Passed
Push — develop ( cd4090...9696c4 )
by Andrew
04:47
created

Vite::installEventListeners()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 38
rs 9.552
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 nystudio107\vite\models\Settings;
14
use nystudio107\vite\variables\ViteVariable;
15
16
use nystudio107\pluginvite\services\ViteService;
17
18
use Craft;
19
use craft\base\Plugin;
20
use craft\events\RegisterCacheOptionsEvent;
21
use craft\events\TemplateEvent;
22
use craft\utilities\ClearCaches;
23
use craft\web\twig\variables\CraftVariable;
24
use craft\web\View;
25
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
 */
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...
37
class Vite extends Plugin
38
{
39
    // Static Properties
40
    // =========================================================================
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @var Vite
44
     */
45
    public static $plugin;
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @var string
49
     */
50
    public static $templateName;
51
52
    // Static Methods
53
    // =========================================================================
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $id should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @inheritdoc
57
     */
58
    public function __construct($id, $parent = null, array $config = [])
59
    {
60
        $config['components'] = [
61
            'vite' => [
62
                'class' => ViteService::class,
63
            ]
64
        ];
65
66
        parent::__construct($id, $parent, $config);
67
    }
68
69
    // Public Properties
70
    // =========================================================================
71
72
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
73
     * @var string
74
     */
75
    public $schemaVersion = '1.0.0';
76
77
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
78
     * @var bool
79
     */
80
    public $hasCpSettings = false;
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @var bool
84
     */
85
    public $hasCpSection = false;
86
87
    // Public Methods
88
    // =========================================================================
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
91
     * @inheritdoc
92
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
93
    public function init()
94
    {
95
        parent::init();
96
        self::$plugin = $this;
97
98
        // Configure our Vite service with the settings
99
        $settings = $this->getSettings();
100
        if ($settings) {
101
            $settingsAttrs = $settings->getAttributes();
102
            $viteAttrs = $this->vite->getAttributes();
103
            Craft::configure($this->vite, array_intersect_key(
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...
104
                $settingsAttrs,
105
                $viteAttrs
106
            ));
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...
107
        }
108
       // Log that the plugin has loaded
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 7
Loading history...
109
        Craft::info(
110
            Craft::t(
111
                'vite',
112
                '{name} plugin loaded',
113
                ['name' => $this->name]
114
            ),
115
            __METHOD__
116
        );
117
    }
118
119
    /**
120
     * Clear all the caches!
121
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
122
    public function clearAllCaches()
123
    {
124
        // Clear all of Vite's caches
125
        $this->vite->invalidateCaches();
126
    }
127
128
    // Protected Methods
129
    // =========================================================================
130
131
    /**
132
     * Install our event listeners.
133
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
134
    protected function installEventListeners()
135
    {
136
        // Register our variable
137
        Event::on(
138
            CraftVariable::class,
139
            CraftVariable::EVENT_INIT,
140
            function (Event $event) {
141
                /** @var CraftVariable $variable */
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
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...
142
                $variable = $event->sender;
143
                $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...
144
                    'class' => ViteVariable::class,
145
                    'viteService' => $this->vite,
146
                ]);
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...
147
            }
148
        );
149
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
150
        Event::on(
151
            ClearCaches::class,
152
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
153
            function (RegisterCacheOptionsEvent $event) {
154
                Craft::debug(
155
                    'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
156
                    __METHOD__
157
                );
158
                // Register our caches for the Clear Cache Utility
159
                $event->options = array_merge(
160
                    $event->options,
161
                    $this->customAdminCpCacheOptions()
162
                );
163
            }
164
        );
165
        // Remember the name of the currently rendering template
166
        // Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE
167
        Event::on(
168
            View::class,
169
            View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
170
            function (TemplateEvent $event) {
171
                self::$templateName = $event->template;
172
            }
173
        );
174
175
    }
176
177
    /**
178
     * Returns the custom Control Panel cache options.
179
     *
180
     * @return array
181
     */
182
    protected function customAdminCpCacheOptions(): array
183
    {
184
        return [
185
            // Manifest cache
186
            [
187
                'key' => 'vite-file-cache',
188
                'label' => Craft::t('vite', 'Vite Cache'),
189
                'action' => [$this, 'clearAllCaches'],
190
            ],
191
        ];
192
    }
193
194
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
195
     * @inheritdoc
196
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
197
    protected function createSettingsModel()
198
    {
199
        return new Settings();
200
    }
201
}
202