Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,238 @@
<?php
namespace WordPress\ByteStream\ReadStream;
use WordPress\ByteStream\ByteStreamException;
use WordPress\ByteStream\NotEnoughDataException;
abstract class BaseByteReadStream implements ByteReadStream {
const CHUNK_SIZE_BYTES = 64 * 1024; // 64kb.
/**
* The maximum number of consumed bytes to keep in memory.
*
* For example:
*
* The quick brown fox jumps over the lazy dog.
* ^-------------------^
* consumed bytes
*
* Say the maximum lookbehind bytes is 4. Then the byte stream will forget about
* all consumed bytes except the last 4:
*
* fox jumps over the lazy dog.
* ^--^
* consumed but retained for seek()-ing backwards.
*
* @var int
*/
protected $max_lookbehind_bytes = 2048;
/**
* The remaining unconsumed bytes.
*
* @var string
*/
protected $buffer = '';
/**
* How many bytes have already been consumed in the current **buffer**.
*
* @var int
*/
protected $offset_in_current_buffer = 0;
/**
* How many bytes have already been forgotten in the current **stream**.
*
* @var int
*/
protected $bytes_already_forgotten = 0;
/**
* Whether the stream has been closed for reading.
*
* @var bool
*/
protected $is_read_closed = false;
/**
* How many bytes are expected in the stream. Optional.
*
* When it's null, the stream is unbounded and length() will also return null.
*
* @var int|null
*/
protected $expected_length = null;
public function length(): ?int {
return $this->expected_length;
}
public function pull( ?int $n = self::CHUNK_SIZE_BYTES, string $mode = self::PULL_NO_MORE_THAN ): int {
switch ( $mode ) {
case self::PULL_NO_MORE_THAN:
case self::PULL_EXACTLY:
break;
default:
throw new ByteStreamException( 'Invalid pull mode' );
}
if ( $this->is_read_closed ) {
throw new ByteStreamException( 'Cannot pull() on a closed producer' );
}
if ( 0 === $n ) {
return 0;
}
if ( $n < 0 ) {
throw new ByteStreamException( 'Cannot pull a negative number of bytes' );
}
if ( $n <= $this->count_consumable_bytes() ) {
return $n;
}
if ( $this->reached_end_of_data() ) {
if ( ByteReadStream::PULL_EXACTLY === $mode ) {
throw new NotEnoughDataException( 'End of data reached while pulling' );
}
return 0;
}
if ( ByteReadStream::PULL_NO_MORE_THAN === $mode ) {
return $this->pull_no_more_than( $n );
}
return $this->pull_exactly( $n );
}
protected function pull_exactly( $n ): int {
$empty_pulls = 0;
while ( $this->count_consumable_bytes() < $n ) {
$consumable_before = $this->count_consumable_bytes();
$this->pull_no_more_than( $n );
$consumable_after = $this->count_consumable_bytes();
if ( $consumable_after === $consumable_before ) {
++$empty_pulls;
if ( $this->reached_end_of_data() ) {
throw new NotEnoughDataException( 'End of data reached while pulling' );
}
}
if ( $empty_pulls > 4 ) {
throw new NotEnoughDataException( '4 empty pulls in a row, we are probably at the end of the data' );
}
}
return $n;
}
protected function pull_no_more_than( $n ): int {
$this->buffer .= $this->internal_pull( self::CHUNK_SIZE_BYTES );
return min( $n, $this->count_consumable_bytes() );
}
public function consume_all(): string {
$body = '';
while ( true ) {
if ( $this->reached_end_of_data() ) {
return $body;
}
$consumable = $this->pull( self::CHUNK_SIZE_BYTES );
$body .= $this->consume( $consumable );
}
}
protected function count_consumable_bytes(): int {
return strlen( $this->buffer ) - $this->offset_in_current_buffer;
}
abstract protected function internal_pull( $n ): string;
public function peek( int $n ): string {
return substr( $this->buffer, $this->offset_in_current_buffer, $n );
}
public function consume( int $n ): string {
if ( strlen( $this->buffer ) < $this->offset_in_current_buffer + $n ) {
throw new NotEnoughDataException( 'Cannot consume more bytes than available in the buffer.' );
}
$bytes = substr( $this->buffer, $this->offset_in_current_buffer, $n );
$this->offset_in_current_buffer += $n;
if ( $this->offset_in_current_buffer > $this->max_lookbehind_bytes ) {
$overflow = $this->offset_in_current_buffer - $this->max_lookbehind_bytes;
$this->offset_in_current_buffer -= $overflow;
$this->bytes_already_forgotten += $overflow;
$this->buffer = substr( $this->buffer, $overflow );
}
return $bytes;
}
public function seek( int $target_offset ): void {
// We have that offset in the buffer, let's just update the pointer.
if ( $target_offset >= $this->bytes_already_forgotten && $target_offset <= $this->bytes_already_forgotten + strlen( $this->buffer ) ) {
$this->offset_in_current_buffer = $target_offset - $this->bytes_already_forgotten;
return;
}
if ( null !== $this->length() && $target_offset > $this->length() ) {
$length = $this->length();
throw new NotEnoughDataException(
esc_html(
sprintf(
'Cannot seek to past the stream length (seeked to %d, stream length is %d).',
$target_offset,
$length
)
)
);
}
if ( $target_offset < 0 ) {
throw new ByteStreamException( 'Cannot seek to a negative offset' );
}
// Seeking outside of buffer range, we need a producer-specific implementation.
$this->seek_outside_of_buffer( $target_offset );
}
protected function seek_outside_of_buffer( int $target_offset ): void {
throw new ByteStreamException( 'Cannot seek outside of the buffered range' );
}
public function tell(): int {
return $this->bytes_already_forgotten + $this->offset_in_current_buffer;
}
public function reached_end_of_data(): bool {
if ( $this->is_read_closed ) {
return true;
}
if ( $this->count_consumable_bytes() > 0 ) {
return false;
}
if ( null !== $this->length() ) {
return $this->tell() >= $this->length();
}
return $this->internal_reached_end_of_data();
}
protected function internal_reached_end_of_data(): bool {
return false;
}
public function close_reading(): void {
$this->is_read_closed = true;
$this->internal_close_reading();
}
protected function internal_close_reading(): void {
}
}
@@ -0,0 +1,84 @@
<?php
namespace WordPress\ByteStream\ReadStream;
use WordPress\ByteStream\ByteStreamException;
class FileReadStream extends BaseByteReadStream {
protected $file_pointer;
public static function from_path( $file_path ) {
if ( ! file_exists( $file_path ) ) {
throw new ByteStreamException( esc_html( sprintf( 'File %s does not exist', $file_path ) ) );
}
if ( ! is_file( $file_path ) ) {
throw new ByteStreamException( esc_html( sprintf( '%s is not a file', $file_path ) ) );
}
$handle = fopen( $file_path, 'r' );
if ( ! $handle ) {
throw new ByteStreamException( esc_html( sprintf( 'Failed to open file %s', $file_path ) ) );
}
return self::from_resource( $handle, filesize( $file_path ) );
}
public static function from_resource( $handle, $expected_length = null ) {
if ( ! is_resource( $handle ) ) {
throw new ByteStreamException( 'Invalid file pointer provided' );
}
return new self( $handle, $expected_length );
}
public function __construct( $fp, $expected_length = null ) {
$this->file_pointer = $fp;
$this->expected_length = $expected_length;
}
protected function internal_pull( $n ): string {
$bytes = fread( $this->file_pointer, $n );
/**
* Workaround for a streaming bug in WordPress Playground.
*
* Without the feof() call, Playground doesn't notice when the stream reaches EOF.
* The feof() call in internal_reached_end_of_data() somehow does not trigger the
* EOF event. It must be here, right after fread().
*
* @TODO: Improve the streaming support in WordPress Playground.
*/
feof( $this->file_pointer );
if ( false === $bytes ) {
throw new ByteStreamException( 'Failed to read from file' );
}
return $bytes;
}
protected function seek_outside_of_buffer( int $target_offset ): void {
$retval = fseek( $this->file_pointer, $target_offset );
if ( -1 === $retval ) {
throw new ByteStreamException( 'Failed to seek to offset' );
}
$this->buffer = '';
$this->offset_in_current_buffer = 0;
$this->bytes_already_forgotten = $target_offset;
}
public function close_reading(): void {
if ( $this->is_read_closed ) {
return;
}
$this->is_read_closed = true;
$this->buffer = '';
if ( ! fclose( $this->file_pointer ) ) {
throw new ByteStreamException( 'Failed to close file pointer' );
}
$this->file_pointer = null;
}
protected function internal_reached_end_of_data(): bool {
return ! is_resource( $this->file_pointer ) || feof( $this->file_pointer );
}
}
@@ -0,0 +1,86 @@
<?php
namespace WordPress\ByteStream\ReadStream;
/**
* Interface for streaming, seekable byte readers.
*
* Implementations of this interface can be used to read data from
* various sources, such as files, strings, network sockets, zip files,
* parsers, etc.
*/
interface ByteReadStream {
const PULL_NO_MORE_THAN = '#pull-no-more-than';
const PULL_EXACTLY = '#pull-exactly';
/**
* Get the total length of the data stream.
*
* @return int|null The length of the data stream, or null if the length is unknown.
*/
public function length(): ?int;
/**
* Get the current position in the data stream.
*
* @return int The current byte offset in the data stream.
*/
public function tell(): int;
/**
* Seek to a specific position in the data stream.
*
* @param int $offset The byte offset to seek to.
*
* @return void
* @throws ByteStreamException If the offset is invalid.
*/
public function seek( int $offset ): void;
/**
* Check if the end of the data stream has been reached.
* At this point, next_bytes() will always return false until
* seek() is called.
*
* @return bool Whether the end of the data stream has been reached.
*/
public function reached_end_of_data(): bool;
/**
* Read the next chunk of bytes from the data stream.
*
* @return int how many bytes were pulled
*/
public function pull( ?int $n, string $mode = self::PULL_NO_MORE_THAN ): int;
/**
* Get the next $n bytes without advancing the pointer.
*
* @return string The bytes read.
*/
public function peek( int $n ): string;
/**
* Returns $n bytes and advances the pointer.
*
* @param int $n
*
* @return string
*/
public function consume( int $n ): string;
/**
* Returns all remaining bytes in the stream.
*
* @return string
*/
public function consume_all(): string;
/**
* Close the data stream.
*
* @return void
*/
public function close_reading(): void;
}
@@ -0,0 +1,9 @@
<?php
namespace WordPress\ByteStream;
use Exception;
class ByteStreamException extends Exception {
}
@@ -0,0 +1,6 @@
<?php
namespace WordPress\ByteStream;
class NotEnoughDataException extends ByteStreamException {
}