File size: 2,439 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

declare(strict_types=1);

namespace Presentation\Http\Message;

use Closure;
use Psr\Http\Message\StreamInterface;
use RuntimeException;

class CallbackStream implements StreamInterface
{
    private bool $called = false;
    private array $args = [];
    private ?Closure $callback = null;

    /**
     * @param Closure $callback 
     * @param array $args 
     * @return void 
     */
    public function __construct(
        Closure $callback,
        mixed ...$args
    ) {
        $this->callback = $callback;
        $this->args = $args;
    }

    /** @inheritDoc */
    public function __toString()
    {
        return $this->getContents();
    }

    /** @inheritDoc */
    public function close()
    {
        // There is nothing to do here.
    }

    /** @inheritDoc */
    public function detach()
    {
        $callback = $this->callback;
        $this->callback = null;
        return $callback;
    }

    /** @inheritDoc */
    public function getSize()
    {
        return null;
    }

    /** @inheritDoc */
    public function tell()
    {
        return 0;
    }

    /** @inheritDoc */
    public function eof()
    {
        return $this->called;
    }

    /** @inheritDoc */
    public function isSeekable()
    {
        return false;
    }

    /** @inheritDoc */
    public function seek(int $offset, int $whence = SEEK_SET)
    {
        throw new RuntimeException('Cannot seek a callback stream');
    }

    /** @inheritDoc */
    public function rewind()
    {
        throw new RuntimeException('Cannot seek a callback stream');
    }

    /** @inheritDoc */
    public function isWritable()
    {
        return false;
    }

    /** @inheritDoc */
    public function write(string $string)
    {
        throw new RuntimeException('Cannot write to a callback stream');
    }

    /** @inheritDoc */
    public function isReadable()
    {
        return true;
    }

    /** @inheritDoc */
    public function read(int $length)
    {
        return $this->getContents();
    }

    /** @inheritDoc */
    public function getContents()
    {
        if ($this->called || !$this->callback) {
            return '';
        }

        $this->called = true;
        $content = call_user_func($this->callback, ...$this->args);
        return is_string($content) ? $content : '';
    }

    /** @inheritDoc */
    public function getMetadata(?string $key = null)
    {
        return null;
    }
}