xp-framework / core

The XP Framework is an all-purpose, object oriented PHP framework.
Other
19 stars 6 forks source link

Implement a limited in-memory buffer with filesystem overflow #343

Closed thekid closed 2 months ago

thekid commented 2 months ago

Example:

use io\streams\Buffer;
use lang\Environment;

$buffer= new Buffer(Environment::tempDir(), 1024);

// This will be written to memory as it's shorter than the threshold
$buffer->write('Slice #1');

// With this write, the buffer exceeds the threshold and is thus saved to a file
$buffer->write(str_repeat('*', 2048)); 

// Drain the buffer
while ($buffer->available()) {
  $chunk= $buffer->read();
}

// Close the buffer, removing the file created by the second write
$buffer->close();

Inspired by https://github.com/xp-forge/web/issues/118

thekid commented 2 months ago

Released in https://github.com/xp-framework/core/releases/tag/v12.1.0