-
Notifications
You must be signed in to change notification settings - Fork 14
/
WebPathResolver.php
85 lines (70 loc) · 2.24 KB
/
WebPathResolver.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
<?php
namespace Kunstmaan\MediaBundle\Helper\Imagine;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\RequestContext;
class WebPathResolver extends \Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver
{
/**
* @var FilterConfiguration
*/
protected $filterConfig;
/**
* @param string $webRootDir
* @param string $cachePrefix
*/
public function __construct(Filesystem $filesystem, RequestContext $requestContext, $webRootDir, $cachePrefix, FilterConfiguration $filterConfig)
{
parent::__construct($filesystem, $requestContext, $webRootDir, $cachePrefix);
$this->filterConfig = $filterConfig;
}
/**
* {@inheritdoc}
*/
protected function getFileUrl($path, $filter)
{
$filterConf = $this->filterConfig->get($filter);
$path = $this->changeFileExtension($path, $filterConf['format']);
return parent::getFileUrl($path, $filter);
}
protected function getFilePath($path, $filter)
{
$filterConf = $this->filterConfig->get($filter);
$path = $this->changeFileExtension($path, $filterConf['format']);
$fullPath = $this->getFullPath($path, $filter);
return $this->webRoot . '/' . $fullPath;
}
/**
* {@inheritdoc}
*
* @return string
*/
public function resolve($path, $filter)
{
return sprintf('%s/%s', $this->getBaseUrl(), $this->getFileUrl($path, $filter));
}
/**
* @param string $path
* @param string $format
*
* @return string
*/
private function changeFileExtension($path, $format)
{
if (!$format) {
return $path;
}
$info = pathinfo($path);
$path = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '.' . $format;
return $path;
}
/**
* Copy from \Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver::getFullPath
*/
private function getFullPath($path, $filter)
{
// crude way of sanitizing URL scheme ("protocol") part
$path = str_replace('://', '---', $path);
return $this->cachePrefix . '/' . $filter . '/' . ltrim($path, '/');
}
}