Skip to content

Commit 7efdf1c

Browse files
committed
Add FilterStream trait
1 parent 2fb1946 commit 7efdf1c

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/Streams/FilterStream.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of Aplus Framework CLI Library.
4+
*
5+
* (c) Natan Felles <natanfelles@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Framework\CLI\Streams;
11+
12+
use JetBrains\PhpStorm\Pure;
13+
14+
/**
15+
* Trait FilterStream.
16+
*
17+
* @package cli
18+
*
19+
* @since 2.4
20+
*/
21+
trait FilterStream
22+
{
23+
protected static string $contents = '';
24+
25+
/**
26+
* @param resource $in
27+
* @param resource $out
28+
* @param int $consumed
29+
* @param bool $closing
30+
*
31+
* @see https://php.net/manual/en/php-user-filter.filter.php
32+
*
33+
* @return int
34+
*/
35+
public function filter($in, $out, &$consumed, $closing) : int
36+
{
37+
while ($bucket = \stream_bucket_make_writeable($in)) {
38+
static::$contents .= $bucket->data;
39+
$consumed += $bucket->datalen;
40+
\stream_bucket_append($out, $bucket);
41+
}
42+
return \PSFS_FEED_ME;
43+
}
44+
45+
#[Pure]
46+
public static function getContents() : string
47+
{
48+
return static::$contents;
49+
}
50+
51+
public static function reset() : void
52+
{
53+
static::$contents = '';
54+
}
55+
}

0 commit comments

Comments
 (0)