|
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\Imagine\Cache\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterManager; |
|
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
16
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
17
|
|
|
|
|
18
|
|
|
class FormatResolver extends WebPathResolver |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var FilterManager |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $filterManager; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $webRootDir |
|
27
|
|
|
* @param string $cachePrefix |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct( |
|
30
|
|
|
Filesystem $filesystem, |
|
31
|
|
|
RequestContext $requestContext, |
|
32
|
|
|
$webRootDir, |
|
33
|
|
|
$cachePrefix, |
|
34
|
|
|
FilterManager $filterManager |
|
35
|
|
|
) { |
|
36
|
|
|
parent::__construct($filesystem, $requestContext, $webRootDir, $cachePrefix); |
|
37
|
|
|
|
|
38
|
|
|
$this->filterManager = $filterManager; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritDoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function getFilePath($path, $filter) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->webRoot.'/'.$this->getFileUrl($this->replaceImageFileExtension($path, $filter), $filter); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritDoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function getFileUrl($path, $filter) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->cachePrefix.'/'.$filter.'/'.ltrim($this->replaceImageFileExtension($path, $filter), '/'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Replaces original image file extension to conversion format extension |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $path |
|
61
|
|
|
* @param string $filter |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function replaceImageFileExtension($path, $filter) |
|
64
|
|
|
{ |
|
65
|
|
|
$newExtension = $this->getImageFormat($filter); |
|
66
|
|
|
if (null !== $newExtension) { |
|
67
|
|
|
$path = preg_replace('/\.[^.]+$/', '.'.$newExtension, $path); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $path; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns image conversion format |
|
75
|
|
|
* |
|
76
|
|
|
* @param $filterName |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function getImageFormat($filterName) |
|
79
|
|
|
{ |
|
80
|
|
|
$filterConfig = $this->filterManager->getFilterConfiguration(); |
|
81
|
|
|
$currentFilterConfig = $filterConfig->get($filterName); |
|
82
|
|
|
|
|
83
|
|
|
return $currentFilterConfig['format']; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|