Passed
Push — v1 ( 02ea9f...4232cd )
by Andrew
14:00 queued 08: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\Plugin;
15
use craft\events\RegisterCacheOptionsEvent;
16
use craft\events\TemplateEvent;
17
use craft\utilities\ClearCaches;
18
use craft\web\twig\variables\CraftVariable;
19
use craft\web\View;
20
use nystudio107\vite\models\Settings;
21
use nystudio107\vite\services\ServicesTrait;
22
use nystudio107\vite\variables\ViteVariable;
23
use yii\base\Event;
24
25
/**
26
 * Class Vite
27
 *
28
 * @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...
29
 * @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...
30
 * @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...
31
 */
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...
32
class Vite extends Plugin
33
{
34
    // Traits
35
    // =========================================================================
36
37
    use ServicesTrait;
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
    // Public Properties
53
    // =========================================================================
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @var string
57
     */
58
    public $schemaVersion = '1.0.0';
59
60
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
61
     * @var bool
62
     */
63
    public $hasCpSettings = false;
64
65
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
66
     * @var bool
67
     */
68
    public $hasCpSection = false;
69
70
    // Public 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 function init()
77
    {
78
        parent::init();
79
        self::$plugin = $this;
80
        // Add our event listeners
81
        $this->installEventListeners();
82
        // Log that the plugin has loaded
83
        Craft::info(
84
            Craft::t(
85
                'vite',
86
                '{name} plugin loaded',
87
                ['name' => $this->name]
88
            ),
89
            __METHOD__
90
        );
91
    }
92
93
    /**
94
     * Clear all the caches!
95
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
96
    public function clearAllCaches()
97
    {
98
        // Clear all of Vite's caches
99
        $this->vite->invalidateCaches();
100
    }
101
102
    // Protected Methods
103
    // =========================================================================
104
105
    /**
106
     * Install our event listeners.
107
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
108
    protected function installEventListeners()
109
    {
110
        // Register our variable
111
        Event::on(
112
            CraftVariable::class,
113
            CraftVariable::EVENT_INIT,
114
            function (Event $event) {
115
                /** @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...
116
                $variable = $event->sender;
117
                $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...
118
                    'class' => ViteVariable::class,
119
                    'viteService' => $this->vite,
120
                ]);
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...
121
            }
122
        );
123
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
124
        Event::on(
125
            ClearCaches::class,
126
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
127
            function (RegisterCacheOptionsEvent $event) {
128
                Craft::debug(
129
                    'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
130
                    __METHOD__
131
                );
132
                // Register our caches for the Clear Cache Utility
133
                $event->options = array_merge(
134
                    $event->options,
135
                    $this->customAdminCpCacheOptions()
136
                );
137
            }
138
        );
139
        // Remember the name of the currently rendering template
140
        // Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE
141
        Event::on(
142
            View::class,
143
            View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
144
            function (TemplateEvent $event) {
145
                self::$templateName = $event->template;
146
            }
147
        );
148
149
    }
150
151
    /**
152
     * Returns the custom Control Panel cache options.
153
     *
154
     * @return array
155
     */
156
    protected function customAdminCpCacheOptions(): array
157
    {
158
        return [
159
            // Manifest cache
160
            [
161
                'key' => 'vite-file-cache',
162
                'label' => Craft::t('vite', 'Vite Cache'),
163
                'action' => [$this, 'clearAllCaches'],
164
            ],
165
        ];
166
    }
167
168
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
169
     * @inheritdoc
170
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
171
    protected function createSettingsModel()
172
    {
173
        return new Settings();
174
    }
175
}
176