|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\ETL\Tests\Double; |
| 6 | + |
| 7 | +use Flow\Filesystem\{DestinationStream, FileStatus, Filesystem, Path, Protocol, SourceStream}; |
| 8 | +use Flow\Filesystem\Exception\{InvalidArgumentException, RuntimeException}; |
| 9 | +use Flow\Filesystem\Path\Filter; |
| 10 | +use Flow\Filesystem\Path\Filter\OnlyFiles; |
| 11 | +use Flow\Filesystem\Stream\{NativeLocalDestinationStream, NativeLocalSourceStream}; |
| 12 | +use Webmozart\Glob\Glob; |
| 13 | + |
| 14 | +final class FakeNativeLocalFilesystem implements Filesystem |
| 15 | +{ |
| 16 | + public function appendTo(Path $path) : DestinationStream |
| 17 | + { |
| 18 | + if ($path->isEqual($this->getSystemTmpDir())) { |
| 19 | + throw new RuntimeException('Cannot write to system tmp directory'); |
| 20 | + } |
| 21 | + |
| 22 | + $this->protocol()->validateScheme($path); |
| 23 | + |
| 24 | + if ($path->isPattern()) { |
| 25 | + throw new InvalidArgumentException("Pattern paths can't be written: " . $path->uri()); |
| 26 | + } |
| 27 | + |
| 28 | + if (!$this->status($path->parentDirectory())) { |
| 29 | + if (!\mkdir($concurrentDirectory = $path->parentDirectory()->path(), recursive: true) && !\is_dir($concurrentDirectory)) { |
| 30 | + throw new RuntimeException(\sprintf('Directory "%s" was not created', $concurrentDirectory)); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return NativeLocalDestinationStream::openAppend($path); |
| 35 | + } |
| 36 | + |
| 37 | + public function getSystemTmpDir() : Path |
| 38 | + { |
| 39 | + return new Path(\sys_get_temp_dir()); |
| 40 | + } |
| 41 | + |
| 42 | + public function list(Path $path, Filter $pathFilter = new OnlyFiles()) : \Generator |
| 43 | + { |
| 44 | + $this->protocol()->validateScheme($path); |
| 45 | + |
| 46 | + if (!$path->isPattern()) { |
| 47 | + if ($pathFilter->accept($status = new FileStatus($path, \is_file($path->path())))) { |
| 48 | + yield $status; |
| 49 | + } |
| 50 | + |
| 51 | + return; |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + foreach (Glob::glob($path->path()) as $filePath) { |
| 56 | + $status = new FileStatus(Path::realpath($filePath, $path->options()), \is_file($filePath)); |
| 57 | + |
| 58 | + if ($pathFilter->accept($status)) { |
| 59 | + yield $status; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public function mv(Path $from, Path $to) : bool |
| 65 | + { |
| 66 | + $this->protocol()->validateScheme($from); |
| 67 | + $this->protocol()->validateScheme($to); |
| 68 | + |
| 69 | + if (\file_exists($to->path())) { |
| 70 | + $this->rm($to); |
| 71 | + } |
| 72 | + |
| 73 | + if (!\rename($from->path(), $to->path())) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + public function protocol() : Protocol |
| 81 | + { |
| 82 | + return new Protocol('fake'); |
| 83 | + } |
| 84 | + |
| 85 | + public function readFrom(Path $path) : SourceStream |
| 86 | + { |
| 87 | + $this->protocol()->validateScheme($path); |
| 88 | + |
| 89 | + if ($path->isPattern()) { |
| 90 | + throw new InvalidArgumentException("Pattern paths can't be open: " . $path->uri()); |
| 91 | + } |
| 92 | + |
| 93 | + if (!$this->status($path->parentDirectory())) { |
| 94 | + if (!\mkdir($concurrentDirectory = $path->parentDirectory()->path(), recursive: true) && !\is_dir($concurrentDirectory)) { |
| 95 | + throw new RuntimeException(\sprintf('Directory "%s" was not created', $concurrentDirectory)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return NativeLocalSourceStream::open($path); |
| 100 | + } |
| 101 | + |
| 102 | + public function rm(Path $path) : bool |
| 103 | + { |
| 104 | + $this->protocol()->validateScheme($path); |
| 105 | + |
| 106 | + if (!$path->isPattern()) { |
| 107 | + if (!\file_exists($path->path())) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + if (\is_dir($path->path())) { |
| 112 | + $this->rmdir($path->path()); |
| 113 | + } else { |
| 114 | + \unlink($path->path()); |
| 115 | + } |
| 116 | + |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + $deletedCount = 0; |
| 121 | + |
| 122 | + foreach (Glob::glob($path->path()) as $filePath) { |
| 123 | + if (\is_dir($filePath)) { |
| 124 | + $this->rmdir($filePath); |
| 125 | + } else { |
| 126 | + \unlink($filePath); |
| 127 | + } |
| 128 | + |
| 129 | + $deletedCount++; |
| 130 | + } |
| 131 | + |
| 132 | + return (bool) $deletedCount; |
| 133 | + } |
| 134 | + |
| 135 | + public function status(Path $path) : ?FileStatus |
| 136 | + { |
| 137 | + $this->protocol()->validateScheme($path); |
| 138 | + |
| 139 | + if (!$path->isPattern() && \file_exists($path->path())) { |
| 140 | + return new FileStatus( |
| 141 | + $path, |
| 142 | + \is_file($path->path()) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + foreach (Glob::glob($path->path()) as $filePath) { |
| 147 | + if (\file_exists($filePath)) { |
| 148 | + return new FileStatus(new Path($filePath, $path->options()), true); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + public function writeTo(Path $path) : DestinationStream |
| 156 | + { |
| 157 | + if ($path->isEqual($this->getSystemTmpDir())) { |
| 158 | + throw new RuntimeException('Cannot write to system tmp directory'); |
| 159 | + } |
| 160 | + |
| 161 | + $this->protocol()->validateScheme($path); |
| 162 | + |
| 163 | + if ($path->isPattern()) { |
| 164 | + throw new InvalidArgumentException("Pattern paths can't be written: " . $path->uri()); |
| 165 | + } |
| 166 | + |
| 167 | + if (!$this->status($path->parentDirectory())) { |
| 168 | + if (!\mkdir($concurrentDirectory = $path->parentDirectory()->path(), recursive: true) && !\is_dir($concurrentDirectory)) { |
| 169 | + throw new RuntimeException(\sprintf('Directory "%s" was not created', $concurrentDirectory)); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return NativeLocalDestinationStream::openBlank($path); |
| 174 | + } |
| 175 | + |
| 176 | + private function rmdir(string $dirPath) : void |
| 177 | + { |
| 178 | + if (!\is_dir($dirPath)) { |
| 179 | + throw new InvalidArgumentException("{$dirPath} must be a directory"); |
| 180 | + } |
| 181 | + |
| 182 | + if (!\str_ends_with($dirPath, '/')) { |
| 183 | + $dirPath .= '/'; |
| 184 | + } |
| 185 | + |
| 186 | + $files = \scandir($dirPath); |
| 187 | + |
| 188 | + if (!$files) { |
| 189 | + throw new RuntimeException("Can't read directory: {$dirPath}"); |
| 190 | + } |
| 191 | + |
| 192 | + foreach ($files as $file) { |
| 193 | + if (\in_array($file, ['.', '..'], true)) { |
| 194 | + continue; |
| 195 | + } |
| 196 | + |
| 197 | + $filePath = $dirPath . $file; |
| 198 | + |
| 199 | + if (\is_dir($filePath)) { |
| 200 | + $this->rmdir($filePath); |
| 201 | + } else { |
| 202 | + \unlink($filePath); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + \rmdir($dirPath); |
| 207 | + } |
| 208 | +} |
0 commit comments