Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

RFC Send Response Workflow #3105

Merged
merged 9 commits into from
Dec 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions library/Zend/Console/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@
*/
class Response extends Message implements ResponseInterface
{

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

/**
* Check if content was sent
*
* @return bool
* @deprecated
*/
public function contentSent()
{
return $this->contentSent;
Expand Down Expand Up @@ -48,6 +58,12 @@ public function getErrorLevel()
return $this->getMetadata('errorLevel', 0);
}

/**
* Send content
*
* @return Response
* @deprecated
*/
public function sendContent()
{
if ($this->contentSent()) {
Expand All @@ -58,10 +74,14 @@ public function sendContent()
return $this;
}

/**
* @deprecated
*/
public function send()
{
$this->sendContent();
$errorLevel = (int)$this->getMetadata('errorLevel',0);
exit($errorLevel);
}

}
11 changes: 5 additions & 6 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
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this file are backwards incompatible and cannot be made in ZF2.

protected $version;

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

/**
* @var bool
*/
Expand Down Expand Up @@ -73,11 +68,12 @@ protected function detectVersion()
*/
public function headersSent()
{
return $this->headersSent;
return headers_sent();
}

/**
* @return bool
* @deprecated
*/
public function contentSent()
{
Expand All @@ -88,6 +84,7 @@ public function contentSent()
* Send HTTP headers
*
* @return Response
* @deprecated
*/
public function sendHeaders()
{
Expand Down Expand Up @@ -115,6 +112,7 @@ public function sendHeaders()
* Send content
*
* @return Response
* @deprecated
*/
public function sendContent()
{
Expand All @@ -131,6 +129,7 @@ public function sendContent()
* Send HTTP response
*
* @return Response
* @deprecated
*/
public function send()
{
Expand Down
1 change: 1 addition & 0 deletions library/Zend/Mvc/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
56 changes: 56 additions & 0 deletions library/Zend/Mvc/ResponseSender/ConsoleResponseSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace Zend\Mvc\ResponseSender;

use Zend\Console\Response;

/**
* @category Zend
* @package Zend_Mvc
* @subpackage ResponseSender
*/
class ConsoleResponseSender implements ResponseSenderInterface
{
/**
* Send content
*
* @param SendResponseEvent $event
* @return ConsoleResponseSender
*/
public function sendContent(SendResponseEvent $event)
{
if ($event->contentSent()) {
return $this;
}
$response = $event->getResponse();
echo $response->getContent();
$event->setContentSent();
return $this;
}

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

}
84 changes: 84 additions & 0 deletions library/Zend/Mvc/ResponseSender/PhpEnvironmentResponseSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace Zend\Mvc\ResponseSender;

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

/**
* @category Zend
* @package Zend_Mvc
* @subpackage ResponseSender
*/
class PhpEnvironmentResponseSender implements ResponseSenderInterface
{
/**
* Send HTTP headers
*
* @param SendResponseEvent $event
* @return PhpEnvironmentResponseSender
*/
public function sendHeaders(SendResponseEvent $event)
{
$response = $event->getResponse();
if ($response->headersSent() || $event->headersSent()) {
return $this;
}
$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());
}
$event->setHeadersSent();
return $this;
}

/**
* Send content
*
* @param SendResponseEvent $event
* @return PhpEnvironmentResponseSender
*/
public function sendContent(SendResponseEvent $event)
{
if ($event->contentSent()) {
return $this;
}
$response = $event->getResponse();
echo $response->getContent();
$event->setContentSent();
return $this;
}

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

}
30 changes: 30 additions & 0 deletions library/Zend/Mvc/ResponseSender/ResponseSenderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace Zend\Mvc\ResponseSender;

use Zend\Mvc\ResponseSender\SendResponseEvent;;

/**
* @category Zend
* @package Zend_Mvc
* @subpackage ResponseSender
*/
interface ResponseSenderInterface
{
/**
* Send the response
*
* @param SendResponseEvent $event
* @return void
*/
public function __invoke(SendResponseEvent $event);

}
Loading