Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\HttpFoundation;
13
 
14
/**
15
 * StreamedResponse represents a streamed HTTP response.
16
 *
17
 * A StreamedResponse uses a callback for its content.
18
 *
19
 * The callback should use the standard PHP functions like echo
20
 * to stream the response back to the client. The flush() function
21
 * can also be used if needed.
22
 *
23
 * @see flush()
24
 *
25
 * @author Fabien Potencier <fabien@symfony.com>
26
 */
27
class StreamedResponse extends Response
28
{
29
    protected $callback;
30
    protected $streamed;
31
    private bool $headersSent;
32
 
33
    /**
34
     * @param int $status The HTTP status code (200 "OK" by default)
35
     */
36
    public function __construct(callable $callback = null, int $status = 200, array $headers = [])
37
    {
38
        parent::__construct(null, $status, $headers);
39
 
40
        if (null !== $callback) {
41
            $this->setCallback($callback);
42
        }
43
        $this->streamed = false;
44
        $this->headersSent = false;
45
    }
46
 
47
    /**
48
     * Sets the PHP callback associated with this Response.
49
     *
50
     * @return $this
51
     */
52
    public function setCallback(callable $callback): static
53
    {
54
        $this->callback = $callback;
55
 
56
        return $this;
57
    }
58
 
59
    /**
60
     * This method only sends the headers once.
61
     *
62
     * @return $this
63
     */
64
    public function sendHeaders(): static
65
    {
66
        if ($this->headersSent) {
67
            return $this;
68
        }
69
 
70
        $this->headersSent = true;
71
 
72
        return parent::sendHeaders();
73
    }
74
 
75
    /**
76
     * This method only sends the content once.
77
     *
78
     * @return $this
79
     */
80
    public function sendContent(): static
81
    {
82
        if ($this->streamed) {
83
            return $this;
84
        }
85
 
86
        $this->streamed = true;
87
 
88
        if (null === $this->callback) {
89
            throw new \LogicException('The Response callback must not be null.');
90
        }
91
 
92
        ($this->callback)();
93
 
94
        return $this;
95
    }
96
 
97
    /**
98
     * @return $this
99
     *
100
     * @throws \LogicException when the content is not null
101
     */
102
    public function setContent(?string $content): static
103
    {
104
        if (null !== $content) {
105
            throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
106
        }
107
 
108
        $this->streamed = true;
109
 
110
        return $this;
111
    }
112
 
113
    public function getContent(): string|false
114
    {
115
        return false;
116
    }
117
}