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