-
Notifications
You must be signed in to change notification settings - Fork 379
/
CacheManager.php
300 lines (258 loc) · 8.94 KB
/
CacheManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Imagine\Cache;
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Events\CacheResolveEvent;
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Liip\ImagineBundle\ImagineEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
class CacheManager
{
/**
* @var FilterConfiguration
*/
protected $filterConfig;
/**
* @var RouterInterface
*/
protected $router;
/**
* @var ResolverInterface[]
*/
protected $resolvers = [];
/**
* @var SignerInterface
*/
protected $signer;
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
/**
* @var string
*/
protected $defaultResolver;
/**
* @var bool
*/
private $webpGenerate;
/**
* Constructs the cache manager to handle Resolvers based on the provided FilterConfiguration.
*
* @param string $defaultResolver
* @param bool $webpGenerate
*/
public function __construct(
FilterConfiguration $filterConfig,
RouterInterface $router,
SignerInterface $signer,
EventDispatcherInterface $dispatcher,
$defaultResolver = null,
$webpGenerate = false
) {
$this->filterConfig = $filterConfig;
$this->router = $router;
$this->signer = $signer;
$this->dispatcher = $dispatcher;
$this->defaultResolver = $defaultResolver ?: 'default';
$this->webpGenerate = $webpGenerate;
}
/**
* Adds a resolver to handle cached images for the given filter.
*
* @param string $filter
*/
public function addResolver($filter, ResolverInterface $resolver)
{
$this->resolvers[$filter] = $resolver;
if ($resolver instanceof CacheManagerAwareInterface) {
$resolver->setCacheManager($this);
}
}
/**
* Gets filtered path for rendering in the browser.
* It could be the cached one or an url of filter action.
*
* @param string $path The path where the resolved file is expected
* @param string $filter
* @param string $resolver
* @param int $referenceType
*
* @return string
*/
public function getBrowserPath($path, $filter, array $runtimeConfig = [], $resolver = null, $referenceType = UrlGeneratorInterface::ABSOLUTE_URL)
{
if (!empty($runtimeConfig)) {
$rcPath = $this->getRuntimePath($path, $runtimeConfig);
return !$this->webpGenerate && $this->isStored($rcPath, $filter, $resolver) ?
$this->resolve($rcPath, $filter, $resolver) :
$this->generateUrl($path, $filter, $runtimeConfig, $resolver, $referenceType);
}
return !$this->webpGenerate && $this->isStored($path, $filter, $resolver) ?
$this->resolve($path, $filter, $resolver) :
$this->generateUrl($path, $filter, [], $resolver, $referenceType);
}
/**
* Get path to runtime config image.
*
* @param string $path
*
* @return string
*/
public function getRuntimePath($path, array $runtimeConfig)
{
$path = ltrim($path, '/');
return 'rc/'.$this->signer->sign($path, $runtimeConfig).'/'.$path;
}
/**
* Returns a web accessible URL.
*
* @param string $path The path where the resolved file is expected
* @param string $filter The name of the imagine filter in effect
* @param string $resolver
* @param int $referenceType The type of reference to be generated (one of the UrlGenerator constants)
*
* @return string
*/
public function generateUrl($path, $filter, array $runtimeConfig = [], $resolver = null, $referenceType = UrlGeneratorInterface::ABSOLUTE_URL)
{
$params = [
'path' => ltrim($path, '/'),
'filter' => $filter,
];
if ($resolver) {
$params['resolver'] = $resolver;
}
if (empty($runtimeConfig)) {
$filterUrl = $this->router->generate('liip_imagine_filter', $params, $referenceType);
} else {
$params['filters'] = $runtimeConfig;
$params['hash'] = $this->signer->sign($path, $runtimeConfig);
$filterUrl = $this->router->generate('liip_imagine_filter_runtime', $params, $referenceType);
}
return $filterUrl;
}
/**
* Checks whether the path is already stored within the respective Resolver.
*
* @param string $path
* @param string $filter
* @param string $resolver
*
* @return bool
*/
public function isStored($path, $filter, $resolver = null)
{
return $this->getResolver($filter, $resolver)->isStored($path, $filter);
}
/**
* Resolves filtered path for rendering in the browser.
*
* @param string $path
* @param string $filter
* @param string $resolver
*
* @throws NotFoundHttpException if the path can not be resolved
*
* @return string The url of resolved image
*/
public function resolve($path, $filter, $resolver = null)
{
if (false !== mb_strpos($path, '/../') || 0 === mb_strpos($path, '../')) {
throw new NotFoundHttpException(\sprintf("Source image was searched with '%s' outside of the defined root path", $path));
}
$preEvent = new CacheResolveEvent($path, $filter);
$this->dispatchWithBC($preEvent, ImagineEvents::PRE_RESOLVE);
$url = $this->getResolver($preEvent->getFilter(), $resolver)->resolve($preEvent->getPath(), $preEvent->getFilter());
$postEvent = new CacheResolveEvent($preEvent->getPath(), $preEvent->getFilter(), $url);
$this->dispatchWithBC($postEvent, ImagineEvents::POST_RESOLVE);
return $postEvent->getUrl();
}
/**
* @see ResolverInterface::store
*
* @param string $path
* @param string $filter
* @param string $resolver
*/
public function store(BinaryInterface $binary, $path, $filter, $resolver = null)
{
$this->getResolver($filter, $resolver)->store($binary, $path, $filter);
}
/**
* @param string|string[]|null $paths
* @param string|string[]|null $filters
*/
public function remove($paths = null, $filters = null)
{
if (null === $filters) {
$filters = array_keys($this->filterConfig->all());
} elseif (!\is_array($filters)) {
$filters = [$filters];
}
if (!\is_array($paths)) {
$paths = [$paths];
}
$paths = array_filter($paths);
$filters = array_filter($filters);
$mapping = new \SplObjectStorage();
foreach ($filters as $filter) {
$resolver = $this->getResolver($filter, null);
$list = isset($mapping[$resolver]) ? $mapping[$resolver] : [];
$list[] = $filter;
$mapping[$resolver] = $list;
}
foreach ($mapping as $resolver) {
$resolver->remove($paths, $mapping[$resolver]);
}
}
/**
* Gets a resolver for the given filter.
*
* In case there is no specific resolver, but a default resolver has been configured, the default will be returned.
*
* @param string $filter
* @param string $resolver
*
* @throws \OutOfBoundsException If neither a specific nor a default resolver is available
*
* @return ResolverInterface
*/
protected function getResolver($filter, $resolver)
{
// BC
if (!$resolver) {
$config = $this->filterConfig->get($filter);
$resolverName = empty($config['cache']) ? $this->defaultResolver : $config['cache'];
} else {
$resolverName = $resolver;
}
if (!isset($this->resolvers[$resolverName])) {
throw new \OutOfBoundsException(\sprintf('Could not find resolver "%s" for "%s" filter type', $resolverName, $filter));
}
return $this->resolvers[$resolverName];
}
/**
* BC Layer for Symfony < 4.3
*/
private function dispatchWithBC(CacheResolveEvent $event, string $eventName): void
{
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($event, $eventName);
} else {
$this->dispatcher->dispatch($eventName, $event);
}
}
}