|
| 1 | +<?php //phpcs:disable WordPress.WP.AlternativeFunctions |
| 2 | +/** |
| 3 | + * WP_Filesystem_Streaming class. |
| 4 | + * |
| 5 | + * File intentionally left without namespace. |
| 6 | + * |
| 7 | + * @package WooCommerce Sync Service |
| 8 | + * @subpackage WP |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Custom WP filesystem class |
| 13 | + */ |
| 14 | +class WP_Filesystem_Streaming extends WP_Filesystem_Base { |
| 15 | + /** |
| 16 | + * Current file path |
| 17 | + * |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + private $file_path; |
| 21 | + |
| 22 | + /** |
| 23 | + * Current file handle. |
| 24 | + * |
| 25 | + * @var null|resource $handle |
| 26 | + */ |
| 27 | + private $handle; |
| 28 | + |
| 29 | + /** |
| 30 | + * Class constructor |
| 31 | + */ |
| 32 | + public function __construct() { |
| 33 | + $this->file_path = ''; |
| 34 | + $this->handle = null; |
| 35 | + $this->method = 'streaming'; |
| 36 | + $this->errors = new \WP_Error(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Opens the file handle |
| 41 | + * |
| 42 | + * @param string $file File path. |
| 43 | + * @param string $method Optional - Either 'r' or 'w'. |
| 44 | + * @return WP_Error|bool True on success, WP_Error on failure. |
| 45 | + */ |
| 46 | + public function open_file( $file, $method = 'a+' ) { |
| 47 | + if ( ! is_null( $this->handle ) ) { |
| 48 | + return new \WP_Error( 'fs_already_open', 'Filesystem is already open' ); |
| 49 | + } |
| 50 | + |
| 51 | + $handle = fopen( $file, $method ); |
| 52 | + |
| 53 | + if ( ! $handle ) { |
| 54 | + return new \WP_Error( 'fs_no_file', 'Could not open file' ); |
| 55 | + } |
| 56 | + |
| 57 | + $this->handle = $handle; |
| 58 | + $this->file_path = $file; |
| 59 | + |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Closes the current open file handle |
| 65 | + * |
| 66 | + * @return bool True on success, false on failure. |
| 67 | + */ |
| 68 | + public function close_file() { |
| 69 | + return fclose( $this->handle ) |
| 70 | + ? true |
| 71 | + : new \WP_Error( 'fs_no_file', 'Could not close file' ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Writes to the file handle |
| 76 | + * |
| 77 | + * @param string $data Data to write. |
| 78 | + * @return int|WP_Error Number of bytes written on success, WP_Error on failure. |
| 79 | + */ |
| 80 | + public function write( $data ) { |
| 81 | + return fwrite( $this->handle, $data ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Writes a CSV Line to the file handle |
| 86 | + * |
| 87 | + * @param mixed $data Fields to write. |
| 88 | + * @param string $separator Field separator. |
| 89 | + * @param string $enclosure Field enclosure. |
| 90 | + * @param string $escape Field escape. |
| 91 | + * @param string $eol End of line. |
| 92 | + * @return int|false Number of bytes written on success, false on failure. |
| 93 | + */ |
| 94 | + public function write_csv( $data, $separator = ',', $enclosure = '"', $escape = '\\', $eol = "\n" ) { |
| 95 | + return fputcsv( $this->handle, $data, $separator, $enclosure, $escape ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Checks if the file handle is at the end of the file |
| 100 | + * |
| 101 | + * @return bool |
| 102 | + */ |
| 103 | + public function end_of_file(): bool { |
| 104 | + return feof( $this->handle ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Reads a line from the file handle |
| 109 | + * |
| 110 | + * @return string |
| 111 | + */ |
| 112 | + public function read_line(): string { |
| 113 | + if ( ! feof( $this->handle ) ) { |
| 114 | + return fgets( $this->handle ); |
| 115 | + } |
| 116 | + |
| 117 | + return new \WP_Error( 'fs_eof', 'End of file reached' ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Reads from the file handle |
| 122 | + * |
| 123 | + * @param int $length Number of lines to read. |
| 124 | + * @param int $offset Offset to start reading from. |
| 125 | + * @return array|WP_Error Array of lines read on success, WP_Error on failure. |
| 126 | + */ |
| 127 | + public function read_lines( int $length, int $offset ): array { |
| 128 | + $current_line = 0; |
| 129 | + $lines = array(); |
| 130 | + |
| 131 | + while ( $current_line < $offset && ! feof( $this->handle ) ) { |
| 132 | + fgets( $this->handle ); |
| 133 | + ++$current_line; |
| 134 | + } |
| 135 | + |
| 136 | + while ( $current_line < $offset + $length && ! feof( $this->handle ) ) { |
| 137 | + $lines[] = fgets( $this->handle ); |
| 138 | + ++$current_line; |
| 139 | + } |
| 140 | + |
| 141 | + return $lines; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Gets the number of lines in the file handle |
| 146 | + * |
| 147 | + * @return int |
| 148 | + */ |
| 149 | + public function get_number_of_lines(): int { |
| 150 | + $current_line = 0; |
| 151 | + |
| 152 | + while ( ! feof( $this->handle ) ) { |
| 153 | + fgets( $this->handle ); |
| 154 | + ++$current_line; |
| 155 | + } |
| 156 | + |
| 157 | + rewind( $this->handle ); |
| 158 | + |
| 159 | + return $current_line; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Reads from the file handle |
| 164 | + * |
| 165 | + * @return string|WP_Error String of bytes read on success, WP_Error on failure. |
| 166 | + */ |
| 167 | + public function read_file(): string|\WP_Error { |
| 168 | + $file_size = filesize( $this->file_path ); |
| 169 | + |
| 170 | + if ( ! $file_size ) { |
| 171 | + return new \WP_Error( 'fs_no_file', 'Could not read file' ); |
| 172 | + } |
| 173 | + |
| 174 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread |
| 175 | + $data = fread( $this->handle, $file_size ); |
| 176 | + |
| 177 | + $this->close_file(); |
| 178 | + |
| 179 | + return $data; |
| 180 | + } |
| 181 | +} |
0 commit comments