1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Helper\Imagine; |
4
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Routing\RequestContext; |
8
|
|
|
|
9
|
|
|
class WebPathResolver extends \Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var FilterConfiguration |
13
|
|
|
*/ |
14
|
|
|
protected $filterConfig; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param Filesystem $filesystem |
18
|
|
|
* @param RequestContext $requestContext |
19
|
|
|
* @param string $webRootDir |
20
|
|
|
* @param string $cachePrefix |
21
|
|
|
* @param FilterConfiguration $filterConfig |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Filesystem $filesystem, RequestContext $requestContext, $webRootDir, $cachePrefix = 'media/cache', FilterConfiguration $filterConfig) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($filesystem, $requestContext, $webRootDir, $cachePrefix); |
26
|
|
|
|
27
|
|
|
$this->filterConfig = $filterConfig; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function getFileUrl($path, $filter) |
34
|
|
|
{ |
35
|
|
|
$filterConf = $this->filterConfig->get($filter); |
36
|
|
|
$path = $this->changeFileExtension($path, $filterConf['format']); |
37
|
|
|
|
38
|
|
|
return parent::getFileUrl($path, $filter); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function getFilePath($path, $filter) |
42
|
|
|
{ |
43
|
|
|
$filterConf = $this->filterConfig->get($filter); |
44
|
|
|
$path = $this->changeFileExtension($path, $filterConf['format']); |
45
|
|
|
$fullPath = $this->getFullPath($path, $filter); |
46
|
|
|
|
47
|
|
|
return $this->webRoot.'/'.$fullPath; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function resolve($path, $filter) |
54
|
|
|
{ |
55
|
|
|
return sprintf('%s/%s', $this->getBaseUrl(), $this->getFileUrl($path, $filter)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $path |
60
|
|
|
* @param string $format |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
private function changeFileExtension($path, $format) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (!$format) { |
67
|
|
|
return $path; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$info = pathinfo($path); |
71
|
|
|
$path = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '.' . $format; |
72
|
|
|
|
73
|
|
|
return $path; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Copy from \Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver::getFullPath |
78
|
|
|
*/ |
79
|
|
|
private function getFullPath($path, $filter) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
// crude way of sanitizing URL scheme ("protocol") part |
82
|
|
|
$path = str_replace('://', '---', $path); |
83
|
|
|
|
84
|
|
|
return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.