1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ImageOptimize plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Automatically optimize images after they've been transformed |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\services; |
12
|
|
|
|
13
|
|
|
use nystudio107\imageoptimize\assetbundles\imageoptimize\ImageOptimizeAsset; |
14
|
|
|
use nystudio107\imageoptimize\services\Optimize as OptimizeService; |
15
|
|
|
use nystudio107\imageoptimize\services\OptimizedImages as OptimizedImagesService; |
16
|
|
|
use nystudio107\imageoptimize\services\Placeholder as PlaceholderService; |
17
|
|
|
use nystudio107\pluginvite\services\VitePluginService; |
18
|
|
|
use yii\base\InvalidConfigException; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package ImageOptimize |
|
|
|
|
23
|
|
|
* @since 4.0.3 |
|
|
|
|
24
|
|
|
* |
25
|
|
|
* @property OptimizeService $optimize |
26
|
|
|
* @property PlaceholderService $placeholder |
27
|
|
|
* @property OptimizedImagesService $optimizedImages |
28
|
|
|
* @property VitePluginService $vite |
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
trait ServicesTrait |
31
|
|
|
{ |
32
|
|
|
// Public Static Methods |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
|
|
|
|
38
|
|
|
public static function config(): array |
39
|
|
|
{ |
40
|
|
|
// Constants aren't allowed in traits until PHP >= 8.2, and config() is called before __construct(), |
41
|
|
|
// so we can't extract it from the passed in $config |
42
|
|
|
$majorVersion = '5'; |
43
|
|
|
// Dev server container name & port are based on the major version of this plugin |
44
|
|
|
$devPort = 3000 + (int)$majorVersion; |
45
|
|
|
$versionName = 'v' . $majorVersion; |
46
|
|
|
return [ |
47
|
|
|
'components' => [ |
48
|
|
|
'optimize' => OptimizeService::class, |
49
|
|
|
'optimizedImages' => OptimizedImagesService::class, |
50
|
|
|
'placeholder' => PlaceholderService::class, |
51
|
|
|
// Register the vite service |
52
|
|
|
'vite' => [ |
53
|
|
|
'assetClass' => ImageOptimizeAsset::class, |
54
|
|
|
'checkDevServer' => true, |
55
|
|
|
'class' => VitePluginService::class, |
56
|
|
|
'devServerInternal' => 'http://craft-imageoptimize-' . $versionName . '-buildchain-dev:' . $devPort, |
57
|
|
|
'devServerPublic' => 'http://localhost:' . $devPort, |
58
|
|
|
'errorEntry' => 'src/js/ImageOptimize.js', |
59
|
|
|
'useDevServer' => true, |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Public Methods |
66
|
|
|
// ========================================================================= |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the optimize service |
70
|
|
|
* |
71
|
|
|
* @return OptimizeService The optimize service |
72
|
|
|
* @throws InvalidConfigException |
73
|
|
|
*/ |
74
|
|
|
public function getOptimize(): OptimizeService |
75
|
|
|
{ |
76
|
|
|
return $this->get('optimize'); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the optimizedImages service |
81
|
|
|
* |
82
|
|
|
* @return OptimizedImagesService The optimizedImages service |
83
|
|
|
* @throws InvalidConfigException |
84
|
|
|
*/ |
85
|
|
|
public function getOptimizedImages(): OptimizedImagesService |
86
|
|
|
{ |
87
|
|
|
return $this->get('optimizedImages'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the placeholder service |
92
|
|
|
* |
93
|
|
|
* @return PlaceholderService The placeholder service |
94
|
|
|
* @throws InvalidConfigException |
95
|
|
|
*/ |
96
|
|
|
public function getPlaceholder(): PlaceholderService |
97
|
|
|
{ |
98
|
|
|
return $this->get('placeholder'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the vite service |
103
|
|
|
* |
104
|
|
|
* @return VitePluginService The vite service |
105
|
|
|
* @throws InvalidConfigException |
106
|
|
|
*/ |
107
|
|
|
public function getVite(): VitePluginService |
108
|
|
|
{ |
109
|
|
|
return $this->get('vite'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|