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