From bb9e7175104f7d657bca592e552d062d84d4685b Mon Sep 17 00:00:00 2001 From: prolic Date: Mon, 3 Dec 2012 20:33:09 +0100 Subject: [PATCH 1/2] updated send response workflow - implemented changes proposed by weierophinney - reverted changes in Console\Response and PhpEnvironment\Response objects - added Zend\Mvc\View\SendResponseListener extending the new class - added ResponseListenerFactory - Response sender triggers events --- src/PhpEnvironment/Response.php | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/PhpEnvironment/Response.php b/src/PhpEnvironment/Response.php index c1f6fad235..768d4803d8 100644 --- a/src/PhpEnvironment/Response.php +++ b/src/PhpEnvironment/Response.php @@ -29,6 +29,16 @@ class Response extends HttpResponse */ protected $version; + /** + * @var bool + */ + protected $headersSent = false; + + /** + * @var bool + */ + protected $contentSent = false; + /** * Return the HTTP version for this response * @@ -58,4 +68,89 @@ protected function detectVersion() return self::VERSION_10; } + /** + * @return bool + */ + public function headersSent() + { + return $this->headersSent; + } + + /** + * @return bool + */ + public function contentSent() + { + return $this->contentSent; + } + + /** + * Set content sent + * + * @param bool $flag + * @return Response + */ + public function setContentSent($flag) + { + $this->contentSent = (bool) $flag; + return $this; + } + + /** + * Send HTTP headers + * + * @return Response + * @deprecated + */ + public function sendHeaders() + { + if ($this->headersSent()) { + return $this; + } + + $status = $this->renderStatusLine(); + header($status); + + /** @var \Zend\Http\Header\HeaderInterface $header */ + foreach ($this->getHeaders() as $header) { + if ($header instanceof MultipleHeaderInterface) { + header($header->toString(), false); + continue; + } + header($header->toString()); + } + + $this->headersSent = true; + return $this; + } + + /** + * Send content + * + * @return Response + * @deprecated + */ + public function sendContent() + { + if ($this->contentSent()) { + return $this; + } + + echo $this->getContent(); + $this->contentSent = true; + return $this; + } + + /** + * Send HTTP response + * + * @return Response + * @deprecated + */ + public function send() + { + $this->sendHeaders() + ->sendContent(); + return $this; + } } From 4fc672210d66f11fec8a72c055f779f4439e9c23 Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 7 Dec 2012 21:21:14 +0100 Subject: [PATCH 2/2] refactored send response workflow - added SendResponseEvent - ResponseSender listen for an event - first response sender that can send the response, stops propagation - send response listener just triggers event, sends no response - SendResponseEvent tracks which response headers and content were sent - send response listener attaches default listeners (phpenvironmentresponse and consoleresponse, stream response sender is in progress) - removed AbstractResponseSender --- src/PhpEnvironment/Response.php | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/PhpEnvironment/Response.php b/src/PhpEnvironment/Response.php index 768d4803d8..188a177858 100644 --- a/src/PhpEnvironment/Response.php +++ b/src/PhpEnvironment/Response.php @@ -29,11 +29,6 @@ class Response extends HttpResponse */ protected $version; - /** - * @var bool - */ - protected $headersSent = false; - /** * @var bool */ @@ -73,29 +68,18 @@ protected function detectVersion() */ public function headersSent() { - return $this->headersSent; + return headers_sent(); } /** * @return bool + * @deprecated */ public function contentSent() { return $this->contentSent; } - /** - * Set content sent - * - * @param bool $flag - * @return Response - */ - public function setContentSent($flag) - { - $this->contentSent = (bool) $flag; - return $this; - } - /** * Send HTTP headers *