@@ -45,7 +45,8 @@ discard block |
||
45 | 45 | */ |
46 | 46 | public function __destruct() |
47 | 47 | { |
48 | - foreach ($this->destructFiles as $filename) { |
|
48 | + foreach ($this->destructFiles as $filename) |
|
49 | + { |
|
49 | 50 | $this->delete($filename); |
50 | 51 | } |
51 | 52 | } |
@@ -60,30 +61,36 @@ discard block |
||
60 | 61 | int $mode = null, |
61 | 62 | bool $recursivePermissions = true |
62 | 63 | ): bool { |
63 | - if (empty($mode)) { |
|
64 | + if (empty($mode)) |
|
65 | + { |
|
64 | 66 | $mode = self::DEFAULT_FILE_MODE; |
65 | 67 | } |
66 | 68 | |
67 | 69 | //Directories always executable |
68 | 70 | $mode = $mode | 0111; |
69 | - if (is_dir($directory)) { |
|
71 | + if (is_dir($directory)) |
|
72 | + { |
|
70 | 73 | //Exists :( |
71 | 74 | return $this->setPermissions($directory, $mode); |
72 | 75 | } |
73 | 76 | |
74 | - if (!$recursivePermissions) { |
|
77 | + if (!$recursivePermissions) |
|
78 | + { |
|
75 | 79 | return mkdir($directory, $mode, true); |
76 | 80 | } |
77 | 81 | |
78 | 82 | $directoryChain = [basename($directory)]; |
79 | 83 | |
80 | 84 | $baseDirectory = $directory; |
81 | - while (!is_dir($baseDirectory = dirname($baseDirectory))) { |
|
85 | + while (!is_dir($baseDirectory = dirname($baseDirectory))) |
|
86 | + { |
|
82 | 87 | $directoryChain[] = basename($baseDirectory); |
83 | 88 | } |
84 | 89 | |
85 | - foreach (array_reverse($directoryChain) as $directory) { |
|
86 | - if (!mkdir($baseDirectory = "{$baseDirectory}/{$directory}")) { |
|
90 | + foreach (array_reverse($directoryChain) as $directory) |
|
91 | + { |
|
92 | + if (!mkdir($baseDirectory = "{$baseDirectory}/{$directory}")) |
|
93 | + { |
|
87 | 94 | return false; |
88 | 95 | } |
89 | 96 | |
@@ -98,7 +105,8 @@ discard block |
||
98 | 105 | */ |
99 | 106 | public function read(string $filename): string |
100 | 107 | { |
101 | - if (!$this->exists($filename)) { |
|
108 | + if (!$this->exists($filename)) |
|
109 | + { |
|
102 | 110 | throw new FileNotFoundException($filename); |
103 | 111 | } |
104 | 112 | |
@@ -119,12 +127,15 @@ discard block |
||
119 | 127 | ): bool { |
120 | 128 | $mode = $mode ?? self::DEFAULT_FILE_MODE; |
121 | 129 | |
122 | - try { |
|
123 | - if ($ensureDirectory) { |
|
130 | + try |
|
131 | + { |
|
132 | + if ($ensureDirectory) |
|
133 | + { |
|
124 | 134 | $this->ensureDirectory(dirname($filename), $mode); |
125 | 135 | } |
126 | 136 | |
127 | - if ($this->exists($filename)) { |
|
137 | + if ($this->exists($filename)) |
|
138 | + { |
|
128 | 139 | //Forcing mode for existed file |
129 | 140 | $this->setPermissions($filename, $mode); |
130 | 141 | } |
@@ -135,11 +146,14 @@ discard block |
||
135 | 146 | $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
136 | 147 | ); |
137 | 148 | |
138 | - if ($result !== false) { |
|
149 | + if ($result !== false) |
|
150 | + { |
|
139 | 151 | //Forcing mode after file creation |
140 | 152 | $this->setPermissions($filename, $mode); |
141 | 153 | } |
142 | - } catch (\Exception $e) { |
|
154 | + } |
|
155 | + catch (\Exception $e) |
|
156 | + { |
|
143 | 157 | throw new WriteErrorException($e->getMessage(), $e->getCode(), $e); |
144 | 158 | } |
145 | 159 | |
@@ -163,7 +177,8 @@ discard block |
||
163 | 177 | */ |
164 | 178 | public function delete(string $filename) |
165 | 179 | { |
166 | - if ($this->exists($filename)) { |
|
180 | + if ($this->exists($filename)) |
|
181 | + { |
|
167 | 182 | $result = unlink($filename); |
168 | 183 | |
169 | 184 | //Wiping out changes in local file cache |
@@ -187,7 +202,8 @@ discard block |
||
187 | 202 | */ |
188 | 203 | public function deleteDirectory(string $directory, bool $contentOnly = false): void |
189 | 204 | { |
190 | - if (!$this->isDirectory($directory)) { |
|
205 | + if (!$this->isDirectory($directory)) |
|
206 | + { |
|
191 | 207 | throw new FilesException("Undefined or invalid directory {$directory}"); |
192 | 208 | } |
193 | 209 | |
@@ -196,15 +212,20 @@ discard block |
||
196 | 212 | \RecursiveIteratorIterator::CHILD_FIRST |
197 | 213 | ); |
198 | 214 | |
199 | - foreach ($files as $file) { |
|
200 | - if ($file->isDir()) { |
|
215 | + foreach ($files as $file) |
|
216 | + { |
|
217 | + if ($file->isDir()) |
|
218 | + { |
|
201 | 219 | rmdir($file->getRealPath()); |
202 | - } else { |
|
220 | + } |
|
221 | + else |
|
222 | + { |
|
203 | 223 | $this->delete($file->getRealPath()); |
204 | 224 | } |
205 | 225 | } |
206 | 226 | |
207 | - if (!$contentOnly) { |
|
227 | + if (!$contentOnly) |
|
228 | + { |
|
208 | 229 | rmdir($directory); |
209 | 230 | } |
210 | 231 | } |
@@ -214,7 +235,8 @@ discard block |
||
214 | 235 | */ |
215 | 236 | public function move(string $filename, string $destination): bool |
216 | 237 | { |
217 | - if (!$this->exists($filename)) { |
|
238 | + if (!$this->exists($filename)) |
|
239 | + { |
|
218 | 240 | throw new FileNotFoundException($filename); |
219 | 241 | } |
220 | 242 | |
@@ -226,7 +248,8 @@ discard block |
||
226 | 248 | */ |
227 | 249 | public function copy(string $filename, string $destination): bool |
228 | 250 | { |
229 | - if (!$this->exists($filename)) { |
|
251 | + if (!$this->exists($filename)) |
|
252 | + { |
|
230 | 253 | throw new FileNotFoundException($filename); |
231 | 254 | } |
232 | 255 | |
@@ -238,7 +261,8 @@ discard block |
||
238 | 261 | */ |
239 | 262 | public function touch(string $filename, int $mode = null): bool |
240 | 263 | { |
241 | - if (!touch($filename)) { |
|
264 | + if (!touch($filename)) |
|
265 | + { |
|
242 | 266 | return false; |
243 | 267 | } |
244 | 268 | |
@@ -258,7 +282,8 @@ discard block |
||
258 | 282 | */ |
259 | 283 | public function size(string $filename): int |
260 | 284 | { |
261 | - if (!$this->exists($filename)) { |
|
285 | + if (!$this->exists($filename)) |
|
286 | + { |
|
262 | 287 | throw new FileNotFoundException($filename); |
263 | 288 | } |
264 | 289 | |
@@ -278,7 +303,8 @@ discard block |
||
278 | 303 | */ |
279 | 304 | public function md5(string $filename): string |
280 | 305 | { |
281 | - if (!$this->exists($filename)) { |
|
306 | + if (!$this->exists($filename)) |
|
307 | + { |
|
282 | 308 | throw new FileNotFoundException($filename); |
283 | 309 | } |
284 | 310 | |
@@ -290,7 +316,8 @@ discard block |
||
290 | 316 | */ |
291 | 317 | public function time(string $filename): int |
292 | 318 | { |
293 | - if (!$this->exists($filename)) { |
|
319 | + if (!$this->exists($filename)) |
|
320 | + { |
|
294 | 321 | throw new FileNotFoundException($filename); |
295 | 322 | } |
296 | 323 | |
@@ -318,7 +345,8 @@ discard block |
||
318 | 345 | */ |
319 | 346 | public function getPermissions(string $filename): int |
320 | 347 | { |
321 | - if (!$this->exists($filename)) { |
|
348 | + if (!$this->exists($filename)) |
|
349 | + { |
|
322 | 350 | throw new FileNotFoundException($filename); |
323 | 351 | } |
324 | 352 | |
@@ -330,7 +358,8 @@ discard block |
||
330 | 358 | */ |
331 | 359 | public function setPermissions(string $filename, int $mode) |
332 | 360 | { |
333 | - if (is_dir($filename)) { |
|
361 | + if (is_dir($filename)) |
|
362 | + { |
|
334 | 363 | //Directories must always be executable (i.e. 664 for dir => 775) |
335 | 364 | $mode |= 0111; |
336 | 365 | } |
@@ -344,8 +373,10 @@ discard block |
||
344 | 373 | public function getFiles(string $location, string $pattern = null): array |
345 | 374 | { |
346 | 375 | $result = []; |
347 | - foreach ($this->filesIterator($location, $pattern) as $filename) { |
|
348 | - if ($this->isDirectory($filename->getPathname())) { |
|
376 | + foreach ($this->filesIterator($location, $pattern) as $filename) |
|
377 | + { |
|
378 | + if ($this->isDirectory($filename->getPathname())) |
|
379 | + { |
|
349 | 380 | $result = array_merge($result, $this->getFiles($filename . DIRECTORY_SEPARATOR)); |
350 | 381 | |
351 | 382 | continue; |
@@ -362,13 +393,15 @@ discard block |
||
362 | 393 | */ |
363 | 394 | public function tempFilename(string $extension = '', string $location = null): string |
364 | 395 | { |
365 | - if (empty($location)) { |
|
396 | + if (empty($location)) |
|
397 | + { |
|
366 | 398 | $location = sys_get_temp_dir(); |
367 | 399 | } |
368 | 400 | |
369 | 401 | $filename = tempnam($location, 'spiral'); |
370 | 402 | |
371 | - if (!empty($extension)) { |
|
403 | + if (!empty($extension)) |
|
404 | + { |
|
372 | 405 | //I should find more original way of doing that |
373 | 406 | rename($filename, $filename = "{$filename}.{$extension}"); |
374 | 407 | $this->destructFiles[] = $filename; |
@@ -402,15 +435,20 @@ discard block |
||
402 | 435 | $path = explode('/', $path); |
403 | 436 | $relative = $path; |
404 | 437 | |
405 | - foreach ($from as $depth => $dir) { |
|
438 | + foreach ($from as $depth => $dir) |
|
439 | + { |
|
406 | 440 | //Find first non-matching dir |
407 | - if ($dir === $path[$depth]) { |
|
441 | + if ($dir === $path[$depth]) |
|
442 | + { |
|
408 | 443 | //Ignore this directory |
409 | 444 | array_shift($relative); |
410 | - } else { |
|
445 | + } |
|
446 | + else |
|
447 | + { |
|
411 | 448 | //Get number of remaining dirs to $from |
412 | 449 | $remaining = count($from) - $depth; |
413 | - if ($remaining > 1) { |
|
450 | + if ($remaining > 1) |
|
451 | + { |
|
414 | 452 | //Add traversals up to first matching directory |
415 | 453 | $padLength = (count($relative) + $remaining - 1) * -1; |
416 | 454 | $relative = array_pad($relative, $padLength, '..'); |
@@ -137,7 +137,8 @@ discard block |
||
137 | 137 | $this->assertTrue($files->exists($directory)); |
138 | 138 | $this->assertTrue($files->isDirectory($directory)); |
139 | 139 | |
140 | - foreach ($filenames as $filename) { |
|
140 | + foreach ($filenames as $filename) |
|
141 | + { |
|
141 | 142 | $this->assertFalse($files->exists($filename)); |
142 | 143 | $files->write($filename, 'random-data'); |
143 | 144 | $this->assertTrue($files->exists($filename)); |
@@ -151,7 +152,8 @@ discard block |
||
151 | 152 | $this->assertFalse($files->exists($directory)); |
152 | 153 | $this->assertFalse($files->isDirectory($directory)); |
153 | 154 | |
154 | - foreach ($filenames as $filename) { |
|
155 | + foreach ($filenames as $filename) |
|
156 | + { |
|
155 | 157 | $this->assertFalse($files->exists($filename)); |
156 | 158 | } |
157 | 159 | } |
@@ -182,7 +184,8 @@ discard block |
||
182 | 184 | $this->assertTrue($files->exists($directory)); |
183 | 185 | $this->assertTrue($files->isDirectory($directory)); |
184 | 186 | |
185 | - foreach ($filenames as $filename) { |
|
187 | + foreach ($filenames as $filename) |
|
188 | + { |
|
186 | 189 | $this->assertFalse($files->exists($filename)); |
187 | 190 | $files->write($filename, 'random-data'); |
188 | 191 | $this->assertTrue($files->exists($filename)); |
@@ -196,7 +199,8 @@ discard block |
||
196 | 199 | $this->assertFalse($files->exists($directory)); |
197 | 200 | $this->assertFalse($files->isDirectory($directory)); |
198 | 201 | |
199 | - foreach ($filenames as $filename) { |
|
202 | + foreach ($filenames as $filename) |
|
203 | + { |
|
200 | 204 | $this->assertFalse($files->exists($filename)); |
201 | 205 | } |
202 | 206 | } |