Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws;
3
 
4
use GuzzleHttp\Psr7\StreamDecoratorTrait;
5
use Psr\Http\Message\StreamInterface;
6
 
7
/**
8
 * Stream decorator that calculates a rolling hash of the stream as it is read.
9
 */
10
class HashingStream implements StreamInterface
11
{
12
    use StreamDecoratorTrait;
13
 
14
    /** @var StreamInterface */
15
    private $stream;
16
 
17
    /** @var HashInterface */
18
    private $hash;
19
 
20
    /** @var callable|null */
21
    private $callback;
22
 
23
    /**
24
     * @param StreamInterface $stream     Stream that is being read.
25
     * @param HashInterface   $hash       Hash used to calculate checksum.
26
     * @param callable        $onComplete Optional function invoked when the
27
     *                                    hash calculation is completed.
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)
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)
54
    {
55
        if ($offset === 0) {
56
            $this->hash->reset();
57
            return $this->stream->seek($offset);
58
        }
59
 
60
        // Seeking arbitrarily is not supported.
61
        return false;
62
    }
63
}