|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Neuron\Core\System; |
| 4 | + |
| 5 | +/** |
| 6 | + * In-memory file system implementation for testing. |
| 7 | + * |
| 8 | + * Provides a virtual file system that exists entirely in memory. Files and |
| 9 | + * directories can be programmatically added for testing without touching the |
| 10 | + * real file system. Perfect for unit testing file-dependent operations. |
| 11 | + */ |
| 12 | +class MemoryFileSystem implements IFileSystem |
| 13 | +{ |
| 14 | +/** |
| 15 | + * @var array<string, string> In-memory files (path => content) |
| 16 | + */ |
| 17 | +private array $files = []; |
| 18 | + |
| 19 | +/** |
| 20 | + * @var array<string, bool> In-memory directories (path => true) |
| 21 | + */ |
| 22 | +private array $directories = []; |
| 23 | + |
| 24 | +/** |
| 25 | + * @var string Current working directory |
| 26 | + */ |
| 27 | +private string $cwd = '/'; |
| 28 | + |
| 29 | +/** |
| 30 | + * Add a file to the virtual file system |
| 31 | + * |
| 32 | + * @param string $path File path |
| 33 | + * @param string $content File content |
| 34 | + * @return void |
| 35 | + */ |
| 36 | +public function addFile( string $path, string $content ): void |
| 37 | +{ |
| 38 | +$this->files[$path] = $content; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Add a directory to the virtual file system |
| 43 | + * |
| 44 | + * @param string $path Directory path |
| 45 | + * @return void |
| 46 | + */ |
| 47 | +public function addDirectory( string $path ): void |
| 48 | +{ |
| 49 | +$this->directories[$path] = true; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Set the current working directory |
| 54 | + * |
| 55 | + * @param string $path Directory path |
| 56 | + * @return void |
| 57 | + */ |
| 58 | +public function setCwd( string $path ): void |
| 59 | +{ |
| 60 | +$this->cwd = $path; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Remove a file from the virtual file system |
| 65 | + * |
| 66 | + * @param string $path File path |
| 67 | + * @return void |
| 68 | + */ |
| 69 | +public function removeFile( string $path ): void |
| 70 | +{ |
| 71 | +unset( $this->files[$path] ); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Remove a directory from the virtual file system |
| 76 | + * |
| 77 | + * @param string $path Directory path |
| 78 | + * @return void |
| 79 | + */ |
| 80 | +public function removeDirectory( string $path ): void |
| 81 | +{ |
| 82 | +unset( $this->directories[$path] ); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Clear all files and directories |
| 87 | + * |
| 88 | + * @return void |
| 89 | + */ |
| 90 | +public function clear(): void |
| 91 | +{ |
| 92 | +$this->files = []; |
| 93 | +$this->directories = []; |
| 94 | +$this->cwd = '/'; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * @inheritDoc |
| 99 | + */ |
| 100 | +public function fileExists( string $path ): bool |
| 101 | +{ |
| 102 | +return isset( $this->files[$path] ); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * @inheritDoc |
| 107 | + */ |
| 108 | +public function readFile( string $path ): string|false |
| 109 | +{ |
| 110 | +return $this->files[$path] ?? false; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * @inheritDoc |
| 115 | + */ |
| 116 | +public function isDir( string $path ): bool |
| 117 | +{ |
| 118 | +return isset( $this->directories[$path] ); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * @inheritDoc |
| 123 | + */ |
| 124 | +public function realpath( string $path ): string|false |
| 125 | +{ |
| 126 | +// Simplistic realpath: just check if file or directory exists |
| 127 | +if( $this->fileExists( $path ) || $this->isDir( $path ) ) |
| 128 | +{ |
| 129 | +return $path; |
| 130 | +} |
| 131 | + |
| 132 | +return false; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * @inheritDoc |
| 137 | + */ |
| 138 | +public function getcwd(): string|false |
| 139 | +{ |
| 140 | +return $this->cwd; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * @inheritDoc |
| 145 | + */ |
| 146 | +public function writeFile( string $path, string $data ): int|false |
| 147 | +{ |
| 148 | +$this->files[$path] = $data; |
| 149 | +return strlen( $data ); |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Get all files in the virtual file system |
| 154 | + * |
| 155 | + * @return array<string, string> |
| 156 | + */ |
| 157 | +public function getFiles(): array |
| 158 | +{ |
| 159 | +return $this->files; |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * Get all directories in the virtual file system |
| 164 | + * |
| 165 | + * @return array<string, bool> |
| 166 | + */ |
| 167 | +public function getDirectories(): array |
| 168 | +{ |
| 169 | +return $this->directories; |
| 170 | +} |
| 171 | +} |
0 commit comments