|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Stream\Hash; |
|
4
|
|
|
|
|
5
|
|
|
use Evenement\EventEmitterTrait; |
|
6
|
|
|
use React\Stream\ReadableStreamInterface; |
|
7
|
|
|
use React\Stream\Util; |
|
8
|
|
|
use React\Stream\WritableStreamInterface; |
|
9
|
|
|
|
|
10
|
|
|
final class ReadableStreamHash implements ReadableStreamInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use EventEmitterTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var WritableStreamInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $stream; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var resource |
|
21
|
|
|
*/ |
|
22
|
|
|
private $context; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* WritableStreamHash constructor. |
|
26
|
|
|
* @param WritableStreamInterface $stream |
|
27
|
|
|
*/ |
|
28
|
184 |
|
public function __construct(ReadableStreamInterface $stream, string $hash, int $options = 0, string $key = '') |
|
29
|
|
|
{ |
|
30
|
184 |
|
$this->stream = $stream; |
|
|
|
|
|
|
31
|
184 |
|
$this->context = hash_init($hash, $options, $key); |
|
32
|
|
|
$this->stream->on('data', function ($data) { |
|
33
|
184 |
|
$this->emit('data', [$data]); |
|
34
|
184 |
|
hash_update($this->context, $data); |
|
35
|
184 |
|
}); |
|
36
|
184 |
|
$this->stream->on('close', function () { |
|
37
|
184 |
|
$this->emit('close'); |
|
38
|
184 |
|
$this->emit('hash', [ |
|
39
|
184 |
|
hash_final($this->context), |
|
40
|
|
|
]); |
|
41
|
184 |
|
}); |
|
42
|
184 |
|
Util::forwardEvents($stream, $this, ['error', 'end']); |
|
43
|
184 |
|
} |
|
44
|
|
|
|
|
45
|
184 |
|
public function isReadable() |
|
46
|
|
|
{ |
|
47
|
184 |
|
return $this->stream->isReadable(); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function pause() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->stream->pause(); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function resume() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->stream->resume(); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function pipe(WritableStreamInterface $dest, array $options = []) |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->stream->pipe($dest, $options); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function close() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->stream->close(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..