Skip to content

Commit

Permalink
Merge pull request #384 from php-vcr/383-backport-fix-code-transforma…
Browse files Browse the repository at this point in the history
…tion-on-streams-which-are-divided-into-several-chunks

FIX: Read the whole stream and perform code transformations
  • Loading branch information
higidi committed Dec 27, 2022
2 parents 0425495 + df90936 commit fde2ab6
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 fde2ab6

Please sign in to comment.