ByteTransformer
1 week ago
ReadStream
1 week ago
WriteStream
1 week ago
LICENSE.md
1 week ago
README.md
1 week ago
class-bytepipe.php
1 week ago
class-bytestreamexception.php
1 week ago
class-filereadwritestream.php
1 week ago
class-memorypipe.php
1 week ago
class-notenoughdataexception.php
1 week ago
composer.json
1 week ago
class-filereadwritestream.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\ByteStream; |
| 4 | |
| 5 | use WordPress\ByteStream\ReadStream\BaseByteReadStream; |
| 6 | |
| 7 | class FileReadWriteStream extends BaseByteReadStream implements BytePipe { |
| 8 | |
| 9 | /** @var resource|null */ |
| 10 | private $file_pointer; |
| 11 | |
| 12 | private $is_write_closed = false; |
| 13 | |
| 14 | public static function from_path( string $path, bool $truncate = false ): self { |
| 15 | $mode = $truncate ? 'w+b' : 'a+b'; |
| 16 | $file_pointer = fopen( $path, $mode ); |
| 17 | if ( ! $file_pointer ) { |
| 18 | throw new ByteStreamException( esc_html( "Cannot open $path" ) ); |
| 19 | } |
| 20 | |
| 21 | return new self( $file_pointer, filesize( $path ) ); |
| 22 | } |
| 23 | |
| 24 | public static function from_resource( $file_pointer ): self { |
| 25 | if ( ! is_resource( $file_pointer ) ) { |
| 26 | throw new ByteStreamException( 'Invalid resource' ); |
| 27 | } |
| 28 | |
| 29 | return new self( $file_pointer, fstat( $file_pointer )['size'] ); |
| 30 | } |
| 31 | |
| 32 | public function __construct( $file_pointer, $expected_length = null ) { |
| 33 | $this->file_pointer = $file_pointer; |
| 34 | $this->expected_length = $expected_length; |
| 35 | } |
| 36 | |
| 37 | protected function internal_pull( $n ): string { |
| 38 | return fread( $this->file_pointer, $n ); |
| 39 | } |
| 40 | |
| 41 | protected function internal_reached_end_of_data(): bool { |
| 42 | return ! is_resource( $this->file_pointer ) || feof( $this->file_pointer ); |
| 43 | } |
| 44 | |
| 45 | protected function seek_outside_of_buffer( int $target_offset ): void { |
| 46 | $this->buffer = ''; |
| 47 | $this->bytes_already_forgotten = $target_offset; |
| 48 | $this->offset_in_current_buffer = 0; |
| 49 | if ( 0 !== fseek( $this->file_pointer, $target_offset ) ) { |
| 50 | throw new ByteStreamException( 'fseek() failed' ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function append_bytes( string $bytes ): void { |
| 55 | if ( $this->is_write_closed ) { |
| 56 | throw new ByteStreamException( 'Cannot append after close_writing()' ); |
| 57 | } |
| 58 | if ( '' === $bytes ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $offset_before_append = ftell( $this->file_pointer ); |
| 63 | fseek( $this->file_pointer, 0, SEEK_END ); |
| 64 | |
| 65 | $len = fwrite( $this->file_pointer, $bytes ); |
| 66 | if ( false === $len || strlen( $bytes ) !== $len ) { |
| 67 | throw new ByteStreamException( 'fwrite() failed' ); |
| 68 | } |
| 69 | fflush( $this->file_pointer ); // ensures visibility for concurrent readers. |
| 70 | |
| 71 | // Rewind to the starting offset so we don't affect the reading position. |
| 72 | fseek( $this->file_pointer, $offset_before_append ); |
| 73 | |
| 74 | $this->expected_length += strlen( $bytes ); |
| 75 | } |
| 76 | |
| 77 | public function close_writing(): void { |
| 78 | $this->is_write_closed = true; |
| 79 | $this->maybe_close_file_pointer(); |
| 80 | } |
| 81 | |
| 82 | protected function internal_close_reading(): void { |
| 83 | $this->maybe_close_file_pointer(); |
| 84 | } |
| 85 | |
| 86 | private function maybe_close_file_pointer(): void { |
| 87 | if ( $this->is_read_closed && $this->is_write_closed && $this->file_pointer ) { |
| 88 | fclose( $this->file_pointer ); |
| 89 | $this->file_pointer = null; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 |