1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Service; |
13
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Binary\BinaryInterface; |
15
|
|
|
use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException; |
16
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
17
|
|
|
use Liip\ImagineBundle\Imagine\Data\DataManager; |
18
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterManager; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
use Psr\Log\NullLogger; |
21
|
|
|
|
22
|
|
|
class FilterService |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var DataManager |
26
|
|
|
*/ |
27
|
|
|
private $dataManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var FilterManager |
31
|
|
|
*/ |
32
|
|
|
private $filterManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CacheManager |
36
|
|
|
*/ |
37
|
|
|
private $cacheManager; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var LoggerInterface |
41
|
|
|
*/ |
42
|
|
|
private $logger; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $webpGenerate; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $webpOptions; |
53
|
|
|
|
54
|
|
|
public function __construct( |
55
|
|
|
DataManager $dataManager, |
56
|
|
|
FilterManager $filterManager, |
57
|
|
|
CacheManager $cacheManager, |
58
|
|
|
bool $webpGenerate, |
59
|
|
|
array $webpOptions, |
60
|
|
|
?LoggerInterface $logger = null |
61
|
|
|
) { |
62
|
|
|
$this->dataManager = $dataManager; |
63
|
|
|
$this->filterManager = $filterManager; |
64
|
|
|
$this->cacheManager = $cacheManager; |
65
|
|
|
$this->webpGenerate = $webpGenerate; |
66
|
|
|
$this->webpOptions = $webpOptions; |
67
|
|
|
$this->logger = $logger ?: new NullLogger(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $path |
72
|
|
|
* @param string $filter |
73
|
|
|
*/ |
74
|
|
|
public function bustCache($path, $filter) |
75
|
|
|
{ |
76
|
|
|
$basePathContainer = new FilterPathContainer($path); |
77
|
|
|
$filterPathContainers = [$basePathContainer]; |
78
|
|
|
|
79
|
|
|
if ($this->webpGenerate) { |
80
|
|
|
$filterPathContainers[] = $basePathContainer->createWebp($this->webpOptions); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($filterPathContainers as $filterPathContainer) { |
84
|
|
|
if ($this->cacheManager->isStored($filterPathContainer->getTarget(), $filter)) { |
85
|
|
|
$this->cacheManager->remove($filterPathContainer->getTarget(), $filter); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $path |
92
|
|
|
* @param string $filter |
93
|
|
|
* @param string|null $resolver |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getUrlOfFilteredImage($path, $filter, $resolver = null, bool $webpSupported = false) |
98
|
|
|
{ |
99
|
|
|
$basePathContainer = new FilterPathContainer($path); |
100
|
|
|
|
101
|
|
|
return $this->getUrlOfFilteredImageByContainer($basePathContainer, $filter, $resolver, $webpSupported); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $path |
106
|
|
|
* @param string $filter |
107
|
|
|
* @param string|null $resolver |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getUrlOfFilteredImageWithRuntimeFilters( |
112
|
|
|
$path, |
113
|
|
|
$filter, |
114
|
|
|
array $runtimeFilters = [], |
115
|
|
|
$resolver = null, |
116
|
|
|
bool $webpSupported = false |
117
|
|
|
) { |
118
|
|
|
$runtimePath = $this->cacheManager->getRuntimePath($path, $runtimeFilters); |
119
|
|
|
$basePathContainer = new FilterPathContainer($path, $runtimePath, [ |
120
|
|
|
'filters' => $runtimeFilters, |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
return $this->getUrlOfFilteredImageByContainer($basePathContainer, $filter, $resolver, $webpSupported); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function getUrlOfFilteredImageByContainer( |
127
|
|
|
FilterPathContainer $basePathContainer, |
128
|
|
|
string $filter, |
129
|
|
|
?string $resolver = null, |
130
|
|
|
bool $webpSupported = false |
131
|
|
|
): string { |
132
|
|
|
$filterPathContainers = [$basePathContainer]; |
133
|
|
|
|
134
|
|
|
if ($this->webpGenerate) { |
135
|
|
|
$webpPathContainer = $basePathContainer->createWebp($this->webpOptions); |
136
|
|
|
$filterPathContainers[] = $webpPathContainer; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
foreach ($filterPathContainers as $filterPathContainer) { |
140
|
|
|
if (!$this->cacheManager->isStored($filterPathContainer->getTarget(), $filter, $resolver)) { |
141
|
|
|
$this->cacheManager->store( |
142
|
|
|
$this->createFilteredBinary($filterPathContainer, $filter), |
143
|
|
|
$filterPathContainer->getTarget(), |
144
|
|
|
$filter, |
145
|
|
|
$resolver |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($webpSupported && isset($webpPathContainer)) { |
151
|
|
|
return $this->cacheManager->resolve($webpPathContainer->getTarget(), $filter, $resolver); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->cacheManager->resolve($basePathContainer->getTarget(), $filter, $resolver); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @throws NonExistingFilterException |
159
|
|
|
*/ |
160
|
|
|
private function createFilteredBinary(FilterPathContainer $filterPathContainer, string $filter): BinaryInterface |
161
|
|
|
{ |
162
|
|
|
$binary = $this->dataManager->find($filter, $filterPathContainer->getSource()); |
163
|
|
|
|
164
|
|
|
try { |
165
|
|
|
return $this->filterManager->applyFilter($binary, $filter, $filterPathContainer->getOptions()); |
166
|
|
|
} catch (NonExistingFilterException $e) { |
167
|
|
|
$this->logger->debug(sprintf( |
168
|
|
|
'Could not locate filter "%s" for path "%s". Message was "%s"', |
169
|
|
|
$filter, |
170
|
|
|
$filterPathContainer->getSource(), |
171
|
|
|
$e->getMessage() |
172
|
|
|
)); |
173
|
|
|
|
174
|
|
|
throw $e; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|