-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathAbstractFilesystemResolver.php
171 lines (144 loc) · 4.23 KB
/
AbstractFilesystemResolver.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Imagine\Cache\Resolver;
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Cache\CacheManagerAwareInterface;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
abstract class AbstractFilesystemResolver implements ResolverInterface, CacheManagerAwareInterface
{
/**
* @var Filesystem
*/
protected $filesystem;
/**
* @var string
*/
protected $basePath = '';
/**
* @var CacheManager
*/
protected $cacheManager;
/**
* @var int
*/
protected $folderPermissions = 0777;
/**
* @var Request
*/
private $request;
/**
* Constructs a filesystem based cache resolver.
*/
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function setRequest(?Request $request = null)
{
$this->request = $request;
}
public function setCacheManager(CacheManager $cacheManager)
{
$this->cacheManager = $cacheManager;
}
/**
* Set the base path to.
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
/**
* @param int $folderPermissions
*/
public function setFolderPermissions($folderPermissions)
{
$this->folderPermissions = $folderPermissions;
}
public function isStored($path, $filter)
{
return file_exists($this->getFilePath($path, $filter));
}
public function store(BinaryInterface $binary, $path, $filter)
{
$filePath = $this->getFilePath($path, $filter);
$dir = pathinfo($filePath, PATHINFO_DIRNAME);
$this->makeFolder($dir);
file_put_contents($filePath, $binary->getContent());
}
public function remove(array $paths, array $filters)
{
if (empty($paths) && empty($filters)) {
return;
}
// TODO: this logic has to be refactored.
[$rootCachePath] = explode(current($filters), $this->getFilePath('whateverpath', current($filters)));
if (empty($paths)) {
$filtersCachePaths = [];
foreach ($filters as $filter) {
$filterCachePath = $rootCachePath.$filter;
if (is_dir($filterCachePath)) {
$filtersCachePaths[] = $filterCachePath;
}
}
$this->filesystem->remove($filtersCachePaths);
return;
}
foreach ($paths as $path) {
foreach ($filters as $filter) {
$this->filesystem->remove($this->getFilePath($path, $filter));
}
}
}
/**
* @throws \LogicException
*
* @return Request
*/
protected function getRequest()
{
if (false === $this->request) {
throw new \LogicException('The request was not injected, inject it before using resolver.');
}
return $this->request;
}
/**
* @param string $dir
*
* @throws \RuntimeException
*/
protected function makeFolder($dir)
{
if (!is_dir($dir)) {
$parent = \dirname($dir);
try {
$this->makeFolder($parent);
$this->filesystem->mkdir($dir);
$this->filesystem->chmod($dir, $this->folderPermissions);
} catch (IOException $e) {
throw new \RuntimeException(\sprintf('Could not create directory %s', $dir), 0, $e);
}
}
}
/**
* Return the local filepath.
*
* @param string $path The resource path to convert
* @param string $filter The name of the imagine filter
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*
* @return string
*/
abstract protected function getFilePath($path, $filter);
}