|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
6
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
7
|
|
|
|
|
8
|
|
|
//implements ResolverInterface |
|
9
|
|
|
class ImagineWebCacheResolver extends \Liip\ImagineBundle\Imagine\Cache\Resolver\WebPathResolver |
|
10
|
|
|
{ |
|
11
|
|
|
public function __construct( |
|
12
|
|
|
Filesystem $filesystem, |
|
13
|
|
|
RequestContext $requestContext, |
|
14
|
|
|
$webRootDir, |
|
15
|
|
|
$cachePrefix = 'media' |
|
16
|
|
|
) { |
|
17
|
|
|
$this->filesystem = $filesystem; |
|
18
|
|
|
$this->requestContext = $requestContext; |
|
19
|
|
|
$this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); |
|
20
|
|
|
$this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); |
|
21
|
|
|
$this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function resolve($path, $filter) |
|
25
|
|
|
{ |
|
26
|
|
|
$path = ltrim($path, '/media'); |
|
27
|
|
|
|
|
28
|
|
|
return '/'.$this->getFileUrl($path, $filter); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function getFilePath($path, $filter) |
|
32
|
|
|
{ |
|
33
|
|
|
$path = ltrim($path, '/media'); |
|
34
|
|
|
|
|
35
|
|
|
return $this->webRoot.'/'.$this->getFullPath($path, $filter); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function getFullPath($path, $filter) |
|
39
|
|
|
{ |
|
40
|
|
|
// crude way of sanitizing URL scheme ("protocol") part |
|
41
|
|
|
$path = str_replace('://', '---', $path); |
|
42
|
|
|
|
|
43
|
|
|
return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|