Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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 $headersSent;
32
 
33
    public function __construct(callable $callback = null, int $status = 200, array $headers = [])
34
    {
35
        parent::__construct(null, $status, $headers);
36
 
37
        if (null !== $callback) {
38
            $this->setCallback($callback);
39
        }
40
        $this->streamed = false;
41
        $this->headersSent = false;
42
    }
43
 
44
    /**
45
     * Factory method for chainability.
46
     *
47
     * @param callable|null $callback A valid PHP callback or null to set it later
48
     *
49
     * @return static
50
     *
51
     * @deprecated since Symfony 5.1, use __construct() instead.
52
     */
53
    public static function create($callback = null, int $status = 200, array $headers = [])
54
    {
55
        trigger_deprecation('symfony/http-foundation', '5.1', 'The "%s()" method is deprecated, use "new %s()" instead.', __METHOD__, \get_called_class());
56
 
57
        return new static($callback, $status, $headers);
58
    }
59
 
60
    /**
61
     * Sets the PHP callback associated with this Response.
62
     *
63
     * @return $this
64
     */
65
    public function setCallback(callable $callback)
66
    {
67
        $this->callback = $callback;
68
 
69
        return $this;
70
    }
71
 
72
    /**
73
     * {@inheritdoc}
74
     *
75
     * This method only sends the headers once.
76
     *
77
     * @return $this
78
     */
79
    public function sendHeaders()
80
    {
81
        if ($this->headersSent) {
82
            return $this;
83
        }
84
 
85
        $this->headersSent = true;
86
 
87
        return parent::sendHeaders();
88
    }
89
 
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * This method only sends the content once.
94
     *
95
     * @return $this
96
     */
97
    public function sendContent()
98
    {
99
        if ($this->streamed) {
100
            return $this;
101
        }
102
 
103
        $this->streamed = true;
104
 
105
        if (null === $this->callback) {
106
            throw new \LogicException('The Response callback must not be null.');
107
        }
108
 
109
        ($this->callback)();
110
 
111
        return $this;
112
    }
113
 
114
    /**
115
     * {@inheritdoc}
116
     *
117
     * @throws \LogicException when the content is not null
118
     *
119
     * @return $this
120
     */
121
    public function setContent(?string $content)
122
    {
123
        if (null !== $content) {
124
            throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
125
        }
126
 
127
        $this->streamed = true;
128
 
129
        return $this;
130
    }
131
 
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getContent()
136
    {
137
        return false;
138
    }
139
}