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 craft\helpers\ArrayHelper; |
14
|
|
|
use nystudio107\imageoptimize\assetbundles\imageoptimize\ImageOptimizeAsset; |
15
|
|
|
use nystudio107\imageoptimize\services\Optimize as OptimizeService; |
16
|
|
|
use nystudio107\imageoptimize\services\OptimizedImages as OptimizedImagesService; |
17
|
|
|
use nystudio107\imageoptimize\services\Placeholder as PlaceholderService; |
18
|
|
|
use nystudio107\pluginvite\services\VitePluginService; |
19
|
|
|
use yii\base\InvalidConfigException; |
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @author nystudio107 |
|
|
|
|
23
|
|
|
* @package ImageOptimize |
|
|
|
|
24
|
|
|
* @since 1.6.49 |
|
|
|
|
25
|
|
|
* |
26
|
|
|
* @property OptimizeService $optimize |
27
|
|
|
* @property PlaceholderService $placeholder |
28
|
|
|
* @property OptimizedImagesService $optimizedImages |
29
|
|
|
* @property VitePluginService $vite |
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
trait ServicesTrait |
32
|
|
|
{ |
33
|
|
|
// Public Methods |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function __construct($id, $parent = null, array $config = []) |
40
|
|
|
{ |
41
|
|
|
// Merge in the passed config, so it our config can be overridden by Plugins::pluginConfigs['vite'] |
42
|
|
|
// ref: https://github.com/craftcms/cms/issues/1989 |
43
|
|
|
$config = ArrayHelper::merge([ |
|
|
|
|
44
|
|
|
'components' => [ |
45
|
|
|
'optimize' => OptimizeService::class, |
46
|
|
|
'optimizedImages' => OptimizedImagesService::class, |
47
|
|
|
'placeholder' => PlaceholderService::class, |
48
|
|
|
// Register the vite service |
49
|
|
|
'vite' => [ |
50
|
|
|
'class' => VitePluginService::class, |
51
|
|
|
'assetClass' => ImageOptimizeAsset::class, |
52
|
|
|
'useDevServer' => true, |
53
|
|
|
'devServerPublic' => 'http://localhost:3001', |
54
|
|
|
'serverPublic' => 'http://localhost:8000', |
55
|
|
|
'errorEntry' => 'src/js/ImageOptimize.js', |
56
|
|
|
'devServerInternal' => 'http://craft-imageoptimize-buildchain:3001', |
57
|
|
|
'checkDevServer' => true, |
58
|
|
|
], |
59
|
|
|
] |
60
|
|
|
], $config); |
|
|
|
|
61
|
|
|
|
62
|
|
|
parent::__construct($id, $parent, $config); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the optimize service |
67
|
|
|
* |
68
|
|
|
* @return OptimizeService The optimize service |
69
|
|
|
* @throws InvalidConfigException |
70
|
|
|
*/ |
71
|
|
|
public function getOptimize(): OptimizeService |
72
|
|
|
{ |
73
|
|
|
return $this->get('optimize'); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the optimizedImages service |
78
|
|
|
* |
79
|
|
|
* @return OptimizedImagesService The optimizedImages service |
80
|
|
|
* @throws InvalidConfigException |
81
|
|
|
*/ |
82
|
|
|
public function getOptimizedImages(): OptimizedImagesService |
83
|
|
|
{ |
84
|
|
|
return $this->get('optimizedImages'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the placeholder service |
89
|
|
|
* |
90
|
|
|
* @return PlaceholderService The placeholder service |
91
|
|
|
* @throws InvalidConfigException |
92
|
|
|
*/ |
93
|
|
|
public function getPlaceholder(): PlaceholderService |
94
|
|
|
{ |
95
|
|
|
return $this->get('placeholder'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the vite service |
100
|
|
|
* |
101
|
|
|
* @return VitePluginService The vite service |
102
|
|
|
* @throws InvalidConfigException |
103
|
|
|
*/ |
104
|
|
|
public function getVite(): VitePluginService |
105
|
|
|
{ |
106
|
|
|
return $this->get('vite'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|