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
|
|
|
* @param DataManager $dataManager |
46
|
|
|
* @param FilterManager $filterManager |
47
|
|
|
* @param CacheManager $cacheManager |
48
|
|
|
* @param LoggerInterface $logger |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
DataManager $dataManager, |
52
|
|
|
FilterManager $filterManager, |
53
|
|
|
CacheManager $cacheManager, |
54
|
|
|
LoggerInterface $logger = null |
55
|
|
|
) { |
56
|
|
|
$this->dataManager = $dataManager; |
57
|
|
|
$this->filterManager = $filterManager; |
58
|
|
|
$this->cacheManager = $cacheManager; |
59
|
|
|
$this->logger = $logger ?: new NullLogger(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $path |
64
|
|
|
* @param string $filter |
65
|
|
|
*/ |
66
|
|
|
public function bustCache($path, $filter) |
67
|
|
|
{ |
68
|
|
|
if (!$this->cacheManager->isStored($path, $filter)) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->cacheManager->remove($path, $filter); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $path |
77
|
|
|
* @param string $filter |
78
|
|
|
* @param string $resolver |
79
|
|
|
* @param bool $webp |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getUrlOfFilteredImage($path, $filter, $resolver = null, $webp = false) |
84
|
|
|
{ |
85
|
|
|
if ($this->cacheManager->isStored($path, $filter, $resolver)) { |
86
|
|
|
return $this->cacheManager->resolve($path, $filter, $resolver); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$filteredBinary = $this->createFilteredBinary( |
90
|
|
|
$path, |
91
|
|
|
$filter |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->cacheManager->store( |
95
|
|
|
$filteredBinary, |
96
|
|
|
$path, |
97
|
|
|
$filter, |
98
|
|
|
$resolver |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
// add webp in new location |
102
|
|
|
$filteredWebpBinary = $this->createFilteredWebpBinary($path, $filter); |
103
|
|
|
$this->cacheManager->store($filteredWebpBinary, $path.'.webp', $filter, $resolver); |
104
|
|
|
|
105
|
|
|
if ($webp) { |
106
|
|
|
return $this->cacheManager->resolve($path.'.webp', $filter, $resolver); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->cacheManager->resolve($path, $filter, $resolver); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $path |
114
|
|
|
* @param string $filter |
115
|
|
|
* @param array $runtimeFilters |
116
|
|
|
* @param string|null $resolver |
117
|
|
|
* @param bool $webp |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getUrlOfFilteredImageWithRuntimeFilters( |
122
|
|
|
$path, |
123
|
|
|
$filter, |
124
|
|
|
array $runtimeFilters = [], |
125
|
|
|
$resolver = null, |
126
|
|
|
$webp = false |
127
|
|
|
) { |
128
|
|
|
$runtimePath = $this->cacheManager->getRuntimePath($path, $runtimeFilters); |
129
|
|
|
if ($this->cacheManager->isStored($runtimePath, $filter, $resolver)) { |
130
|
|
|
return $this->cacheManager->resolve($runtimePath, $filter, $resolver); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$filteredBinary = $this->createFilteredBinary( |
134
|
|
|
$path, |
135
|
|
|
$filter, |
136
|
|
|
$runtimeFilters |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$this->cacheManager->store( |
140
|
|
|
$filteredBinary, |
141
|
|
|
$runtimePath, |
142
|
|
|
$filter, |
143
|
|
|
$resolver |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
// add webp in new location |
147
|
|
|
$filteredWebpBinary = $this->createFilteredWebpBinary($path, $filter); |
148
|
|
|
$this->cacheManager->store($filteredWebpBinary, $runtimePath.'.webp', $filter, $resolver); |
149
|
|
|
|
150
|
|
|
if ($webp) { |
151
|
|
|
return $this->cacheManager->resolve($runtimePath.'.webp', $filter, $resolver); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->cacheManager->resolve($runtimePath, $filter, $resolver); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $path |
159
|
|
|
* @param string $filter |
160
|
|
|
* @param array $runtimeFilters |
161
|
|
|
* |
162
|
|
|
* @throws NonExistingFilterException |
163
|
|
|
* |
164
|
|
|
* @return BinaryInterface |
165
|
|
|
*/ |
166
|
|
|
private function createFilteredBinary($path, $filter, array $runtimeFilters = []) |
167
|
|
|
{ |
168
|
|
|
$binary = $this->dataManager->find($filter, $path); |
169
|
|
|
|
170
|
|
|
try { |
171
|
|
|
return $this->filterManager->applyFilter($binary, $filter, [ |
172
|
|
|
'filters' => $runtimeFilters, |
173
|
|
|
]); |
174
|
|
|
} catch (NonExistingFilterException $e) { |
175
|
|
|
$message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $path, $e->getMessage()); |
176
|
|
|
|
177
|
|
|
$this->logger->debug($message); |
178
|
|
|
|
179
|
|
|
throw $e; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $path |
185
|
|
|
* @param string $filter |
186
|
|
|
* @param array $runtimeFilters |
187
|
|
|
* |
188
|
|
|
* @throws NonExistingFilterException |
189
|
|
|
* |
190
|
|
|
* @return BinaryInterface |
191
|
|
|
*/ |
192
|
|
|
private function createFilteredWebpBinary($path, $filter, array $runtimeFilters = []) |
193
|
|
|
{ |
194
|
|
|
return $this->createFilteredBinary($path, $filter, [ |
195
|
|
|
'quality' => 100, |
196
|
|
|
'format' => 'webp', |
197
|
|
|
] + $runtimeFilters); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|