|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the aether/aether. |
| 5 | + * |
| 6 | + * Copyright (C) 2024 Dominik Szamburski |
| 7 | + * |
| 8 | + * This software may be modified and distributed under the terms |
| 9 | + * of the MIT license. See the LICENSE file for details. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Aether\Filesystem; |
| 13 | + |
| 14 | +class Filesystem implements FilesystemInterface |
| 15 | +{ |
| 16 | + public function exists(string $path): bool |
| 17 | + { |
| 18 | + if (\strlen($path) > \PHP_MAXPATHLEN - 2) { |
| 19 | + throw new IOException("Could not check if file exists because path exceeds " . (\PHP_MAXPATHLEN - 2) . " characters."); |
| 20 | + } |
| 21 | + |
| 22 | + return \file_exists($path); |
| 23 | + } |
| 24 | + |
| 25 | + public function read(string $path): string |
| 26 | + { |
| 27 | + if (! $this->exists($path) && ! $this->isFile($path)) { |
| 28 | + throw new IOException("File $path does not exists."); |
| 29 | + } |
| 30 | + |
| 31 | + return \file_get_contents($path); |
| 32 | + } |
| 33 | + |
| 34 | + public function write(string $path, string $contents, bool $lock = false): void |
| 35 | + { |
| 36 | + if (\file_put_contents($path, $contents, $lock ? \LOCK_EX : 0) === false) { |
| 37 | + throw new IOException("Failed to save file $path."); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function copy(string $source, string $destination): void |
| 42 | + { |
| 43 | + if (\copy($source, $destination) === false) { |
| 44 | + throw new IOException("Filed to copy '".$source."' to '".$destination."'."); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public function move(string $source, string $destination): void |
| 49 | + { |
| 50 | + if (\rename($source, $destination) === false) { |
| 51 | + throw new IOException("Failed to rename '".$source."' to '".$destination."'."); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public function append(string $path, string $contents): bool |
| 56 | + { |
| 57 | + return \file_put_contents($path, $contents, \FILE_APPEND) !== false; |
| 58 | + } |
| 59 | + |
| 60 | + public function touch(string $filename, ?int $mtime = null, ?int $atime = null): void |
| 61 | + { |
| 62 | + if (\touch($filename, $mtime, $atime) === false) { |
| 63 | + throw new IOException("Failed to touch '$filename'."); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public function unlink(string|array $filename): void |
| 68 | + { |
| 69 | + $paths = \is_array($filename) ? $filename : [$filename]; |
| 70 | + |
| 71 | + foreach ($paths as $path) { |
| 72 | + if (\unlink($path) === false) { |
| 73 | + throw new IOException("Failed to unlink '". $path ."'."); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function mkdir(string $directory, int $mode = 0777, bool $recursive = false): void |
| 79 | + { |
| 80 | + if (\mkdir($directory, $mode, $recursive) === false) { |
| 81 | + throw new IOException("Failed to create a directory '".$directory."'."); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public function rmdir(string $directory): void |
| 86 | + { |
| 87 | + if (! $this->isDirectory($directory)) { |
| 88 | + throw new IOException("Directory $directory does not exists."); |
| 89 | + } |
| 90 | + |
| 91 | + $items = new \FilesystemIterator($directory); |
| 92 | + foreach ($items as $item) { |
| 93 | + $item->isDir() && ! $item->isLink() |
| 94 | + ? $this->rmdir($item->getPathname()) |
| 95 | + : $this->unlink($item->getPathname()); |
| 96 | + } |
| 97 | + |
| 98 | + unset($items); |
| 99 | + @rmdir($directory); |
| 100 | + } |
| 101 | + |
| 102 | + public function name(string $path): string |
| 103 | + { |
| 104 | + return \pathinfo($path, \PATHINFO_FILENAME); |
| 105 | + } |
| 106 | + |
| 107 | + public function basename(string $path): string |
| 108 | + { |
| 109 | + return \pathinfo($path, \PATHINFO_BASENAME); |
| 110 | + } |
| 111 | + |
| 112 | + public function dirname(string $path): string |
| 113 | + { |
| 114 | + return \pathinfo($path, \PATHINFO_DIRNAME); |
| 115 | + } |
| 116 | + |
| 117 | + public function extension(string $path): string |
| 118 | + { |
| 119 | + return \pathinfo($path, \PATHINFO_EXTENSION); |
| 120 | + } |
| 121 | + |
| 122 | + public function filesize(string $path): int |
| 123 | + { |
| 124 | + if (! $this->exists($path)) { |
| 125 | + throw new IOException("File $path does not exists."); |
| 126 | + } |
| 127 | + |
| 128 | + return \filesize($path); |
| 129 | + } |
| 130 | + |
| 131 | + public function isDirectory(string $path): bool |
| 132 | + { |
| 133 | + return \is_dir($path); |
| 134 | + } |
| 135 | + |
| 136 | + public function isFile(string $path): bool |
| 137 | + { |
| 138 | + return \is_file($path); |
| 139 | + } |
| 140 | + |
| 141 | + public function mimeType(string $path): string |
| 142 | + { |
| 143 | + if (! $this->exists($path)) { |
| 144 | + throw new IOException("File not found $path"); |
| 145 | + } |
| 146 | + |
| 147 | + return \mime_content_type($path); |
| 148 | + } |
| 149 | +} |
0 commit comments