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