Skip to content

Commit 473741b

Browse files
committed
added the possibility to change a StreamedResponse callback after its creation
1 parent 8717d44 commit 473741b

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,27 @@ public function render($view, array $parameters = array(), Response $response =
102102
/**
103103
* Streams a view.
104104
*
105-
* @param string $view The view name
106-
* @param array $parameters An array of parameters to pass to the view
107-
* @param Response $response A response instance
105+
* @param string $view The view name
106+
* @param array $parameters An array of parameters to pass to the view
107+
* @param StreamedResponse $response A response instance
108108
*
109109
* @return StreamedResponse A StreamedResponse instance
110110
*/
111-
public function stream($view, array $parameters = array(), Response $response = null)
111+
public function stream($view, array $parameters = array(), StreamedResponse $response = null)
112112
{
113113
$templating = $this->container->get('templating');
114114

115-
return new StreamedResponse(function () use ($templating, $view, $parameters) {
115+
$callback = function () use ($templating, $view, $parameters) {
116116
$templating->stream($view, $parameters);
117-
}, null === $response ? 200 : $response->getStatusCode(), null === $response ? array() : $response->headers->all());
117+
};
118+
119+
if (null === $response) {
120+
return new StreamedResponse($callback);
121+
}
122+
123+
$response->setCallback($callback);
124+
125+
return $response;
118126
}
119127

120128
/**

src/Symfony/Component/HttpFoundation/StreamedResponse.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,27 @@ class StreamedResponse extends Response
4040
*
4141
* @api
4242
*/
43-
public function __construct($callback, $status = 200, $headers = array())
43+
public function __construct($callback = null, $status = 200, $headers = array())
4444
{
4545
parent::__construct(null, $status, $headers);
4646

47+
if (null !== $callback) {
48+
$this->setCallback($callback);
49+
}
50+
$this->streamed = false;
51+
}
52+
53+
/**
54+
* Sets the PHP callback associated with this Response.
55+
*
56+
* @param mixed $callback A valid PHP callback
57+
*/
58+
public function setCallback($callback)
59+
{
4760
$this->callback = $callback;
4861
if (!is_callable($this->callback)) {
4962
throw new \LogicException('The Response callback must be a valid PHP callable.');
5063
}
51-
52-
$this->streamed = false;
5364
}
5465

5566
/**
@@ -80,6 +91,10 @@ public function sendContent()
8091

8192
$this->streamed = true;
8293

94+
if (null === $this->callback) {
95+
throw new \LogicException('The Response callback must not be null.');
96+
}
97+
8398
call_user_func($this->callback);
8499
}
85100

0 commit comments

Comments
 (0)