class-basebytereadstream.php
1 week ago
class-deflatereadstream.php
1 week ago
class-filereadstream.php
1 week ago
class-inflatereadstream.php
1 week ago
class-limitedbytereadstream.php
1 week ago
class-transformedreadstream.php
1 week ago
interface-bytereadstream.php
1 week ago
class-filereadstream.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\ByteStream\ReadStream; |
| 4 | |
| 5 | use WordPress\ByteStream\ByteStreamException; |
| 6 | |
| 7 | class FileReadStream extends BaseByteReadStream { |
| 8 | |
| 9 | protected $file_pointer; |
| 10 | |
| 11 | public static function from_path( $file_path ) { |
| 12 | if ( ! file_exists( $file_path ) ) { |
| 13 | throw new ByteStreamException( esc_html( sprintf( 'File %s does not exist', $file_path ) ) ); |
| 14 | } |
| 15 | if ( ! is_file( $file_path ) ) { |
| 16 | throw new ByteStreamException( esc_html( sprintf( '%s is not a file', $file_path ) ) ); |
| 17 | } |
| 18 | $handle = fopen( $file_path, 'r' ); |
| 19 | if ( ! $handle ) { |
| 20 | throw new ByteStreamException( esc_html( sprintf( 'Failed to open file %s', $file_path ) ) ); |
| 21 | } |
| 22 | |
| 23 | return self::from_resource( $handle, filesize( $file_path ) ); |
| 24 | } |
| 25 | |
| 26 | public static function from_resource( $handle, $expected_length = null ) { |
| 27 | if ( ! is_resource( $handle ) ) { |
| 28 | throw new ByteStreamException( 'Invalid file pointer provided' ); |
| 29 | } |
| 30 | |
| 31 | return new self( $handle, $expected_length ); |
| 32 | } |
| 33 | |
| 34 | public function __construct( $fp, $expected_length = null ) { |
| 35 | $this->file_pointer = $fp; |
| 36 | $this->expected_length = $expected_length; |
| 37 | } |
| 38 | |
| 39 | protected function internal_pull( $n ): string { |
| 40 | $bytes = fread( $this->file_pointer, $n ); |
| 41 | /** |
| 42 | * Workaround for a streaming bug in WordPress Playground. |
| 43 | * |
| 44 | * Without the feof() call, Playground doesn't notice when the stream reaches EOF. |
| 45 | * The feof() call in internal_reached_end_of_data() somehow does not trigger the |
| 46 | * EOF event. It must be here, right after fread(). |
| 47 | * |
| 48 | * @TODO: Improve the streaming support in WordPress Playground. |
| 49 | */ |
| 50 | feof( $this->file_pointer ); |
| 51 | if ( false === $bytes ) { |
| 52 | throw new ByteStreamException( 'Failed to read from file' ); |
| 53 | } |
| 54 | |
| 55 | return $bytes; |
| 56 | } |
| 57 | |
| 58 | protected function seek_outside_of_buffer( int $target_offset ): void { |
| 59 | $retval = fseek( $this->file_pointer, $target_offset ); |
| 60 | if ( -1 === $retval ) { |
| 61 | throw new ByteStreamException( 'Failed to seek to offset' ); |
| 62 | } |
| 63 | |
| 64 | $this->buffer = ''; |
| 65 | $this->offset_in_current_buffer = 0; |
| 66 | $this->bytes_already_forgotten = $target_offset; |
| 67 | } |
| 68 | |
| 69 | public function close_reading(): void { |
| 70 | if ( $this->is_read_closed ) { |
| 71 | return; |
| 72 | } |
| 73 | $this->is_read_closed = true; |
| 74 | $this->buffer = ''; |
| 75 | if ( ! fclose( $this->file_pointer ) ) { |
| 76 | throw new ByteStreamException( 'Failed to close file pointer' ); |
| 77 | } |
| 78 | $this->file_pointer = null; |
| 79 | } |
| 80 | |
| 81 | protected function internal_reached_end_of_data(): bool { |
| 82 | return ! is_resource( $this->file_pointer ) || feof( $this->file_pointer ); |
| 83 | } |
| 84 | } |
| 85 |