@@ -36,7 +36,8 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | public function __destruct() |
| 38 | 38 | { |
| 39 | - foreach ($this->destructFiles as $filename) { |
|
| 39 | + foreach ($this->destructFiles as $filename) |
|
| 40 | + { |
|
| 40 | 41 | $this->delete($filename); |
| 41 | 42 | } |
| 42 | 43 | } |
@@ -49,30 +50,36 @@ discard block |
||
| 49 | 50 | int $mode = null, |
| 50 | 51 | bool $recursivePermissions = true |
| 51 | 52 | ): bool { |
| 52 | - if (empty($mode)) { |
|
| 53 | + if (empty($mode)) |
|
| 54 | + { |
|
| 53 | 55 | $mode = self::DEFAULT_FILE_MODE; |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | 58 | //Directories always executable |
| 57 | 59 | $mode |= 0o111; |
| 58 | - if (\is_dir($directory)) { |
|
| 60 | + if (\is_dir($directory)) |
|
| 61 | + { |
|
| 59 | 62 | //Exists :( |
| 60 | 63 | return $this->setPermissions($directory, $mode); |
| 61 | 64 | } |
| 62 | 65 | |
| 63 | - if (!$recursivePermissions) { |
|
| 66 | + if (!$recursivePermissions) |
|
| 67 | + { |
|
| 64 | 68 | return \mkdir($directory, $mode, true); |
| 65 | 69 | } |
| 66 | 70 | |
| 67 | 71 | $directoryChain = [\basename($directory)]; |
| 68 | 72 | |
| 69 | 73 | $baseDirectory = $directory; |
| 70 | - while (!\is_dir($baseDirectory = \dirname($baseDirectory))) { |
|
| 74 | + while (!\is_dir($baseDirectory = \dirname($baseDirectory))) |
|
| 75 | + { |
|
| 71 | 76 | $directoryChain[] = \basename($baseDirectory); |
| 72 | 77 | } |
| 73 | 78 | |
| 74 | - foreach (\array_reverse($directoryChain) as $dir) { |
|
| 75 | - if (!mkdir($baseDirectory = \sprintf('%s/%s', $baseDirectory, $dir))) { |
|
| 79 | + foreach (\array_reverse($directoryChain) as $dir) |
|
| 80 | + { |
|
| 81 | + if (!mkdir($baseDirectory = \sprintf('%s/%s', $baseDirectory, $dir))) |
|
| 82 | + { |
|
| 76 | 83 | return false; |
| 77 | 84 | } |
| 78 | 85 | |
@@ -84,7 +91,8 @@ discard block |
||
| 84 | 91 | |
| 85 | 92 | public function read(string $filename): string |
| 86 | 93 | { |
| 87 | - if (!$this->exists($filename)) { |
|
| 94 | + if (!$this->exists($filename)) |
|
| 95 | + { |
|
| 88 | 96 | throw new FileNotFoundException($filename); |
| 89 | 97 | } |
| 90 | 98 | |
@@ -103,12 +111,15 @@ discard block |
||
| 103 | 111 | ): bool { |
| 104 | 112 | $mode ??= self::DEFAULT_FILE_MODE; |
| 105 | 113 | |
| 106 | - try { |
|
| 107 | - if ($ensureDirectory) { |
|
| 114 | + try |
|
| 115 | + { |
|
| 116 | + if ($ensureDirectory) |
|
| 117 | + { |
|
| 108 | 118 | $this->ensureDirectory(\dirname($filename), $mode); |
| 109 | 119 | } |
| 110 | 120 | |
| 111 | - if ($this->exists($filename)) { |
|
| 121 | + if ($this->exists($filename)) |
|
| 122 | + { |
|
| 112 | 123 | //Forcing mode for existed file |
| 113 | 124 | $this->setPermissions($filename, $mode); |
| 114 | 125 | } |
@@ -119,11 +130,14 @@ discard block |
||
| 119 | 130 | $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
| 120 | 131 | ); |
| 121 | 132 | |
| 122 | - if ($result !== false) { |
|
| 133 | + if ($result !== false) |
|
| 134 | + { |
|
| 123 | 135 | //Forcing mode after file creation |
| 124 | 136 | $this->setPermissions($filename, $mode); |
| 125 | 137 | } |
| 126 | - } catch (\Exception $e) { |
|
| 138 | + } |
|
| 139 | + catch (\Exception $e) |
|
| 140 | + { |
|
| 127 | 141 | throw new WriteErrorException($e->getMessage(), (int) $e->getCode(), $e); |
| 128 | 142 | } |
| 129 | 143 | |
@@ -141,7 +155,8 @@ discard block |
||
| 141 | 155 | |
| 142 | 156 | public function delete(string $filename): bool |
| 143 | 157 | { |
| 144 | - if ($this->exists($filename)) { |
|
| 158 | + if ($this->exists($filename)) |
|
| 159 | + { |
|
| 145 | 160 | $result = \unlink($filename); |
| 146 | 161 | |
| 147 | 162 | //Wiping out changes in local file cache |
@@ -160,7 +175,8 @@ discard block |
||
| 160 | 175 | */ |
| 161 | 176 | public function deleteDirectory(string $directory, bool $contentOnly = false): bool |
| 162 | 177 | { |
| 163 | - if (!$this->isDirectory($directory)) { |
|
| 178 | + if (!$this->isDirectory($directory)) |
|
| 179 | + { |
|
| 164 | 180 | throw new FilesException(\sprintf('Undefined or invalid directory %s', $directory)); |
| 165 | 181 | } |
| 166 | 182 | |
@@ -169,15 +185,20 @@ discard block |
||
| 169 | 185 | \RecursiveIteratorIterator::CHILD_FIRST |
| 170 | 186 | ); |
| 171 | 187 | |
| 172 | - foreach ($files as $file) { |
|
| 173 | - if ($file->isDir()) { |
|
| 188 | + foreach ($files as $file) |
|
| 189 | + { |
|
| 190 | + if ($file->isDir()) |
|
| 191 | + { |
|
| 174 | 192 | \rmdir($file->getRealPath()); |
| 175 | - } else { |
|
| 193 | + } |
|
| 194 | + else |
|
| 195 | + { |
|
| 176 | 196 | $this->delete($file->getRealPath()); |
| 177 | 197 | } |
| 178 | 198 | } |
| 179 | 199 | |
| 180 | - if (!$contentOnly) { |
|
| 200 | + if (!$contentOnly) |
|
| 201 | + { |
|
| 181 | 202 | return \rmdir($directory); |
| 182 | 203 | } |
| 183 | 204 | |
@@ -186,7 +207,8 @@ discard block |
||
| 186 | 207 | |
| 187 | 208 | public function move(string $filename, string $destination): bool |
| 188 | 209 | { |
| 189 | - if (!$this->exists($filename)) { |
|
| 210 | + if (!$this->exists($filename)) |
|
| 211 | + { |
|
| 190 | 212 | throw new FileNotFoundException($filename); |
| 191 | 213 | } |
| 192 | 214 | |
@@ -195,7 +217,8 @@ discard block |
||
| 195 | 217 | |
| 196 | 218 | public function copy(string $filename, string $destination): bool |
| 197 | 219 | { |
| 198 | - if (!$this->exists($filename)) { |
|
| 220 | + if (!$this->exists($filename)) |
|
| 221 | + { |
|
| 199 | 222 | throw new FileNotFoundException($filename); |
| 200 | 223 | } |
| 201 | 224 | |
@@ -204,7 +227,8 @@ discard block |
||
| 204 | 227 | |
| 205 | 228 | public function touch(string $filename, int $mode = null): bool |
| 206 | 229 | { |
| 207 | - if (!\touch($filename)) { |
|
| 230 | + if (!\touch($filename)) |
|
| 231 | + { |
|
| 208 | 232 | return false; |
| 209 | 233 | } |
| 210 | 234 | |
@@ -218,7 +242,8 @@ discard block |
||
| 218 | 242 | |
| 219 | 243 | public function size(string $filename): int |
| 220 | 244 | { |
| 221 | - if (!$this->exists($filename)) { |
|
| 245 | + if (!$this->exists($filename)) |
|
| 246 | + { |
|
| 222 | 247 | throw new FileNotFoundException($filename); |
| 223 | 248 | } |
| 224 | 249 | |
@@ -232,7 +257,8 @@ discard block |
||
| 232 | 257 | |
| 233 | 258 | public function md5(string $filename): string |
| 234 | 259 | { |
| 235 | - if (!$this->exists($filename)) { |
|
| 260 | + if (!$this->exists($filename)) |
|
| 261 | + { |
|
| 236 | 262 | throw new FileNotFoundException($filename); |
| 237 | 263 | } |
| 238 | 264 | |
@@ -241,7 +267,8 @@ discard block |
||
| 241 | 267 | |
| 242 | 268 | public function time(string $filename): int |
| 243 | 269 | { |
| 244 | - if (!$this->exists($filename)) { |
|
| 270 | + if (!$this->exists($filename)) |
|
| 271 | + { |
|
| 245 | 272 | throw new FileNotFoundException($filename); |
| 246 | 273 | } |
| 247 | 274 | |
@@ -260,7 +287,8 @@ discard block |
||
| 260 | 287 | |
| 261 | 288 | public function getPermissions(string $filename): int |
| 262 | 289 | { |
| 263 | - if (!$this->exists($filename)) { |
|
| 290 | + if (!$this->exists($filename)) |
|
| 291 | + { |
|
| 264 | 292 | throw new FileNotFoundException($filename); |
| 265 | 293 | } |
| 266 | 294 | |
@@ -269,7 +297,8 @@ discard block |
||
| 269 | 297 | |
| 270 | 298 | public function setPermissions(string $filename, int $mode): bool |
| 271 | 299 | { |
| 272 | - if (\is_dir($filename)) { |
|
| 300 | + if (\is_dir($filename)) |
|
| 301 | + { |
|
| 273 | 302 | //Directories must always be executable (i.e. 664 for dir => 775) |
| 274 | 303 | $mode |= 0111; |
| 275 | 304 | } |
@@ -280,8 +309,10 @@ discard block |
||
| 280 | 309 | public function getFiles(string $location, string $pattern = null): array |
| 281 | 310 | { |
| 282 | 311 | $result = []; |
| 283 | - foreach ($this->filesIterator($location, $pattern) as $filename) { |
|
| 284 | - if ($this->isDirectory($filename->getPathname())) { |
|
| 312 | + foreach ($this->filesIterator($location, $pattern) as $filename) |
|
| 313 | + { |
|
| 314 | + if ($this->isDirectory($filename->getPathname())) |
|
| 315 | + { |
|
| 285 | 316 | $result = \array_merge($result, $this->getFiles($filename . DIRECTORY_SEPARATOR)); |
| 286 | 317 | |
| 287 | 318 | continue; |
@@ -295,13 +326,15 @@ discard block |
||
| 295 | 326 | |
| 296 | 327 | public function tempFilename(string $extension = '', string $location = null): string |
| 297 | 328 | { |
| 298 | - if (empty($location)) { |
|
| 329 | + if (empty($location)) |
|
| 330 | + { |
|
| 299 | 331 | $location = \sys_get_temp_dir(); |
| 300 | 332 | } |
| 301 | 333 | |
| 302 | 334 | $filename = \tempnam($location, 'spiral'); |
| 303 | 335 | |
| 304 | - if (!empty($extension)) { |
|
| 336 | + if (!empty($extension)) |
|
| 337 | + { |
|
| 305 | 338 | $old = $filename; |
| 306 | 339 | $filename = \sprintf('%s.%s', $filename, $extension); |
| 307 | 340 | \rename($old, $filename); |
@@ -314,7 +347,8 @@ discard block |
||
| 314 | 347 | public function normalizePath(string $path, bool $asDirectory = false): string |
| 315 | 348 | { |
| 316 | 349 | $isUnc = \str_starts_with($path, '\\\\') || \str_starts_with($path, '//'); |
| 317 | - if ($isUnc) { |
|
| 350 | + if ($isUnc) |
|
| 351 | + { |
|
| 318 | 352 | $leadingSlashes = \substr($path, 0, 2); |
| 319 | 353 | $path = \substr($path, 2); |
| 320 | 354 | } |
@@ -337,15 +371,20 @@ discard block |
||
| 337 | 371 | $path = \explode('/', $path); |
| 338 | 372 | $relative = $path; |
| 339 | 373 | |
| 340 | - foreach ($from as $depth => $dir) { |
|
| 374 | + foreach ($from as $depth => $dir) |
|
| 375 | + { |
|
| 341 | 376 | //Find first non-matching dir |
| 342 | - if ($dir === $path[$depth]) { |
|
| 377 | + if ($dir === $path[$depth]) |
|
| 378 | + { |
|
| 343 | 379 | //Ignore this directory |
| 344 | 380 | \array_shift($relative); |
| 345 | - } else { |
|
| 381 | + } |
|
| 382 | + else |
|
| 383 | + { |
|
| 346 | 384 | //Get number of remaining dirs to $from |
| 347 | 385 | $remaining = \count($from) - $depth; |
| 348 | - if ($remaining > 1) { |
|
| 386 | + if ($remaining > 1) |
|
| 387 | + { |
|
| 349 | 388 | //Add traversals up to first matching directory |
| 350 | 389 | $padLength = (\count($relative) + $remaining - 1) * -1; |
| 351 | 390 | $relative = \array_pad($relative, $padLength, '..'); |