Permalink
Browse files

WIP for RFC Send Response Workflow

  • Loading branch information...
1 parent c00c516 commit bebaea6327fd92a533d3984cc99f35d706b4548f @prolic prolic committed Nov 29, 2012
@@ -30,16 +30,6 @@ class Response extends HttpResponse
protected $version;
/**
- * @var bool
- */
- protected $headersSent = false;
-
- /**
- * @var bool
- */
- protected $contentSent = false;
-
- /**
* Return the HTTP version for this response
*
* @return string
@@ -68,74 +58,4 @@ protected function detectVersion()
return self::VERSION_10;
}
- /**
- * @return bool
- */
- public function headersSent()
- {
- return $this->headersSent;
- }
-
- /**
- * @return bool
- */
- public function contentSent()
- {
- return $this->contentSent;
- }
-
- /**
- * Send HTTP headers
- *
- * @return Response
- */
- 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
- */
- public function sendContent()
- {
- if ($this->contentSent()) {
- return $this;
- }
-
- echo $this->getContent();
- $this->contentSent = true;
- return $this;
- }
-
- /**
- * Send HTTP response
- *
- * @return Response
- */
- public function send()
- {
- $this->sendHeaders()
- ->sendContent();
- return $this;
- }
}
@@ -132,6 +132,7 @@ public function bootstrap()
$events->attach($serviceManager->get('RouteListener'));
$events->attach($serviceManager->get('DispatchListener'));
$events->attach($serviceManager->get('ViewManager'));
+ $events->attach($serviceManager->get('SendResponseListener'));
// Setup MVC Event
$this->event = $event = new MvcEvent();
@@ -0,0 +1,62 @@
+<?php
+
+namespace Zend\Mvc\ResponseSender;
+
+use Zend\Stdlib\ResponseInterface;
+
+abstract class AbstractResponseSender implements ResponseSenderInterface
+{
+
+ /**
+ * @var ResponseInterface
+ */
+ protected $response;
+
+ /**
+ * @var bool
+ */
+ protected $headersSent = false;
+
+ /**
+ * @var bool
+ */
+ protected $contentSent = false;
+
+ /**
+ * Get response
+ *
+ * @return ResponseInterface
+ */
+ public function getResponse()
+ {
+ return $this->response;
+ }
+
+ /**
+ * Set response
+ *
+ * @param ResponseInterface $response
+ * @return void
+ */
+ public function setResponse(ResponseInterface $response)
+ {
+ $this->response = $response;
+ }
+
+ /**
+ * @return bool
+ */
+ public function headersSent()
+ {
+ return $this->headersSent;
+ }
+
+ /**
+ * @return bool
+ */
+ public function contentSent()
+ {
+ return $this->contentSent;
+ }
+
+}
@@ -0,0 +1,67 @@
+<?php
+
+namespace Zend\Mvc\ResponseSender;
+
+use Zend\Http\Header\MultipleHeaderInterface;
+
+class HttpResponseSender extends AbstractResponseSender
+{
+ /**
+ * Send HTTP headers
+ *
+ * @return HttpResponseSender
+ */
+ public function sendHeaders()
+ {
+ if ($this->headersSent()) {
+ return $this;
+ }
+
+ $response = $this->getResponse();
+ /* @var $response \Zend\Http\Response */
+ $status = $response->renderStatusLine();
+ header($status);
+
+ /** @var \Zend\Http\Header\HeaderInterface $header */
+ foreach ($response->getHeaders() as $header) {
+ if ($header instanceof MultipleHeaderInterface) {
+ header($header->toString(), false);
+ continue;
+ }
+ header($header->toString());
+ }
+
+ $this->headersSent = true;
+ return $this;
+ }
+
+ /**
+ * Send content
+ *
+ * @return HttpResponseSender
+ */
+ public function sendContent()
+ {
+ if ($this->contentSent()) {
+ return $this;
+ }
+
+ $response = $this->getResponse();
+ echo $response->getContent();
+ $this->contentSent = true;
+ return $this;
+ }
+
+ /**
+ * Send HTTP response
+ *
+ * @return HttpResponseSender
+ */
+ public function sendResponse()
+ {
+ $this->sendHeaders()
+ ->sendContent();
+ return $this;
+ }
+
+}
@@ -0,0 +1,31 @@
+<?php
+
+namespace Zend\Mvc\ResponseSender;
+
+use Zend\Stdlib\ResponseInterface;
+
+interface ResponseSenderInterface
+{
+
+ /**
+ * Get response
+ *
+ * @return ResponseInterface
+ */
+ public function getResponse();
+
+ /**
+ * Set response
+ *
+ * @param ResponseInterface $response
+ * @return void
+ */
+ public function setResponse(ResponseInterface $response);
+
+ /**
+ * Send the response
+ *
+ * @return void
+ */
+ public function sendResponse();
+}
@@ -8,26 +8,67 @@
* @package Zend_Mvc
*/
-namespace Zend\Mvc\View;
+namespace Zend\Mvc;
+use Traversable;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
+use Zend\Mvc\Exception;
use Zend\Mvc\MvcEvent;
+use Zend\ServiceManager\ServiceLocatorAwareInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\ResponseInterface as Response;
/**
* @category Zend
* @package Zend_Mvc
- * @subpackage View
*/
-class SendResponseListener implements ListenerAggregateInterface
+class SendResponseListener implements
+ ListenerAggregateInterface,
+ ServiceLocatorAwareInterface
{
/**
+ * @var array
+ */
+ protected $options = array();
+
+ /**
+ * @var ServiceLocatorInterface
+ */
+ protected $serviceLocator;
+
+ /**
* @var \Zend\Stdlib\CallbackHandler[]
*/
protected $listeners = array();
/**
+ * Constructor
+ *
+ * @param array $options
+ */
+ public function __construct(array $options)
+ {
+ $this->options = $options;
+ }
+
+ /**
+ * @param ServiceLocatorInterface $serviceLocator
+ */
+ public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
+ {
+ $this->serviceLocator = $serviceLocator;
+ }
+
+ /**
+ * @return ServiceLocatorInterface
+ */
+ public function getServiceLocator()
+ {
+ return $this->serviceLocator;
+ }
+
+ /**
* Attach the aggregate to the specified event manager
*
* @param EventManagerInterface $events
@@ -66,9 +107,13 @@ public function sendResponse(MvcEvent $e)
return false; // there is no response to send
}
- // send the response if possible
- if (is_callable(array($response,'send'))) {
- return $response->send();
+ $responseType = get_class($response);
+ if (!isset($this->options[$responseType])) {
+ return false; // there is no known response type to send
}
+
+ $responseSender = $this->getServiceLocator()->get($this->options[$responseType]);
+ $responseSender->setResponse($response);
+ $responseSender->sendResponse();
}
}
@@ -0,0 +1,38 @@
+<?php
+
+namespace Zend\Mvc\Service;
+
+use Zend\Mvc\SendResponseListener;
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+class SendResponseListenerFactory implements FactoryInterface
+{
+ /**
+ * @var array
+ */
+ protected $defaultOptions = array(
+ 'Zend\Http\PhpEnvironment\Response' => 'HttpResponseSender',
+ 'Zend\Console\Response' => 'ConsoleResponseSender',
+ 'Zend\Http\Response\Stream' => 'StreamResponseSender',
+ 'Zend\Http\Response' => 'HttpResponseSender',
+ );
+
+ /**
+ * Create service
+ *
+ * @param ServiceLocatorInterface $serviceLocator
+ * @return SendResponseListener
+ */
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ $config = $serviceLocator->get('Config');
+ if (isset($config['send_response_listener'])) {
+ $options = array_merge($this->defaultOptions, $config['send_response_listener']);
+ } else {
+ $options = $this->defaultOptions;
+ }
+ return new SendResponseListener($options);
+ }
+
+}
Oops, something went wrong.

0 comments on commit bebaea6

Please sign in to comment.