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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2021 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
33
|
|
|
* @package Vite |
|
|
|
|
34
|
|
|
* @since 1.0.0 |
|
|
|
|
35
|
|
|
* |
36
|
|
|
* @property ViteService $vite |
|
|
|
|
37
|
|
|
* @property HelperService $helper |
|
|
|
|
38
|
|
|
*/ |
|
|
|
|
39
|
|
|
class Vite extends Plugin |
40
|
|
|
{ |
41
|
|
|
// Static Properties |
42
|
|
|
// ========================================================================= |
43
|
|
|
|
44
|
|
|
/** |
|
|
|
|
45
|
|
|
* @var Vite |
46
|
|
|
*/ |
47
|
|
|
public static $plugin; |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public static $templateName; |
53
|
|
|
|
54
|
|
|
// Static Methods |
55
|
|
|
// ========================================================================= |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
public $schemaVersion = '1.0.0'; |
79
|
|
|
|
80
|
|
|
/** |
|
|
|
|
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
public $hasCpSettings = false; |
84
|
|
|
|
85
|
|
|
/** |
|
|
|
|
86
|
|
|
* @var bool |
87
|
|
|
*/ |
88
|
|
|
public $hasCpSection = false; |
89
|
|
|
|
90
|
|
|
// Public Methods |
91
|
|
|
// ========================================================================= |
92
|
|
|
|
93
|
|
|
/** |
|
|
|
|
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
|
|
|
|
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( |
|
|
|
|
106
|
|
|
$settingsAttrs, |
107
|
|
|
$viteAttrs |
108
|
|
|
)); |
|
|
|
|
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
|
|
|
*/ |
|
|
|
|
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
|
|
|
*/ |
|
|
|
|
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 */ |
|
|
|
|
146
|
|
|
$variable = $event->sender; |
147
|
|
|
$variable->set('vite', [ |
|
|
|
|
148
|
|
|
'class' => ViteVariable::class, |
149
|
|
|
'viteService' => $this->vite, |
150
|
|
|
]); |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
199
|
|
|
* @inheritdoc |
200
|
|
|
*/ |
|
|
|
|
201
|
|
|
protected function createSettingsModel() |
202
|
|
|
{ |
203
|
|
|
return new Settings(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|