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\Model; |
15
|
|
|
use craft\base\Plugin; |
16
|
|
|
use craft\cloud\cli\controllers\UpController; |
17
|
|
|
use craft\events\CancelableEvent; |
18
|
|
|
use craft\events\RegisterCacheOptionsEvent; |
19
|
|
|
use craft\events\TemplateEvent; |
20
|
|
|
use craft\utilities\ClearCaches; |
21
|
|
|
use craft\web\twig\variables\CraftVariable; |
22
|
|
|
use craft\web\View; |
23
|
|
|
use nystudio107\vite\models\Settings; |
24
|
|
|
use nystudio107\vite\services\ServicesTrait; |
25
|
|
|
use nystudio107\vite\variables\ViteVariable; |
26
|
|
|
use yii\base\Event; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Vite |
30
|
|
|
* |
31
|
|
|
* @author nystudio107 |
|
|
|
|
32
|
|
|
* @package Vite |
|
|
|
|
33
|
|
|
* @since 1.0.0 |
|
|
|
|
34
|
|
|
*/ |
|
|
|
|
35
|
|
|
class Vite extends Plugin |
36
|
|
|
{ |
37
|
|
|
// Traits |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
use ServicesTrait; |
41
|
|
|
|
42
|
|
|
// Static Properties |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var Vite |
47
|
|
|
*/ |
48
|
|
|
public static Plugin $plugin; |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public static string $templateName; |
54
|
|
|
|
55
|
|
|
// Public Properties |
56
|
|
|
// ========================================================================= |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
public string $schemaVersion = '1.0.0'; |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
public bool $hasCpSettings = false; |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
public bool $hasCpSection = false; |
72
|
|
|
|
73
|
|
|
// Public Methods |
74
|
|
|
// ========================================================================= |
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
|
|
|
|
79
|
|
|
public function init(): void |
80
|
|
|
{ |
81
|
|
|
parent::init(); |
82
|
|
|
self::$plugin = $this; |
83
|
|
|
// Add our event listeners |
84
|
|
|
$this->installEventListeners(); |
85
|
|
|
// Log that the plugin has loaded |
86
|
|
|
Craft::info( |
87
|
|
|
Craft::t( |
88
|
|
|
'vite', |
89
|
|
|
'{name} plugin loaded', |
90
|
|
|
['name' => $this->name] |
91
|
|
|
), |
92
|
|
|
__METHOD__ |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Clear all the caches! |
98
|
|
|
*/ |
|
|
|
|
99
|
|
|
public function clearAllCaches(): void |
100
|
|
|
{ |
101
|
|
|
// Clear all of Vite's caches |
102
|
|
|
$this->vite->invalidateCaches(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Protected Methods |
106
|
|
|
// ========================================================================= |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Install our event listeners. |
110
|
|
|
*/ |
|
|
|
|
111
|
|
|
protected function installEventListeners(): void |
112
|
|
|
{ |
113
|
|
|
// Register our variable |
114
|
|
|
Event::on( |
115
|
|
|
CraftVariable::class, |
116
|
|
|
CraftVariable::EVENT_INIT, |
117
|
|
|
function(Event $event) { |
|
|
|
|
118
|
|
|
/** @var CraftVariable $variable */ |
|
|
|
|
119
|
|
|
$variable = $event->sender; |
120
|
|
|
$variable->set('vite', [ |
|
|
|
|
121
|
|
|
'class' => ViteVariable::class, |
122
|
|
|
'viteService' => $this->vite, |
123
|
|
|
]); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
); |
126
|
|
|
// Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
127
|
|
|
Event::on( |
128
|
|
|
ClearCaches::class, |
129
|
|
|
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
130
|
|
|
function(RegisterCacheOptionsEvent $event) { |
|
|
|
|
131
|
|
|
Craft::debug( |
132
|
|
|
'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS', |
133
|
|
|
__METHOD__ |
134
|
|
|
); |
135
|
|
|
// Register our caches for the Clear Cache Utility |
136
|
|
|
$event->options = array_merge( |
137
|
|
|
$event->options, |
138
|
|
|
$this->customAdminCpCacheOptions() |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
); |
142
|
|
|
// Remember the name of the currently rendering template |
143
|
|
|
// Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE |
144
|
|
|
Event::on( |
145
|
|
|
View::class, |
146
|
|
|
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
147
|
|
|
static function(TemplateEvent $event) { |
|
|
|
|
148
|
|
|
self::$templateName = $event->template; |
149
|
|
|
} |
150
|
|
|
); |
151
|
|
|
// Clears cache after craft cloud/up is run, which Craft Cloud runs on deploy |
152
|
|
|
// Handler: UpController::EVENT_AFTER_UP |
153
|
|
|
if (class_exists(UpController::class)) { |
154
|
|
|
Event::on( |
155
|
|
|
UpController::class, |
156
|
|
|
UpController::EVENT_AFTER_UP, |
157
|
|
|
function(CancelableEvent $event) { |
|
|
|
|
158
|
|
|
$this->clearAllCaches(); |
159
|
|
|
} |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns the custom Control Panel cache options. |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
protected function customAdminCpCacheOptions(): array |
170
|
|
|
{ |
171
|
|
|
return [ |
172
|
|
|
// Manifest cache |
173
|
|
|
[ |
174
|
|
|
'key' => 'vite-file-cache', |
175
|
|
|
'label' => Craft::t('vite', 'Vite Cache'), |
176
|
|
|
'action' => [$this, 'clearAllCaches'], |
177
|
|
|
], |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
|
|
|
|
182
|
|
|
* @inheritdoc |
183
|
|
|
*/ |
|
|
|
|
184
|
|
|
protected function createSettingsModel(): ?Model |
185
|
|
|
{ |
186
|
|
|
return new Settings(); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|