| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use GuzzleHttp\Psr7\StreamDecoratorTrait; |
| 5 |
use Psr\Http\Message\StreamInterface; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class HashingStream implements StreamInterface |
| 11 |
{ |
| 12 |
use StreamDecoratorTrait; |
| 13 |
|
| 14 |
|
| 15 |
private $stream; |
| 16 |
|
| 17 |
|
| 18 |
private $hash; |
| 19 |
|
| 20 |
|
| 21 |
private $callback; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct( |
| 30 |
StreamInterface $stream, |
| 31 |
HashInterface $hash, |
| 32 |
?callable $onComplete = null |
| 33 |
) { |
| 34 |
$this->stream = $stream; |
| 35 |
$this->hash = $hash; |
| 36 |
$this->callback = $onComplete; |
| 37 |
} |
| 38 |
|
| 39 |
public function read($length): string |
| 40 |
{ |
| 41 |
$data = $this->stream->read($length); |
| 42 |
$this->hash->update($data); |
| 43 |
if ($this->eof()) { |
| 44 |
$result = $this->hash->complete(); |
| 45 |
if ($this->callback) { |
| 46 |
call_user_func($this->callback, $result); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return $data; |
| 51 |
} |
| 52 |
|
| 53 |
public function seek($offset, $whence = SEEK_SET): void |
| 54 |
{ |
| 55 |
|
| 56 |
if ($offset !== 0) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$this->hash->reset(); |
| 61 |
$this->stream->seek($offset); |
| 62 |
} |
| 63 |
} |