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\Utility\Path; |
13
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Imagine\Cache\Helper\PathHelper; |
15
|
|
|
|
16
|
|
|
class PathResolver implements PathResolverInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $webRoot; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $cachePrefix; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $cacheRoot; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
$webRootDir, |
35
|
|
|
$cachePrefix = 'media/cache' |
36
|
|
|
) { |
37
|
|
|
$this->webRoot = rtrim(str_replace('//', '/', $webRootDir), '/'); |
38
|
|
|
$this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); |
39
|
|
|
$this->cacheRoot = $this->webRoot.'/'.$this->cachePrefix; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function getFilePath($path, $filter): string |
46
|
|
|
{ |
47
|
|
|
return $this->webRoot.'/'.$this->getFullPath($path, $filter); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getFileUrl($path, $filter): string |
54
|
|
|
{ |
55
|
|
|
return PathHelper::filePathToUrlPath($this->getFullPath($path, $filter)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getCacheRoot(): string |
62
|
|
|
{ |
63
|
|
|
return $this->cacheRoot; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
private function getFullPath($path, $filter): string |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
// crude way of sanitizing URL scheme ("protocol") part |
69
|
|
|
$path = str_replace('://', '---', $path); |
70
|
|
|
|
71
|
|
|
return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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.