|
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\Imagine\Cache\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Binary\BinaryInterface; |
|
15
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
16
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManagerAwareInterface; |
|
17
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
|
|
21
|
|
|
abstract class AbstractFilesystemResolver implements ResolverInterface, CacheManagerAwareInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Filesystem |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $filesystem; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $basePath = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var CacheManager |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $cacheManager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $folderPermissions = 0777; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var Request |
|
44
|
|
|
*/ |
|
45
|
|
|
private $request; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constructs a filesystem based cache resolver. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(Filesystem $filesystem) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->filesystem = $filesystem; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Request $request |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setRequest(Request $request = null) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->request = $request; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function setCacheManager(CacheManager $cacheManager) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->cacheManager = $cacheManager; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set the base path to. |
|
70
|
|
|
* |
|
71
|
|
|
* @param $basePath |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setBasePath($basePath) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->basePath = $basePath; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param int $folderPermissions |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setFolderPermissions($folderPermissions) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->folderPermissions = $folderPermissions; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function isStored($path, $filter) |
|
90
|
|
|
{ |
|
91
|
|
|
return file_exists($this->getFilePath($path, $filter)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function store(BinaryInterface $binary, $path, $filter) |
|
98
|
|
|
{ |
|
99
|
|
|
$filePath = $this->getFilePath($path, $filter); |
|
100
|
|
|
|
|
101
|
|
|
$dir = pathinfo($filePath, PATHINFO_DIRNAME); |
|
102
|
|
|
|
|
103
|
|
|
$this->makeFolder($dir); |
|
104
|
|
|
|
|
105
|
|
|
file_put_contents($filePath, $binary->getContent()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function remove(array $paths, array $filters) |
|
112
|
|
|
{ |
|
113
|
|
|
if (empty($paths) && empty($filters)) { |
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// TODO: this logic has to be refactored. |
|
118
|
|
|
[$rootCachePath] = explode(current($filters), $this->getFilePath('whateverpath', current($filters))); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
if (empty($paths)) { |
|
121
|
|
|
$filtersCachePaths = []; |
|
122
|
|
|
foreach ($filters as $filter) { |
|
123
|
|
|
$filterCachePath = $rootCachePath.$filter; |
|
124
|
|
|
if (is_dir($filterCachePath)) { |
|
125
|
|
|
$filtersCachePaths[] = $filterCachePath; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$this->filesystem->remove($filtersCachePaths); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
foreach ($paths as $path) { |
|
135
|
|
|
foreach ($filters as $filter) { |
|
136
|
|
|
$this->filesystem->remove($this->getFilePath($path, $filter)); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @throws \LogicException |
|
143
|
|
|
* |
|
144
|
|
|
* @return Request |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getRequest() |
|
147
|
|
|
{ |
|
148
|
|
|
if (false === $this->request) { |
|
149
|
|
|
throw new \LogicException('The request was not injected, inject it before using resolver.'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this->request; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $dir |
|
157
|
|
|
* |
|
158
|
|
|
* @throws \RuntimeException |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function makeFolder($dir) |
|
161
|
|
|
{ |
|
162
|
|
|
if (!is_dir($dir)) { |
|
163
|
|
|
$parent = \dirname($dir); |
|
164
|
|
|
try { |
|
165
|
|
|
$this->makeFolder($parent); |
|
166
|
|
|
$this->filesystem->mkdir($dir); |
|
167
|
|
|
$this->filesystem->chmod($dir, $this->folderPermissions); |
|
168
|
|
|
} catch (IOException $e) { |
|
169
|
|
|
throw new \RuntimeException(sprintf('Could not create directory %s', $dir), 0, $e); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Return the local filepath. |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $path The resource path to convert |
|
178
|
|
|
* @param string $filter The name of the imagine filter |
|
179
|
|
|
* |
|
180
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
abstract protected function getFilePath($path, $filter); |
|
185
|
|
|
} |
|
186
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.