Skip to content

Commit

Permalink
refactored send response workflow
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
prolic committed Dec 7, 2012
1 parent e01df53 commit 011ecde
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 185 deletions.
20 changes: 2 additions & 18 deletions library/Zend/Http/PhpEnvironment/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ class Response extends HttpResponse
*/
protected $version;

/**
* @var bool
*/
protected $headersSent = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -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
*
Expand Down
82 changes: 0 additions & 82 deletions library/Zend/Mvc/ResponseSender/AbstractResponseSender.php

This file was deleted.

30 changes: 17 additions & 13 deletions library/Zend/Mvc/ResponseSender/ConsoleResponseSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@

namespace Zend\Mvc\ResponseSender;

class ConsoleResponseSender extends AbstractResponseSender
use Zend\Console\Response;

class ConsoleResponseSender implements ResponseSenderInterface
{
/**
* Send content
*
* @param SendResponseEvent $event
* @return ConsoleResponseSender
*/
public function sendContent()
public function sendContent(SendResponseEvent $event)
{
$this->getEventManager()->trigger(self::EVENT_SEND_CONTENT, $this);
$response = $this->getResponse();
/* @var $response \Zend\Console\Response */
if ($response->contentSent()) {
if ($event->contentSent()) {
return $this;
}
$response = $event->getResponse();
echo $response->getContent();
$response->setContentSent(true);
$event->setContentSent();
return $this;
}

/**
* Send the response
*
* @param SendResponseEvent $event
* @return void
*/
public function sendResponse()
public function __invoke(SendResponseEvent $event)
{
$this->getEventManager()->trigger(self::EVENT_SEND_RESPONSE, $this);
$this->sendContent();
$response = $this->getResponse();
$errorLevel = (int)$response->getMetadata('errorLevel',0);
exit($errorLevel);
$response = $event->getResponse();
if ($response instanceof Response) {
$this->sendContent($response);
$errorLevel = (int) $response->getMetadata('errorLevel',0);
$event->stopPropagation(true);
exit($errorLevel);
}
}

}
42 changes: 21 additions & 21 deletions library/Zend/Mvc/ResponseSender/PhpEnvironmentResponseSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

namespace Zend\Mvc\ResponseSender;

use Zend\Mvc\ResponseSender\SendResponseEvent;
use Zend\Http\Header\MultipleHeaderInterface;
use Zend\Http\PhpEnvironment\Response;

class PhpEnvironmentResponseSender extends AbstractResponseSender
class PhpEnvironmentResponseSender implements ResponseSenderInterface
{

/**
* Send HTTP headers
*
* @triggers sendHeaders
* @param SendResponseEvent $event
* @return PhpEnvironmentResponseSender
*/
public function sendHeaders()
public function sendHeaders(SendResponseEvent $event)
{
$this->getEventManager()->trigger(self::EVENT_SEND_HEADERS, $this);
$response = $this->getResponse();
/* @var $response \Zend\Http\PhpEnvironment\Response */

if ($response->headersSent()) {
$response = $event->getResponse();
if ($response->headersSent() || $event->headersSent()) {
return $this;
}
$status = $response->renderStatusLine();
Expand All @@ -32,39 +30,41 @@ public function sendHeaders()
}
header($header->toString());
}
$event->setHeadersSent();
return $this;
}

/**
* Send content
*
* @triggers sendContent
* @param SendResponseEvent $event
* @return PhpEnvironmentResponseSender
*/
public function sendContent()
public function sendContent(SendResponseEvent $event)
{
$this->getEventManager()->trigger(self::EVENT_SEND_CONTENT, $this);
$response = $this->getResponse();
/* @var $response \Zend\Http\PhpEnvironment\Response */
if ($response->contentSent()) {
if ($event->contentSent()) {
return $this;
}

$response = $event->getResponse();
echo $response->getContent();
$response->setContentSent(true);
$event->setContentSent();
return $this;
}

/**
* Send HTTP response
*
* @param SendResponseEvent $event
* @return PhpEnvironmentResponseSender
*/
public function sendResponse()
public function __invoke(SendResponseEvent $event)
{
$this->getEventManager()->trigger(self::EVENT_SEND_RESPONSE, $this);
$this->sendHeaders()
->sendContent();
$response = $event->getResponse();
if ($response instanceof Response) {
$this->sendHeaders($event)
->sendContent($event);
$event->stopPropagation(true);
}
return $this;
}

Expand Down
23 changes: 4 additions & 19 deletions library/Zend/Mvc/ResponseSender/ResponseSenderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,16 @@

namespace Zend\Mvc\ResponseSender;

use Zend\EventManager\EventManagerAwareInterface;
use Zend\Stdlib\ResponseInterface;
use Zend\Mvc\ResponseSender\SendResponseEvent;;

interface ResponseSenderInterface extends EventManagerAwareInterface
interface ResponseSenderInterface
{
/**
* Get response
*
* @return ResponseInterface
*/
public function getResponse();

/**
* Set response
*
* @param ResponseInterface $response
* @return void
*/
public function setResponse(ResponseInterface $response);

/**
* Send the response
*
* @param SendResponseEvent $event
* @return void
*/
public function sendResponse();
public function __invoke(SendResponseEvent $event);

}
Loading

0 comments on commit 011ecde

Please sign in to comment.