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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2021 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
29
|
|
|
* @package Vite |
|
|
|
|
30
|
|
|
* @since 1.0.0 |
|
|
|
|
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
class Vite extends Plugin |
33
|
|
|
{ |
34
|
|
|
// Traits |
35
|
|
|
// ========================================================================= |
36
|
|
|
|
37
|
|
|
use ServicesTrait; |
38
|
|
|
|
39
|
|
|
// Static Properties |
40
|
|
|
// ========================================================================= |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @var Vite |
44
|
|
|
*/ |
45
|
|
|
public static $plugin; |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public static $templateName; |
51
|
|
|
|
52
|
|
|
// Public Properties |
53
|
|
|
// ========================================================================= |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public $schemaVersion = '1.0.0'; |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
public $hasCpSettings = false; |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @var bool |
67
|
|
|
*/ |
68
|
|
|
public $hasCpSection = false; |
69
|
|
|
|
70
|
|
|
// Public Methods |
71
|
|
|
// ========================================================================= |
72
|
|
|
|
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
|
|
|
|
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
|
|
|
*/ |
|
|
|
|
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
|
|
|
*/ |
|
|
|
|
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 */ |
|
|
|
|
116
|
|
|
$variable = $event->sender; |
117
|
|
|
$variable->set('vite', [ |
|
|
|
|
118
|
|
|
'class' => ViteVariable::class, |
119
|
|
|
'viteService' => $this->vite, |
120
|
|
|
]); |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
169
|
|
|
* @inheritdoc |
170
|
|
|
*/ |
|
|
|
|
171
|
|
|
protected function createSettingsModel() |
172
|
|
|
{ |
173
|
|
|
return new Settings(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|