1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ImageOptimizer\Optimizers; |
4
|
|
|
|
5
|
|
|
use Spatie\ImageOptimizer\Optimizer; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
use Spatie\ImageOptimizer\OptimizerChain; |
8
|
|
|
|
9
|
|
|
abstract class BaseOptimizer implements Optimizer |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Options. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
public $options = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Image path. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $imagePath = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Binary path. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $binaryPath = ''; |
31
|
|
|
|
32
|
|
|
public function __construct($options = []) |
33
|
|
|
{ |
34
|
|
|
$this->setOptions($options); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set binary Path. |
40
|
|
|
* |
41
|
|
|
* @param string|array $binaryPath |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function setBinaryPath($binaryPath) |
45
|
|
|
{ |
46
|
|
|
$this->binaryPath = $binaryPath; |
|
|
|
|
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get binary path. |
53
|
|
|
* |
54
|
|
|
* @return string|array |
55
|
|
|
*/ |
56
|
|
|
public function binaryPath() |
57
|
|
|
{ |
58
|
|
|
return $this->binaryPath; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function binaryName(): string |
62
|
|
|
{ |
63
|
|
|
return $this->binaryName; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setImagePath(string $imagePath) |
67
|
|
|
{ |
68
|
|
|
$this->imagePath = $imagePath; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setOptions(array $options = []) |
74
|
|
|
{ |
75
|
|
|
$this->options = $options; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Authomatically detect the path where the image optimizes is installed. |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function checkBinary() |
88
|
|
|
{ |
89
|
|
|
// check binary by a given list of binary path |
90
|
|
|
if (is_array($this->binaryPath())) { |
91
|
|
|
foreach ($this->binaryPath() as $path) { |
92
|
|
|
$path = rtrim($path, '/').'/'; |
93
|
|
|
$process = new Process('which -a '.$path.''.$this->binaryName()); |
94
|
|
|
$process->setTimeout(null); |
95
|
|
|
$process->run(); |
96
|
|
|
|
97
|
|
|
if ($process->isSuccessful()) { |
98
|
|
|
$this->setBinaryPath($path); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$binaryPath = implode(',', array_values($this->binaryPath())); |
104
|
|
|
|
105
|
|
|
// if we come so far, it means the binary could not be found |
106
|
|
|
(new OptimizerChain())->getLogger()->error('Binary could not be found in any of the following configured paths: '. $binaryPath .''); |
107
|
|
|
|
108
|
|
|
// Although a given list of possible binary path has been given, the binary may exists |
109
|
|
|
// in the global environment. Therefore, we will unset binary path list so we can later |
110
|
|
|
// check if it exists the global environment |
111
|
|
|
$this->setBinaryPath(''); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// check if binary exists in the global environment |
115
|
|
|
$process = new Process('which -a '.$this->binaryName()); |
116
|
|
|
$process->setTimeout(null); |
117
|
|
|
$process->run(); |
118
|
|
|
if ($process->isSuccessful()) { |
119
|
|
|
return $this; |
120
|
|
|
}else{ |
121
|
|
|
(new OptimizerChain())->getLogger()->error('Binary could not be found: `'.$this->binaryName().'`'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getCommand(): string |
128
|
|
|
{ |
129
|
|
|
$optionString = implode(' ', $this->options); |
130
|
|
|
$fullBinaryPath = $this->checkBinary()->binaryPath().$this->binaryName(); |
131
|
|
|
|
132
|
|
|
return "\"{$fullBinaryPath}\" {$optionString} ".escapeshellarg($this->imagePath); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.