1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Partnermarketing\FileSystemBundle\Adapter; |
4
|
|
|
|
5
|
|
|
use Partnermarketing\FileSystemBundle\Exception\FileDoesNotExistException; |
6
|
|
|
use Partnermarketing\FileSystemBundle\ServerFileSystem\ServerFileSystem; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* LocalStorage specific file system adapter |
10
|
|
|
*/ |
11
|
|
|
class LocalStorage implements AdapterInterface |
12
|
|
|
{ |
13
|
|
|
private $service; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Contains a preceding slash, and no trailing slash. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $absolutePath; |
21
|
|
|
|
22
|
|
|
private $webUrl; |
23
|
|
|
|
24
|
|
|
private $localTmpDir; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor for LocalStorage adapter |
28
|
|
|
*/ |
29
|
5 |
|
public function __construct($service, array $parameters, $localTmpDir) |
30
|
|
|
{ |
31
|
5 |
|
$this->service = $service; |
32
|
|
|
|
33
|
5 |
|
$absolutePathNormalised = '/' . trim($parameters['path'], '/'); |
34
|
5 |
|
if (is_dir($absolutePathNormalised) && realpath($absolutePathNormalised)) { |
35
|
5 |
|
$this->absolutePath = realpath($absolutePathNormalised); |
36
|
5 |
|
} else { |
37
|
|
|
throw new FileDoesNotExistException($absolutePathNormalised); |
38
|
|
|
} |
39
|
|
|
|
40
|
5 |
|
$this->webUrl = trim($parameters['url'], '/'); |
41
|
|
|
|
42
|
5 |
|
$this->localTmpDir = $localTmpDir; |
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function read($path) |
49
|
|
|
{ |
50
|
|
|
// In this method only, $path can be absolute, for reading files from |
51
|
|
|
// outside of the file system directory, e.g. uploads, fixtures. |
52
|
1 |
|
if (file_exists($path)) { |
53
|
|
|
$fullPath = $path; |
54
|
|
|
} else { |
55
|
1 |
|
$path = $this->pathOrUrlToPath($path); |
56
|
1 |
|
$fullPath = $this->absolutePath . '/' . $path; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return (string) file_get_contents($fullPath); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function write($path, $source) |
66
|
|
|
{ |
67
|
|
|
$path = $this->pathOrUrlToPath($path); |
68
|
|
|
|
69
|
|
|
return $this->writeContent($path, $this->read($source)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function writeContent($path, $content) |
76
|
|
|
{ |
77
|
|
|
$path = $this->pathOrUrlToPath($path); |
78
|
|
|
|
79
|
|
|
$this->service->dumpFile($this->absolutePath.'/'.$path, $content); |
80
|
|
|
|
81
|
|
|
return $path; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function rename($sourcePath, $targetPath) |
88
|
|
|
{ |
89
|
|
|
$sourcePath = $this->pathOrUrlToPath($sourcePath); |
90
|
|
|
$targetPath = $this->pathOrUrlToPath($targetPath); |
91
|
|
|
|
92
|
|
|
$this->service->rename($this->absolutePath.'/'.$sourcePath, $this->absolutePath.'/'.$targetPath); |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
|
|
public function delete($path) |
101
|
|
|
{ |
102
|
|
|
$path = $this->pathOrUrlToPath($path); |
103
|
|
|
|
104
|
|
|
$resp = $this->service->remove($this->absolutePath.'/'.$path); |
|
|
|
|
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function getFiles($directory = "") |
113
|
|
|
{ |
114
|
1 |
|
$directory = $this->pathOrUrlToPath($directory); |
115
|
|
|
|
116
|
1 |
|
$files = ServerFileSystem::getFilesInDirectory($this->absolutePath . '/' . $directory); |
117
|
|
|
|
118
|
1 |
|
return array_map(function ($file) { |
119
|
1 |
|
return str_replace($this->absolutePath . '/', '', $file); |
120
|
1 |
|
}, $files); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function mkdir($dir) |
124
|
|
|
{ |
125
|
|
|
$dir = $this->pathOrUrlToPath($dir); |
126
|
|
|
|
127
|
|
|
$this->service->mkdir($this->absolutePath.'/'.$dir); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function copy($originFile, $targetFile) |
131
|
|
|
{ |
132
|
|
|
$originFile = $this->pathOrUrlToPath($originFile); |
133
|
|
|
$targetFile = $this->pathOrUrlToPath($targetFile); |
134
|
|
|
|
135
|
|
|
$this->service->copy($this->absolutePath.'/'.$originFile, $this->absolutePath.'/'.$targetFile); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritDoc} |
140
|
|
|
*/ |
141
|
|
|
public function copyFiles($sourceDir, $targetDir) |
142
|
|
|
{ |
143
|
|
|
$sourceDir = $this->pathOrUrlToPath($sourceDir); |
144
|
|
|
$targetDir = $this->pathOrUrlToPath($targetDir); |
145
|
|
|
|
146
|
|
|
if ($sourceDir === $targetDir) { |
147
|
|
|
throw new \RuntimeException(sprintf('Failed to copy files from %s to %s.', $sourceDir, $targetDir)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// loop trough all files within the folder |
151
|
|
|
if ($handle = opendir($this->absolutePath.'/'.$sourceDir)) { |
152
|
|
|
while (false !== ($entry = readdir($handle))) { |
153
|
|
|
if ($entry != "." && $entry != "..") { |
154
|
|
|
if (!empty($sourceDir)) { |
155
|
|
|
$this->copy($sourceDir.'/'.$entry, $targetDir.'/'.$entry); |
156
|
|
|
} else { |
157
|
|
|
$this->copy($entry, $targetDir.'/'.$entry); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
closedir($handle); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritDoc} |
169
|
|
|
*/ |
170
|
|
|
public function exists($path) |
171
|
|
|
{ |
172
|
|
|
$path = $this->pathOrUrlToPath($path); |
173
|
|
|
|
174
|
|
|
return $this->service->exists($this->absolutePath.'/'.$path); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritDoc} |
179
|
|
|
*/ |
180
|
|
|
public function isDirectory($path) |
181
|
|
|
{ |
182
|
|
|
$path = $this->pathOrUrlToPath($path); |
183
|
|
|
|
184
|
|
|
if (is_dir($this->absolutePath.'/'.$path) && !is_link($this->absolutePath.'/'.$path)) { |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritDoc} |
193
|
|
|
*/ |
194
|
|
|
public function getURL($path) |
195
|
|
|
{ |
196
|
|
|
$path = $this->pathOrUrlToPath($path); |
197
|
|
|
|
198
|
|
|
return $this->webUrl.'/'.$path; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritDoc} |
203
|
|
|
*/ |
204
|
1 |
|
public function getFileSize($path) |
205
|
|
|
{ |
206
|
1 |
|
$fullPath = $this->absolutePath . '/' . $this->pathOrUrlToPath($path); |
207
|
|
|
|
208
|
1 |
|
return filesize($fullPath); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritDoc} |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
public function copyToLocalTemporaryFile($path) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$content = $this->read($path); |
217
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
218
|
|
|
$target = tempnam($this->localTmpDir, null) . '.' . $extension; |
219
|
|
|
|
220
|
|
|
file_put_contents($target, $content); |
221
|
|
|
|
222
|
|
|
return $target; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritDoc} |
227
|
|
|
*/ |
228
|
|
|
public function getService() { |
229
|
|
|
return $this->service; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $input |
234
|
|
|
* @return array The path to the file inside the file system. Does not contain a preceding |
235
|
|
|
* slash. |
236
|
|
|
*/ |
237
|
3 |
|
private function pathOrUrlToPath($input) |
238
|
|
|
{ |
239
|
3 |
|
if (empty($input)) { |
240
|
1 |
|
return ''; |
241
|
|
|
} |
242
|
|
|
|
243
|
2 |
|
if (strpos($input, $this->webUrl) === 0) { |
244
|
|
|
$input = str_replace($this->webUrl, '', $input); |
245
|
2 |
|
} elseif (strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0) { |
246
|
|
|
$input = parse_url($input, PHP_URL_PATH); |
247
|
|
|
} |
248
|
|
|
|
249
|
2 |
|
return trim($input, '/'); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.