class-bytewritestream.php
1 week ago
class-filewritestream.php
1 week ago
class-transformedwritestream.php
1 week ago
class-filewritestream.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\ByteStream\WriteStream; |
| 4 | |
| 5 | use WordPress\ByteStream\ByteStreamException; |
| 6 | |
| 7 | class FileWriteStream implements ByteWriteStream { |
| 8 | |
| 9 | private $file_handle; |
| 10 | |
| 11 | /** |
| 12 | * Creates a new instance of FileWriter from a file path with a mode (truncate or append). |
| 13 | * |
| 14 | * @param string $path Path to the file. |
| 15 | * @param string $mode Writing mode: 'truncate' or 'append'. |
| 16 | * |
| 17 | * @return FileWriteStream |
| 18 | * @throws ByteStreamException If the file cannot be opened for writing. |
| 19 | */ |
| 20 | public static function from_path( string $path, string $mode = 'append' ): self { |
| 21 | switch ( $mode ) { |
| 22 | case 'truncate': |
| 23 | $file_mode = 'wb'; // Write mode: truncates the file. |
| 24 | break; |
| 25 | case 'append': |
| 26 | $file_mode = 'ab'; // Append mode: appends to the file. |
| 27 | break; |
| 28 | default: |
| 29 | throw new ByteStreamException( esc_html( "Invalid mode: $mode. Use 'truncate' or 'append'." ) ); |
| 30 | } |
| 31 | |
| 32 | $file_handle = fopen( $path, $file_mode ); |
| 33 | if ( false === $file_handle ) { |
| 34 | throw new ByteStreamException( esc_html( "Failed to open file at path: $path" ) ); |
| 35 | } |
| 36 | |
| 37 | return new self( $file_handle ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Creates a new instance of FileWriter from an existing file handle. |
| 42 | * |
| 43 | * @param resource $file_handle A valid file handle. |
| 44 | * |
| 45 | * @return FileWriteStream |
| 46 | * @throws ByteStreamException If the file handle is invalid. |
| 47 | */ |
| 48 | public static function from_resource_handle( $file_handle ): self { |
| 49 | return new self( $file_handle ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Private constructor to enforce the use of static factory methods. |
| 54 | * |
| 55 | * @param resource $file_handle A valid file handle. |
| 56 | * @throws ByteStreamException If the file handle is invalid. |
| 57 | */ |
| 58 | public function __construct( $file_handle ) { |
| 59 | if ( ! is_resource( $file_handle ) || 'stream' !== get_resource_type( $file_handle ) ) { |
| 60 | throw new ByteStreamException( 'Invalid file handle provided.' ); |
| 61 | } |
| 62 | $this->file_handle = $file_handle; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Appends bytes to the file. |
| 67 | * |
| 68 | * @param string $bytes The data to write. |
| 69 | * |
| 70 | * @return void |
| 71 | * @throws ByteStreamException If the write operation fails. |
| 72 | */ |
| 73 | public function append_bytes( string $bytes ): void { |
| 74 | $result = fwrite( $this->file_handle, $bytes ); |
| 75 | /** |
| 76 | * We cannot just test for `false === $result` if we want to be |
| 77 | * compatible with PHP 7.3. |
| 78 | * |
| 79 | * The `!fwrite()` check is used for PHP 7.3 compatibility. |
| 80 | * Between PHP 7.3 and 7.4, this change was made: |
| 81 | * |
| 82 | * > fread() and fwrite() will now return FALSE if the operation failed. Previously an empty |
| 83 | * > string or 0 was returned. EAGAIN/EWOULDBLOCK are not considered failures. |
| 84 | * |
| 85 | * https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.core.fread-fwrite |
| 86 | */ |
| 87 | if ( ! $result && '' !== $bytes ) { |
| 88 | throw new ByteStreamException( 'Failed to write bytes to file.' ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Closes the file handle. |
| 94 | * |
| 95 | * @return void |
| 96 | * @throws ByteStreamException If the file handle is already closed. |
| 97 | */ |
| 98 | public function close_writing(): void { |
| 99 | if ( null === $this->file_handle ) { |
| 100 | throw new ByteStreamException( 'File handle is already closed.' ); |
| 101 | } |
| 102 | |
| 103 | fclose( $this->file_handle ); |
| 104 | $this->file_handle = null; |
| 105 | } |
| 106 | } |
| 107 |