1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Twigpack plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Twigpack is the conduit between Twig and webpack, with manifest.json & webpack-dev-server HMR support |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com/ |
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace nystudio107\twigpack; |
12
|
|
|
|
13
|
|
|
use nystudio107\twigpack\services\Manifest as ManifestService; |
14
|
|
|
use nystudio107\twigpack\models\Settings; |
15
|
|
|
use nystudio107\twigpack\variables\ManifestVariable; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
use craft\base\Plugin; |
19
|
|
|
use craft\events\DeleteTemplateCachesEvent; |
20
|
|
|
use craft\events\PluginEvent; |
21
|
|
|
use craft\events\RegisterCacheOptionsEvent; |
22
|
|
|
use craft\events\TemplateEvent; |
23
|
|
|
use craft\services\Plugins; |
24
|
|
|
use craft\services\TemplateCaches; |
25
|
|
|
use craft\utilities\ClearCaches; |
26
|
|
|
use craft\web\twig\variables\CraftVariable; |
27
|
|
|
use craft\web\Application; |
28
|
|
|
use craft\web\View; |
29
|
|
|
|
30
|
|
|
use yii\base\Event; |
31
|
|
|
use yii\web\NotFoundHttpException; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class Twigpack |
35
|
|
|
* |
36
|
|
|
* @author nystudio107 |
37
|
|
|
* @package Twigpack |
38
|
|
|
* @since 1.0.0 |
39
|
|
|
* |
40
|
|
|
* @property ManifestService $manifest |
41
|
|
|
*/ |
42
|
|
|
class Twigpack extends Plugin |
43
|
|
|
{ |
44
|
|
|
// Static Properties |
45
|
|
|
// ========================================================================= |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Twigpack |
49
|
|
|
*/ |
50
|
|
|
public static $plugin; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
public static $templateName; |
56
|
|
|
|
57
|
|
|
// Public Properties |
58
|
|
|
// ========================================================================= |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
public $schemaVersion = '1.0.0'; |
64
|
|
|
|
65
|
|
|
// Public Methods |
66
|
|
|
// ========================================================================= |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function init() |
72
|
|
|
{ |
73
|
|
|
parent::init(); |
74
|
|
|
self::$plugin = $this; |
75
|
|
|
// Install our event listeners |
76
|
|
|
$this->installEventListeners(); |
77
|
|
|
// Log that we've loaded |
78
|
|
|
Craft::info( |
79
|
|
|
Craft::t( |
80
|
|
|
'twigpack', |
81
|
|
|
'{name} plugin loaded', |
82
|
|
|
['name' => $this->name] |
83
|
|
|
), |
84
|
|
|
__METHOD__ |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Clear all the caches! |
90
|
|
|
*/ |
91
|
|
|
public function clearAllCaches() |
92
|
|
|
{ |
93
|
|
|
// Clear all of Twigpack's caches |
94
|
|
|
self::$plugin->manifest->invalidateCaches(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Protected Methods |
98
|
|
|
// ========================================================================= |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Install our event listeners. |
102
|
|
|
*/ |
103
|
|
|
protected function installEventListeners() |
104
|
|
|
{ |
105
|
|
|
// Remember the name of the currently rendering template |
106
|
|
|
// Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE |
107
|
|
|
Event::on( |
108
|
|
|
View::class, |
109
|
|
|
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
110
|
|
|
function (TemplateEvent $event) { |
111
|
|
|
self::$templateName = $event->template; |
112
|
|
|
} |
113
|
|
|
); |
114
|
|
|
// Handler: CraftVariable::EVENT_INIT |
115
|
|
|
Event::on( |
116
|
|
|
CraftVariable::class, |
117
|
|
|
CraftVariable::EVENT_INIT, |
118
|
|
|
function (Event $event) { |
119
|
|
|
/** @var CraftVariable $variable */ |
120
|
|
|
$variable = $event->sender; |
121
|
|
|
$variable->set('twigpack', ManifestVariable::class); |
122
|
|
|
} |
123
|
|
|
); |
124
|
|
|
// Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES |
125
|
|
|
Event::on( |
126
|
|
|
TemplateCaches::class, |
127
|
|
|
TemplateCaches::EVENT_AFTER_DELETE_CACHES, |
128
|
|
|
function (DeleteTemplateCachesEvent $event) { |
|
|
|
|
129
|
|
|
// Invalidate the caches when template caches are deleted |
130
|
|
|
$this->clearAllCaches(); |
131
|
|
|
} |
132
|
|
|
); |
133
|
|
|
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
134
|
|
|
Event::on( |
135
|
|
|
Plugins::class, |
136
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
137
|
|
|
function (PluginEvent $event) { |
138
|
|
|
if ($event->plugin === $this) { |
139
|
|
|
// Invalidate our caches after we've been installed |
140
|
|
|
$this->clearAllCaches(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
); |
144
|
|
|
// Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
145
|
|
|
Event::on( |
146
|
|
|
ClearCaches::class, |
147
|
|
|
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
148
|
|
|
function (RegisterCacheOptionsEvent $event) { |
149
|
|
|
Craft::debug( |
150
|
|
|
'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS', |
151
|
|
|
__METHOD__ |
152
|
|
|
); |
153
|
|
|
// Register our caches for the Clear Cache Utility |
154
|
|
|
$event->options = array_merge( |
155
|
|
|
$event->options, |
156
|
|
|
$this->customAdminCpCacheOptions() |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
); |
160
|
|
|
// delay attaching event handler to the view component after it is fully configured |
161
|
|
|
$app = Craft::$app; |
162
|
|
|
if ($app->getConfig()->getGeneral()->devMode) { |
163
|
|
|
$app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) { |
164
|
|
|
$app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']); |
165
|
|
|
}); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Inject the error entry point JavaScript for auto-reloading of Twig error pages |
171
|
|
|
*/ |
172
|
|
|
public function injectErrorEntry() |
173
|
|
|
{ |
174
|
|
|
if (Craft::$app->getResponse()->isServerError || Craft::$app->getResponse()->isClientError) { |
175
|
|
|
$settings = self::$plugin->getSettings(); |
176
|
|
|
if (!empty($settings->errorEntry) && $settings->useDevServer) { |
177
|
|
|
try { |
178
|
|
|
$tags = self::$plugin->manifest->getJsModuleTags($settings->errorEntry, false); |
179
|
|
|
if ($tags !== null) { |
|
|
|
|
180
|
|
|
echo $tags; |
181
|
|
|
} |
182
|
|
|
} catch (NotFoundHttpException $e) { |
183
|
|
|
// That's okay, Twigpack will have already logged the error |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the custom AdminCP cache options. |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
protected function customAdminCpCacheOptions(): array |
195
|
|
|
{ |
196
|
|
|
return [ |
197
|
|
|
// Manifest cache |
198
|
|
|
[ |
199
|
|
|
'key' => 'twigpack-manfiest-cache', |
200
|
|
|
'label' => Craft::t('twigpack', 'Twigpack Manifest Cache'), |
201
|
|
|
'action' => [$this, 'clearAllCaches'], |
202
|
|
|
], |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @inheritdoc |
208
|
|
|
*/ |
209
|
|
|
protected function createSettingsModel() |
210
|
|
|
{ |
211
|
|
|
return new Settings(); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.