Skip to content

Commit

Permalink
FIX: Read the whole stream and perform code transformations
Browse files Browse the repository at this point in the history
First read the whole stream and afterwards perform code transformations.
This will ensure to also replace code, which is divided into chunks while
reading a file.

Fixes #268 and #383

(cherry picked from commit 1917d66)
  • Loading branch information
higidi committed Dec 27, 2022
1 parent 0425495 commit df90936
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/VCR/CodeTransform/AbstractCodeTransform.php
Expand Up @@ -12,14 +12,20 @@ abstract class AbstractCodeTransform extends \php_user_filter
{
public const NAME = 'vcr_abstract_filter';

/** @var string */
private $data = '';

/**
* Attaches the current filter to a stream.
*/
public function register(): void
{
if (!\in_array(static::NAME, stream_get_filters(), true)) {
$isRegistered = stream_filter_register(static::NAME, static::class);
Assertion::true($isRegistered, sprintf('Failed registering stream filter "%s" on stream "%s"', static::class, static::NAME));
Assertion::true(
$isRegistered,
sprintf('Failed registering stream filter "%s" on stream "%s"', static::class, static::NAME)
);
}
}

Expand All @@ -37,12 +43,19 @@ public function register(): void
*/
public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = $this->transformCode($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
while ($buffer = stream_bucket_make_writeable($in)) {
$this->data .= $buffer->data;
$consumed += $buffer->datalen;
}

if (!$closing) {
return \PSFS_FEED_ME;
}

$bucket = stream_bucket_new($this->stream, $this->transformCode($this->data));
$this->data = '';
stream_bucket_append($out, $bucket);

return \PSFS_PASS_ON;
}

Expand Down

0 comments on commit df90936

Please sign in to comment.