ByteStream
6 days ago
Layer
6 days ago
Mixin
6 days ago
Path
6 days ago
Visitor
6 days ago
LICENSE.md
6 days ago
README.md
6 days ago
class-filesystemexception.php
6 days ago
class-inmemoryfilesystem.php
6 days ago
class-localfilesystem.php
6 days ago
class-sqlitefilesystem.php
6 days ago
class-uploadedfilesystem.php
6 days ago
composer.json
6 days ago
functions.php
6 days ago
interface-filesystem.php
6 days ago
interface-filesystem.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\Filesystem; |
| 4 | |
| 5 | use WordPress\ByteStream\ReadStream\ByteReadStream; |
| 6 | use WordPress\ByteStream\WriteStream\ByteWriteStream; |
| 7 | |
| 8 | /** |
| 9 | * Interface for filesystem implementations. |
| 10 | * |
| 11 | * It enables navigating multiple filesystem implementations in a unified way. |
| 12 | * For example, Zip_Filesystem and Local_Filesystem are both implemented |
| 13 | * as subclasses of this class. |
| 14 | */ |
| 15 | interface Filesystem { |
| 16 | |
| 17 | /** |
| 18 | * List the contents of a directory. |
| 19 | * |
| 20 | * @param string $dir The path to the parent directory. |
| 21 | * |
| 22 | * @return array<string> The contents of the directory. |
| 23 | * @throws FilesystemException If the directory cannot be listed. |
| 24 | */ |
| 25 | public function ls( $dir = '/' ); |
| 26 | |
| 27 | /** |
| 28 | * Check if a path is a directory. |
| 29 | * |
| 30 | * @param string $path The path to check. |
| 31 | * |
| 32 | * @return bool True if the path is a directory, false otherwise. |
| 33 | * @throws FilesystemException If the path cannot be checked. |
| 34 | */ |
| 35 | public function is_dir( $path ); |
| 36 | |
| 37 | /** |
| 38 | * Check if a path is a file. |
| 39 | * |
| 40 | * @param string $path The path to check. |
| 41 | * |
| 42 | * @return bool True if the path is a file, false otherwise. |
| 43 | * @throws FilesystemException If the path cannot be checked. |
| 44 | */ |
| 45 | public function is_file( $path ); |
| 46 | |
| 47 | /** |
| 48 | * Check if a path exists. |
| 49 | * |
| 50 | * @param string $path The path to check. |
| 51 | * |
| 52 | * @return bool True if the path exists, false otherwise. |
| 53 | * @throws FilesystemException If the path cannot be checked. |
| 54 | */ |
| 55 | public function exists( $path ); |
| 56 | |
| 57 | /** |
| 58 | * Create a directory. |
| 59 | * |
| 60 | * @param string $path The path to create. |
| 61 | * @param array $options Additional options. |
| 62 | * |
| 63 | * @throws FilesystemException If the directory cannot be created. |
| 64 | */ |
| 65 | public function mkdir( $path, $options = array() ); |
| 66 | |
| 67 | /** |
| 68 | * Remove a file. |
| 69 | * |
| 70 | * @param string $path The path to remove. |
| 71 | * |
| 72 | * @throws FilesystemException If the file cannot be removed. |
| 73 | */ |
| 74 | public function rm( $path ); |
| 75 | |
| 76 | /** |
| 77 | * Remove a directory. |
| 78 | * |
| 79 | * @param string $path The path to remove. |
| 80 | * @param array $options Additional options. |
| 81 | * |
| 82 | * @throws FilesystemException If the directory cannot be removed. |
| 83 | */ |
| 84 | public function rmdir( $path, $options = array() ); |
| 85 | |
| 86 | /** |
| 87 | * Start streaming a file. |
| 88 | * |
| 89 | * @param string $path The path to the file. |
| 90 | * |
| 91 | * @return ByteReadStream The stream identifier. |
| 92 | * @throws FilesystemException If the stream cannot be opened. |
| 93 | * @example |
| 94 | * |
| 95 | * $fs->open_read_stream($path); |
| 96 | * while($fs->next_file_chunk()) { |
| 97 | * $chunk = $fs->get_file_chunk(); |
| 98 | * // process $chunk |
| 99 | * } |
| 100 | * $fs->close_read_stream(); |
| 101 | */ |
| 102 | public function open_read_stream( $path ): ByteReadStream; |
| 103 | |
| 104 | /** |
| 105 | * Open a write stream to a file. |
| 106 | * |
| 107 | * @param string $path The path to write to. |
| 108 | * |
| 109 | * @return ByteWriteStream The stream identifier. |
| 110 | * @throws FilesystemException If the stream cannot be opened. |
| 111 | */ |
| 112 | public function open_write_stream( $path ): ByteWriteStream; |
| 113 | |
| 114 | /** |
| 115 | * Write data to a file. |
| 116 | * |
| 117 | * @param string $path The path to write to. |
| 118 | * @param string $data The data to write. |
| 119 | * @param array $options Additional options. |
| 120 | * |
| 121 | * @throws FilesystemException If the data cannot be written. |
| 122 | */ |
| 123 | public function put_contents( $path, $data, $options = array() ); |
| 124 | |
| 125 | /** |
| 126 | * Copy a file from one path to another. |
| 127 | * |
| 128 | * @param string $from_path The path to copy from. |
| 129 | * @param string $to_path The path to copy to. |
| 130 | * @param array $options Additional options. |
| 131 | * |
| 132 | * @throws FilesystemException If the file cannot be copied. |
| 133 | */ |
| 134 | public function copy( $from_path, $to_path, $options = array() ); |
| 135 | |
| 136 | /** |
| 137 | * Moves a file from one path to another. |
| 138 | * |
| 139 | * @param string $from_path The path to move from. |
| 140 | * @param string $to_path The path to move to. |
| 141 | * @param array $options Additional options. |
| 142 | * |
| 143 | * @throws FilesystemException If the file cannot be moved. |
| 144 | */ |
| 145 | public function rename( $from_path, $to_path, $options = array() ); |
| 146 | |
| 147 | /** |
| 148 | * Buffers the entire contents of a file into a string |
| 149 | * and returns it. |
| 150 | * |
| 151 | * @param string $path The path to the file. |
| 152 | * |
| 153 | * @return string The contents of the file. |
| 154 | * @throws FilesystemException If the file cannot be read. |
| 155 | */ |
| 156 | public function get_contents( $path ); |
| 157 | |
| 158 | /** |
| 159 | * Get the metadata of the filesystem instance. Different |
| 160 | * for every filesystem implementation. |
| 161 | * |
| 162 | * @return array<string, mixed> The metadata of the filesystem. |
| 163 | */ |
| 164 | public function get_meta(): array; |
| 165 | } |
| 166 |