forked from clue/reactphp-zlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressor.php
More file actions
90 lines (77 loc) · 2.59 KB
/
Compressor.php
File metadata and controls
90 lines (77 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Clue\React\Zlib;
use Clue\StreamFilter as Filter;
/**
* The `Compressor` class can be used to compress a stream of data.
*
* It implements the [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface)
* and accepts uncompressed data on its writable side and emits compressed data
* on its readable side.
*
* ```php
* $encoding = ZLIB_ENCODING_GZIP; // or ZLIB_ENCODING_RAW or ZLIB_ENCODING_DEFLATE
* $compressor = new Clue\React\Zlib\Compressor($encoding);
*
* $compressor->on('data', function ($data) {
* echo $data; // compressed binary data chunk
* });
*
* $compressor->write($uncompressed); // write uncompressed data chunk
* ```
*
* This is particularly useful in a piping context:
*
* ```php
* $input->pipe($filterBadWords)->pipe($compressor)->pipe($output);
* ```
*
* For more details, see ReactPHP's
* [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).
*/
final class Compressor extends TransformStream
{
/** @var ?resource */
private $context;
/**
* @param int $encoding ZLIB_ENCODING_GZIP, ZLIB_ENCODING_RAW or ZLIB_ENCODING_DEFLATE
* @param int $level optional compression level
*/
public function __construct($encoding, $level = -1)
{
$errstr = '';
set_error_handler(function ($_, $error) use (&$errstr) {
// Match errstr from PHP's warning message.
// inflate_init(): encoding mode must be ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE
$errstr = strstr($error, ':'); // @codeCoverageIgnore
});
try {
$context = deflate_init($encoding, ['level' => $level]);
} catch (\ValueError $e) { // @codeCoverageIgnoreStart
// Throws ValueError on PHP 8.0+
restore_error_handler();
throw $e;
} // @codeCoverageIgnoreEnd
restore_error_handler();
if ($context === false) {
throw new \InvalidArgumentException('Unable to initialize compressor' . $errstr); // @codeCoverageIgnore
}
$this->context = $context;
}
protected function transformData($chunk)
{
$ret = deflate_add($this->context, $chunk, ZLIB_NO_FLUSH);
if ($ret !== '') {
$this->emit('data', [$ret]);
}
}
protected function transformEnd($chunk)
{
$ret = deflate_add($this->context, $chunk, ZLIB_FINISH);
$this->context = null;
if ($ret !== '') {
$this->emit('data', [$ret]);
}
$this->emit('end');
$this->close();
}
}