Total Complexity | 49 |
Total Lines | 542 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like ImageOptimize often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImageOptimize, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class ImageOptimize extends Plugin |
||
69 | { |
||
70 | // Traits |
||
71 | // ========================================================================= |
||
72 | |||
73 | use ServicesTrait; |
||
74 | |||
75 | // Static Properties |
||
76 | // ========================================================================= |
||
77 | |||
78 | /** |
||
79 | * @var ?ImageOptimize |
||
80 | */ |
||
81 | public static ?ImageOptimize $plugin = null; |
||
82 | |||
83 | /** |
||
84 | * @var bool |
||
85 | */ |
||
86 | public static bool $generatePlaceholders = true; |
||
87 | |||
88 | // Public Properties |
||
89 | // ========================================================================= |
||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | public string $schemaVersion = '1.0.0'; |
||
94 | |||
95 | /** |
||
96 | * @var bool |
||
97 | */ |
||
98 | public bool $hasCpSection = false; |
||
99 | |||
100 | /** |
||
101 | * @var bool |
||
102 | */ |
||
103 | public bool $hasCpSettings = true; |
||
104 | |||
105 | // Public Methods |
||
106 | // ========================================================================= |
||
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | public function init(): void |
||
112 | { |
||
113 | parent::init(); |
||
114 | self::$plugin = $this; |
||
115 | // Handle any console commands |
||
116 | $request = Craft::$app->getRequest(); |
||
117 | if ($request->getIsConsoleRequest()) { |
||
118 | $this->controllerNamespace = 'nystudio107\imageoptimize\console\controllers'; |
||
119 | } |
||
120 | // Set the image transform component |
||
121 | $this->setImageTransformComponent(); |
||
122 | // Add in our Craft components |
||
123 | $this->addComponents(); |
||
124 | // Install our global event handlers |
||
125 | $this->installEventHandlers(); |
||
126 | // Log that the plugin has loaded |
||
127 | Craft::info( |
||
128 | Craft::t( |
||
129 | 'image-optimize', |
||
130 | '{name} plugin loaded', |
||
131 | ['name' => $this->name] |
||
132 | ), |
||
133 | __METHOD__ |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @inheritdoc |
||
139 | */ |
||
140 | public function getSettingsResponse(): TemplateResponseBehavior|Response |
||
153 | ]); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @inheritdoc |
||
158 | */ |
||
159 | public function settingsHtml(): ?string |
||
160 | { |
||
161 | // Get only the user-editable settings |
||
162 | $settings = $this->getSettings(); |
||
163 | |||
164 | // Get the image transform types |
||
165 | $allImageTransformTypes = self::$plugin->optimize->getAllImageTransformTypes(); |
||
166 | $imageTransformTypeOptions = []; |
||
167 | /** @var ImageTransformInterface $class */ |
||
168 | foreach ($allImageTransformTypes as $class) { |
||
169 | if ($class::isSelectable()) { |
||
170 | $imageTransformTypeOptions[] = [ |
||
171 | 'value' => $class, |
||
172 | 'label' => $class::displayName(), |
||
173 | ]; |
||
174 | } |
||
175 | } |
||
176 | // Sort them by name |
||
177 | ArrayHelper::multisort($imageTransformTypeOptions, 'label'); |
||
178 | |||
179 | // Render the settings template |
||
180 | try { |
||
181 | return Craft::$app->getView()->renderTemplate( |
||
182 | 'image-optimize/settings/_settings.twig', |
||
183 | [ |
||
184 | 'settings' => $settings, |
||
185 | 'gdInstalled' => function_exists('imagecreatefromjpeg'), |
||
186 | 'imageTransformTypeOptions' => $imageTransformTypeOptions, |
||
187 | 'allImageTransformTypes' => $allImageTransformTypes, |
||
188 | 'imageTransform' => self::$plugin->transformMethod, |
||
189 | ] |
||
190 | ); |
||
191 | } catch (Exception $e) { |
||
192 | Craft::error($e->getMessage(), __METHOD__); |
||
193 | } |
||
194 | |||
195 | return ''; |
||
196 | } |
||
197 | |||
198 | // Protected Methods |
||
199 | // ========================================================================= |
||
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | protected function createSettingsModel(): ?Model |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Set the transformMethod component |
||
211 | */ |
||
212 | protected function setImageTransformComponent(): void |
||
213 | { |
||
214 | /* @var Settings $settings */ |
||
215 | $settings = $this->getSettings(); |
||
216 | if ($settings === null) { |
||
217 | return; |
||
218 | } |
||
219 | $definition = array_merge( |
||
220 | $settings->imageTransformTypeSettings[$settings->transformClass] ?? [], |
||
221 | ['class' => $settings->transformClass] |
||
222 | ); |
||
223 | try { |
||
224 | $this->set('transformMethod', $definition); |
||
225 | } catch (InvalidConfigException $e) { |
||
226 | Craft::error($e->getMessage(), __METHOD__); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Add in our Craft components |
||
232 | */ |
||
233 | protected function addComponents(): void |
||
234 | { |
||
235 | // Register our variables |
||
236 | Event::on( |
||
237 | CraftVariable::class, |
||
238 | CraftVariable::EVENT_INIT, |
||
239 | function (Event $event) { |
||
240 | /** @var CraftVariable $variable */ |
||
241 | $variable = $event->sender; |
||
242 | $variable->set('imageOptimize', [ |
||
243 | 'class' => ImageOptimizeVariable::class, |
||
244 | 'viteService' => $this->vite, |
||
245 | ]); |
||
246 | } |
||
247 | ); |
||
248 | |||
249 | // Register our Field |
||
250 | Event::on( |
||
251 | Fields::class, |
||
252 | Fields::EVENT_REGISTER_FIELD_TYPES, |
||
253 | static function (RegisterComponentTypesEvent $event) { |
||
254 | Craft::debug( |
||
255 | 'Fields::EVENT_REGISTER_FIELD_TYPES', |
||
256 | __METHOD__ |
||
257 | ); |
||
258 | $event->types[] = OptimizedImages::class; |
||
259 | } |
||
260 | ); |
||
261 | |||
262 | // Register our Utility only if they are using the CraftImageTransform method |
||
263 | if (self::$plugin->transformMethod instanceof CraftImageTransform) { |
||
264 | Event::on( |
||
265 | Utilities::class, |
||
266 | Utilities::EVENT_REGISTER_UTILITY_TYPES, |
||
267 | static function (RegisterComponentTypesEvent $event) { |
||
268 | $event->types[] = ImageOptimizeUtility::class; |
||
269 | } |
||
270 | ); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Install our event handlers |
||
276 | */ |
||
277 | protected function installEventHandlers(): void |
||
278 | { |
||
279 | $this->installAssetEventHandlers(); |
||
280 | $this->installElementEventHandlers(); |
||
281 | $this->installMiscEventHandlers(); |
||
282 | $request = Craft::$app->getRequest(); |
||
283 | // Install only for non-console site requests |
||
284 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
285 | $this->installSiteEventListeners(); |
||
286 | } |
||
287 | // Install only for non-console cp requests |
||
288 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
289 | $this->installCpEventListeners(); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Install our Asset event handlers |
||
295 | */ |
||
296 | protected function installAssetEventHandlers(): void |
||
297 | { |
||
298 | // Handler: Assets::EVENT_GET_ASSET_URL |
||
299 | Event::on( |
||
300 | Asset::class, |
||
301 | Asset::EVENT_DEFINE_URL, |
||
302 | static function (DefineAssetUrlEvent $event): void { |
||
303 | Craft::debug( |
||
304 | 'Asset::EVENT_GET_ASSET_URL', |
||
305 | __METHOD__ |
||
306 | ); |
||
307 | // Return the URL to the asset URL or null to let Craft handle it |
||
308 | $event->url = ImageOptimize::$plugin->optimize->handleGetAssetUrlEvent( |
||
309 | $event |
||
310 | ); |
||
311 | } |
||
312 | ); |
||
313 | |||
314 | // Handler: Assets::EVENT_GET_ASSET_THUMB_URL |
||
315 | Event::on( |
||
316 | Assets::class, |
||
317 | Assets::EVENT_DEFINE_THUMB_URL, |
||
318 | static function (DefineAssetThumbUrlEvent $event): void { |
||
319 | Craft::debug( |
||
320 | 'Assets::EVENT_DEFINE_THUMB_URL', |
||
321 | __METHOD__ |
||
322 | ); |
||
323 | // Return the URL to the asset URL or null to let Craft handle it |
||
324 | $event->url = ImageOptimize::$plugin->optimize->handleGetAssetThumbUrlEvent( |
||
325 | $event |
||
326 | ); |
||
327 | } |
||
328 | ); |
||
329 | |||
330 | // Handler: ImageTransformer::EVENT_TRANSFORM_IMAGE |
||
331 | Event::on( |
||
332 | ImageTransformer::class, |
||
333 | ImageTransformer::EVENT_TRANSFORM_IMAGE, |
||
334 | static function (ImageTransformerOperationEvent $event): void { |
||
335 | Craft::debug( |
||
336 | 'ImageTransformer::EVENT_TRANSFORM_IMAGE', |
||
337 | __METHOD__ |
||
338 | ); |
||
339 | // Return the path to the optimized image to _createTransformForAsset() |
||
340 | $event->path = ImageOptimize::$plugin->optimize->handleGenerateTransformEvent( |
||
341 | $event |
||
342 | ); |
||
343 | } |
||
344 | ); |
||
345 | |||
346 | // Handler: ImageTransformer::EVENT_DELETE_TRANSFORMED_IMAGE |
||
347 | Event::on( |
||
348 | ImageTransformer::class, |
||
349 | ImageTransformer::EVENT_DELETE_TRANSFORMED_IMAGE, |
||
350 | static function (ImageTransformerOperationEvent $event): void { |
||
351 | Craft::debug( |
||
352 | 'ImageTransformer::EVENT_DELETE_TRANSFORMED_IMAGE', |
||
353 | __METHOD__ |
||
354 | ); |
||
355 | // Clean up any stray variant files |
||
356 | ImageOptimize::$plugin->optimize->handleAfterDeleteTransformsEvent( |
||
357 | $event |
||
358 | ); |
||
359 | } |
||
360 | ); |
||
361 | |||
362 | // Handler: Assets::EVENT_BEFORE_REPLACE_ASSET |
||
363 | Event::on( |
||
364 | Assets::class, |
||
365 | Assets::EVENT_BEFORE_REPLACE_ASSET, |
||
366 | static function (ReplaceAssetEvent $event) { |
||
367 | Craft::debug( |
||
368 | 'Assets::EVENT_BEFORE_REPLACE_ASSET', |
||
369 | __METHOD__ |
||
370 | ); |
||
371 | $element = $event->asset; |
||
372 | // Purge the URL |
||
373 | $purgeUrl = ImageOptimize::$plugin->transformMethod->getPurgeUrl($element); |
||
374 | if ($purgeUrl) { |
||
375 | ImageOptimize::$plugin->transformMethod->purgeUrl($purgeUrl); |
||
376 | } |
||
377 | } |
||
378 | ); |
||
379 | |||
380 | // Handler: Assets::EVENT_AFTER_REPLACE_ASSET |
||
381 | Event::on( |
||
382 | Assets::class, |
||
383 | Assets::EVENT_AFTER_REPLACE_ASSET, |
||
384 | static function (ReplaceAssetEvent $event) { |
||
385 | Craft::debug( |
||
386 | 'Assets::EVENT_AFTER_REPLACE_ASSET', |
||
387 | __METHOD__ |
||
388 | ); |
||
389 | $element = $event->asset; |
||
390 | if ($element->id !== null) { |
||
391 | ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id, true); |
||
392 | } |
||
393 | } |
||
394 | ); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Install our Element event handlers |
||
399 | */ |
||
400 | protected function installElementEventHandlers(): void |
||
438 | } |
||
439 | } |
||
440 | ); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Install our miscellaneous event handlers |
||
445 | */ |
||
446 | protected function installMiscEventHandlers(): void |
||
447 | { |
||
448 | // Handler: Fields::EVENT_AFTER_SAVE_FIELD |
||
449 | Event::on( |
||
450 | Fields::class, |
||
451 | Fields::EVENT_AFTER_SAVE_FIELD, |
||
452 | function (FieldEvent $event) { |
||
453 | Craft::debug( |
||
454 | 'Fields::EVENT_AFTER_SAVE_FIELD', |
||
455 | __METHOD__ |
||
456 | ); |
||
457 | /* @var Settings $settings */ |
||
458 | $settings = $this->getSettings(); |
||
459 | /** @var Field $field */ |
||
460 | if (($settings !== null) && !$event->isNew && $settings->automaticallyResaveImageVariants) { |
||
461 | $this->checkForOptimizedImagesField($event); |
||
462 | } |
||
463 | } |
||
464 | ); |
||
465 | |||
466 | // Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS |
||
467 | Event::on( |
||
468 | Plugins::class, |
||
469 | Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS, |
||
470 | function (PluginEvent $event) { |
||
471 | if ($event->plugin === $this) { |
||
472 | Craft::debug( |
||
473 | 'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS', |
||
474 | __METHOD__ |
||
475 | ); |
||
476 | /* @var Settings $settings */ |
||
477 | $settings = $this->getSettings(); |
||
478 | if (($settings !== null) && $settings->automaticallyResaveImageVariants) { |
||
479 | // After they have changed the settings, resave all the assets |
||
480 | ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets(); |
||
481 | } |
||
482 | } |
||
483 | } |
||
484 | ); |
||
485 | |||
486 | // Handler: Volumes::EVENT_AFTER_SAVE_VOLUME |
||
487 | Event::on( |
||
488 | Volumes::class, |
||
489 | Volumes::EVENT_AFTER_SAVE_VOLUME, |
||
490 | function (VolumeEvent $event) { |
||
491 | Craft::debug( |
||
492 | 'Volumes::EVENT_AFTER_SAVE_VOLUME', |
||
493 | __METHOD__ |
||
494 | ); |
||
495 | /* @var Settings $settings */ |
||
496 | $settings = $this->getSettings(); |
||
497 | // Only worry about this volume if it's not new |
||
498 | if (($settings !== null) && !$event->isNew && $settings->automaticallyResaveImageVariants) { |
||
499 | $volume = $event->volume; |
||
500 | ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume); |
||
501 | } |
||
502 | } |
||
503 | ); |
||
504 | |||
505 | // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
||
506 | Event::on( |
||
507 | Plugins::class, |
||
508 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
509 | function (PluginEvent $event) { |
||
510 | if ($event->plugin === $this) { |
||
511 | $request = Craft::$app->getRequest(); |
||
512 | if ($request->isCpRequest) { |
||
513 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send(); |
||
514 | } |
||
515 | } |
||
516 | } |
||
517 | ); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Install site event listeners for site requests only |
||
522 | */ |
||
523 | protected function installSiteEventListeners(): void |
||
524 | { |
||
525 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
526 | Event::on( |
||
527 | UrlManager::class, |
||
528 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
529 | function (RegisterUrlRulesEvent $event) { |
||
530 | Craft::debug( |
||
531 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
532 | __METHOD__ |
||
533 | ); |
||
534 | // Register our Control Panel routes |
||
535 | $event->rules = array_merge( |
||
536 | $event->rules, |
||
537 | $this->customFrontendRoutes() |
||
538 | ); |
||
539 | } |
||
540 | ); |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Install site event listeners for cp requests only |
||
545 | */ |
||
546 | protected function installCpEventListeners(): void |
||
547 | { |
||
548 | // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
||
549 | Event::on( |
||
550 | Plugins::class, |
||
551 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
552 | static function () { |
||
553 | // Install these only after all other plugins have loaded |
||
554 | Event::on( |
||
555 | View::class, |
||
556 | View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
||
557 | static function (RegisterTemplateRootsEvent $e) { |
||
558 | // Register the root directodies |
||
559 | $allImageTransformTypes = ImageOptimize::$plugin->optimize->getAllImageTransformTypes(); |
||
560 | /** @var ImageTransformInterface $imageTransformType */ |
||
561 | foreach ($allImageTransformTypes as $imageTransformType) { |
||
562 | [$id, $baseDir] = $imageTransformType::getTemplatesRoot(); |
||
563 | if (is_dir($baseDir)) { |
||
564 | $e->roots[$id] = $baseDir; |
||
565 | } |
||
566 | } |
||
567 | } |
||
568 | ); |
||
569 | } |
||
570 | ); |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * Return the custom frontend routes |
||
575 | * |
||
576 | * @return array |
||
577 | */ |
||
578 | protected function customFrontendRoutes(): array |
||
581 | ]; |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * If the Field being saved is an OptimizedImages field, re-save the |
||
586 | * responsive image variants automatically |
||
587 | * |
||
588 | * @param FieldEvent $event |
||
589 | */ |
||
590 | protected function checkForOptimizedImagesField(FieldEvent $event): void |
||
616 |