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