1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rvdlee\ZfImageOptimiser\Service; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Rvdlee\ZfImageOptimiser\Exception\InvalidArgumentException; |
7
|
|
|
use Rvdlee\ZfImageOptimiser\Interfaces\ImageOptimiserInterface; |
8
|
|
|
use Rvdlee\ZfImageOptimiser\Model\Image; |
9
|
|
|
use Zend\Log\LoggerInterface; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class ImageOptimiserService |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array|ImageOptimiserInterface |
15
|
|
|
*/ |
16
|
|
|
protected $adapters; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var LoggerInterface |
20
|
|
|
*/ |
21
|
|
|
protected $logger; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array|ImageOptimiserInterface $adapters |
25
|
|
|
* |
26
|
|
|
* @throws InvalidArgumentException |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $adapters, LoggerInterface $logger) |
29
|
|
|
{ |
30
|
|
|
$this->setLogger($logger); |
31
|
|
|
$this->getLogger()->info('Constucting ImageOptimiserService'); |
32
|
|
|
|
33
|
|
|
/** @var ImageOptimiserInterface $adapter */ |
34
|
|
|
foreach ($adapters as $adapter) { |
35
|
|
|
if ( ! $adapter instanceof ImageOptimiserInterface) { |
36
|
|
|
throw new InvalidArgumentException( |
37
|
|
|
sprintf('Adapter configured does not have the %s interface.', ImageOptimiserInterface::class) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->getLogger()->debug(sprintf('Added %s to the ImageOptimiserService.', get_class($adapter))); |
42
|
|
|
$this->addAdapter($adapter); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $imagePath |
48
|
|
|
* |
49
|
|
|
* @return mixed|void |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
|
|
public function optimize(string $imagePath) |
53
|
|
|
{ |
54
|
|
|
/** @var ImageOptimiserInterface $adapter */ |
55
|
|
|
foreach ($this->getAdapters() as $adapter) { |
56
|
|
|
try { |
57
|
|
|
if ($adapter->canHandle($imagePath)) { |
58
|
|
|
/** @var string $command */ |
59
|
|
|
$command = $adapter->optimizeCommand(new Image($imagePath)); |
60
|
|
|
|
61
|
|
|
$this->getLogger()->info(sprintf('Handling %s via %s', $imagePath, get_class($adapter))); |
62
|
|
|
$output = shell_exec(sprintf('%s 2>&1', $command)); |
63
|
|
|
$this->getLogger()->info($output); |
64
|
|
|
} |
65
|
|
|
} catch (Exception $exception) { |
66
|
|
|
$this->getLogger()->err( |
67
|
|
|
sprintf('Exception when handling %s to via %s', $imagePath, get_class($adapter)) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array|ImageOptimiserInterface |
75
|
|
|
*/ |
76
|
|
|
public function getAdapters() |
77
|
|
|
{ |
78
|
|
|
return $this->adapters; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function addAdapter(ImageOptimiserInterface $adapter) |
82
|
|
|
{ |
83
|
|
|
$this->adapters[] = $adapter; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return LoggerInterface |
88
|
|
|
*/ |
89
|
|
|
public function getLogger() : LoggerInterface |
90
|
|
|
{ |
91
|
|
|
return $this->logger; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param LoggerInterface $logger |
96
|
|
|
* |
97
|
|
|
* @return ImageOptimiserService |
98
|
|
|
*/ |
99
|
|
|
public function setLogger(LoggerInterface $logger) : ImageOptimiserService |
100
|
|
|
{ |
101
|
|
|
$this->logger = $logger; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths