|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ImageOptimizer\Optimizers; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Spatie\ImageOptimizer\DummyLogger; |
|
7
|
|
|
use Spatie\ImageOptimizer\Optimizer; |
|
8
|
|
|
use Symfony\Component\Process\Process; |
|
9
|
|
|
|
|
10
|
|
|
abstract class BaseOptimizer implements Optimizer |
|
11
|
|
|
{ |
|
12
|
|
|
public $options = []; |
|
13
|
|
|
|
|
14
|
|
|
public $imagePath = ''; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* List of binary paths to check for commands. |
|
18
|
|
|
* @var array $binaryPathList |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $binaryPathList = [ |
|
21
|
|
|
'/usr/local', |
|
22
|
|
|
'/usr/local/bin', |
|
23
|
|
|
'/usr/bin', |
|
24
|
|
|
'/usr/sbin', |
|
25
|
|
|
'/usr/local/bin', |
|
26
|
|
|
'/usr/local/sbin', |
|
27
|
|
|
'/bin', |
|
28
|
|
|
'/sbin' |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
32
|
|
|
protected $logger; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Binary name of the optimizer. |
|
36
|
|
|
* @var string $binaryName |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $binaryName; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Binary path. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string $binaryPath |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $binaryPath = ''; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct($options = []) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->useLogger(new DummyLogger()); |
|
50
|
|
|
$this->setOptions($options); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set binary Path |
|
56
|
|
|
* |
|
57
|
|
|
* Useful in case your commands are not accessible by global environment. ex. /usr/bin/local |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $binaryName |
|
|
|
|
|
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setBinaryPath(string $binaryPath) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->binaryPath = $binaryPath; |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get binary path |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function binaryPath(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->binaryPath; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
public function binaryName(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->binaryName; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setImagePath(string $imagePath) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->imagePath = $imagePath; |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function setOptions(array $options = []) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->options = $options; |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function useLogger(LoggerInterface $log) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->logger = $log; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get Binary path list |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function binaryPathList(){ |
|
113
|
|
|
return $this->binaryPathList; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Authomatically detect the path where the image optimizes is installed |
|
118
|
|
|
* |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
|
|
public function detectBinaryPath(){ |
|
122
|
|
|
// first check if comman is executed in a global environment |
|
123
|
|
|
$process = new Process("which -a " .$this->binaryName()); |
|
124
|
|
|
$process->setTimeout(null); |
|
125
|
|
|
$process->run(); |
|
126
|
|
|
if ($process->isSuccessful()) { |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// add custom path (if given in config.php) |
|
131
|
|
|
if($this->binaryPath()) { |
|
132
|
|
|
$this->binaryPathList = [ |
|
133
|
|
|
$this->binaryPath() |
|
134
|
|
|
]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// check if command is found in every given path |
|
138
|
|
|
foreach ($this->binaryPathList() as $path) { |
|
139
|
|
|
$path = rtrim($path, '/') . '/'; |
|
140
|
|
|
$process = new Process("which -a " . $path . '' . $this->binaryName()); |
|
141
|
|
|
$process->setTimeout(null); |
|
142
|
|
|
$process->run(); |
|
143
|
|
|
if ($process->isSuccessful()) { |
|
144
|
|
|
$this->setBinaryPath($path); |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->logger->error("Command could not be executed in any of the following binary path: `".implode(",", array_values($this->binaryPathList))."`}"); |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getCommand(): string |
|
155
|
|
|
{ |
|
156
|
|
|
$optionString = implode(' ', $this->options); |
|
157
|
|
|
$fullBinaryPath = $this->detectBinaryPath()->binaryPath().$this->binaryName(); |
|
158
|
|
|
|
|
159
|
|
|
return "\"{$fullBinaryPath}\" {$optionString} ".escapeshellarg($this->imagePath); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.