|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\LaravelImageOptimizer; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Spatie\ImageOptimizer\Optimizer; |
|
7
|
|
|
use Spatie\ImageOptimizer\DummyLogger; |
|
8
|
|
|
use Spatie\ImageOptimizer\OptimizerChain; |
|
9
|
|
|
use Spatie\LaravelImageOptimizer\Exceptions\InvalidConfiguration; |
|
10
|
|
|
|
|
11
|
|
|
class OptimizerChainFactory |
|
12
|
|
|
{ |
|
13
|
|
|
public static function create(array $config) |
|
14
|
|
|
{ |
|
15
|
|
|
return (new OptimizerChain()) |
|
16
|
|
|
->useLogger(static::getLogger($config)) |
|
17
|
|
|
->setTimeout($config['timeout']) |
|
18
|
|
|
->setOptimizers(static::getOptimizers($config)); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
protected static function getLogger($config): LoggerInterface |
|
22
|
|
|
{ |
|
23
|
|
|
$configuredLogger = $config['log_optimizer_activity']; |
|
24
|
|
|
|
|
25
|
|
|
if ($configuredLogger === true) { |
|
26
|
|
|
return app('log'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ($configuredLogger === false) { |
|
30
|
|
|
return new DummyLogger(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if (! $configuredLogger instanceof LoggerInterface) { |
|
34
|
|
|
throw InvalidConfiguration::notAnLogger($configuredLogger); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return new $configuredLogger; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected static function getOptimizers(array $config) |
|
41
|
|
|
{ |
|
42
|
|
|
return collect($config['optimizers']) |
|
43
|
|
|
->mapWithKeys(function (array $options, string $optimizerClass) use($config) { |
|
44
|
|
|
if (! is_a($optimizerClass, Optimizer::class, true)) { |
|
45
|
|
|
throw InvalidConfiguration::notAnOptimizer($optimizerClass); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Initialize optimizer class |
|
49
|
|
|
$newOptimizerClass = new $optimizerClass(); |
|
50
|
|
|
|
|
51
|
|
|
if(static::getBinaryPath($config)){ |
|
52
|
|
|
$newOptimizerClass->setBinaryPath(self::getBinaryPath($config)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$newOptimizerClass->setOptions($options); |
|
56
|
|
|
|
|
57
|
|
|
return [$optimizerClass => $newOptimizerClass]; |
|
58
|
|
|
}) |
|
59
|
|
|
->toArray(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
public static function getBinaryPath(array $config): string { |
|
64
|
|
|
return $config['binary_path'] ?? ''; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|