예제 #1
0
 /**
  * Send the response
  *
  * This method is overridden, it's purpose is to disable the exit call and instead
  * just return the error level for unit testing
  *
  * @param SendResponseEvent $event
  * @return int
  */
 public function __invoke(SendResponseEvent $event)
 {
     $response = $event->getResponse();
     if ($response instanceof Response) {
         $this->sendContent($event);
         $errorLevel = (int) $response->getMetadata('errorLevel', 0);
         $event->stopPropagation(true);
         return $errorLevel;
     }
 }
 /**
  * Send php environment response
  *
  * @param  SendResponseEvent $event
  * @return PhpEnvironmentResponseSender
  */
 public function __invoke(SendResponseEvent $event)
 {
     $response = $event->getResponse();
     if (!$response instanceof Response) {
         return $this;
     }
     $this->sendHeaders($event)->sendContent($event);
     $event->stopPropagation(true);
     return $this;
 }
 /**
  * @param SendResponseEvent $event
  * @return $this
  */
 public function __invoke(SendResponseEvent $event)
 {
     $response = $event->getResponse();
     if (!class_exists('tidy') || !$response instanceof Response || $response->getHeaders()->count() > 0 || $this->xmlHttpRequest) {
         return $this;
     }
     $this->sendHeaders($event)->sendContent($event);
     $event->stopPropagation(true);
     return $this;
 }
예제 #4
0
 /**
  * Send HTTP headers
  *
  * @param  SendResponseEvent $event
  * @return self
  */
 public function sendHeaders(SendResponseEvent $event)
 {
     if (headers_sent() || $event->headersSent()) {
         return $this;
     }
     $response = $event->getResponse();
     foreach ($response->getHeaders() as $header) {
         if ($header instanceof MultipleHeaderInterface) {
             header($header->toString(), false);
             continue;
         }
         header($header->toString());
     }
     $status = $response->renderStatusLine();
     header($status);
     $event->setHeadersSent();
     return $this;
 }
 /**
  * Processes and send response to browser
  *
  * @param SendResponseEvent $event event data
  */
 public function send(SendResponseEvent $event)
 {
     // Console //
     if ($this->response instanceof ConsoleResponse) {
         $templateContent = $this->getTemplateContent('console');
         // inject details
         $templateContent = $this->injectTemplateContent($templateContent, $event->getParams());
         $this->response->setContent($templateContent);
         $this->response->setErrorLevel(1);
         return $this->sendResponse($this->response);
     }
     // HTTP //
     $this->response->setStatusCode(500);
     $requestHeaders = $this->request->getHeaders();
     if ($requestHeaders->has('accept')) {
         $accept = $requestHeaders->get('Accept');
         $responseHeaders = $this->response->getHeaders();
         $responseHeaders->clearHeaders();
         if ($accept->match('text/html') !== false) {
             // html
             // get template content
             $templateContent = $this->getTemplateContent($event->getParam('type'));
             // inject details
             $templateContent = $this->injectTemplateContent($templateContent, $event->getParams());
             $contentType = 'text/html; charset=utf-8';
             $responseHeaders->addHeaderLine('content-type', $contentType);
             $this->response->setContent($templateContent);
         } else {
             if ($accept->match('application/json') !== false || $accept->match('application/hal+json') !== false) {
                 // json
                 // return api problem structured json
                 $contentType = 'application/problem+json; charset=utf-8';
                 $responseHeaders->addHeaderLine('content-type', $contentType);
                 // get template
                 $templateContent = $this->getTemplateContent('json');
                 // inject details
                 $templateContent = $this->injectTemplateContent($templateContent, $event->getParams());
                 $this->response->setContent($templateContent);
             }
         }
     }
     $this->sendResponse($this->response);
 }
 /**
  * Send HTTP response headers
  *
  * If an application response is composed, and is an HTTP response, merges
  * its headers with the ApiProblemResponse headers prior to sending them.
  *
  * @param  SendResponseEvent $e
  * @return self
  *
  * @throws \Zend\Http\Exception\InvalidArgumentException
  */
 public function sendHeaders(SendResponseEvent $e)
 {
     $response = $e->getResponse();
     if (!$response instanceof ApiProblemResponse) {
         return $this;
     }
     /** @var Request $request */
     $request = $this->getMvcEvent()->getRequest();
     /** @var Accept $accept */
     $accept = $request->getHeader('Accept');
     if ($accept instanceof Accept && $accept->hasMediaType('text/xml')) {
         $headers = $response->getHeaders();
         if ($headers->has('content-type')) {
             $contentTypeHeader = $headers->get('content-type');
             $headers->removeHeader($contentTypeHeader);
         }
         $headers->addHeaderLine('content-type', 'text/xml');
         if ($this->applicationResponse instanceof HttpResponse) {
             $this->mergeHeaders($this->applicationResponse, $response);
         }
     }
     return parent::sendHeaders($e);
 }
예제 #7
0
 private function nonMvcResponse($extra, $message = '')
 {
     $responseEvent = new SendResponseEvent();
     if (!empty($message)) {
         $extra = array_merge($extra, array('message' => $message));
     }
     $responseEvent->setParams($extra);
     $this->nonMvcResponseSender->send($responseEvent);
 }
 public function testContentSentAndHeadersSent()
 {
     $mockResponse = $this->getMockForAbstractClass('Zend\\Stdlib\\ResponseInterface');
     $mockResponse2 = $this->getMockForAbstractClass('Zend\\Stdlib\\ResponseInterface');
     $event = new SendResponseEvent();
     $event->setResponse($mockResponse);
     $this->assertFalse($event->headersSent());
     $this->assertFalse($event->contentSent());
     $event->setHeadersSent();
     $event->setContentSent();
     $this->assertTrue($event->headersSent());
     $this->assertTrue($event->contentSent());
     $event->setResponse($mockResponse2);
     $this->assertFalse($event->headersSent());
     $this->assertFalse($event->contentSent());
 }
예제 #9
0
 public function attachFatalErrorHandler()
 {
     // catch also fatal errors which would not show the regular error template.
     register_shutdown_function(function () {
         $error = error_get_last();
         // check we have valid error object
         if (null === $error || !isset($error['type'])) {
             return;
         }
         // allow only catchable errors
         if ($error['type'] !== E_ERROR && $error['type'] !== E_PARSE && $error['type'] !== E_RECOVERABLE_ERROR) {
             return;
         }
         $errorReference = $this->generator->generate();
         $extra = array('type' => 'FATAL', 'reference' => $errorReference, 'file' => $error['file'], 'line' => $error['line']);
         // translate error type to log type.
         $logType = Logger::ERR;
         if (isset(Logger::$errorPriorityMap[$error['type']])) {
             $logType = Logger::$errorPriorityMap[$error['type']];
         }
         // log error using logger
         $this->logger->log($logType, $error['message'], $extra);
         $responseEvent = new SendResponseEvent();
         $responseEvent->setParams(array_merge($extra, array('message' => $error['message'])));
         $this->nonMvcResponseSender->send($responseEvent);
     });
 }
예제 #10
0
 /**
  * Renders the Response object.
  *
  * @param Response $response Rcm Response Object
  *
  * @return void
  */
 protected function renderResponse(Response $response)
 {
     $sendEvent = new SendResponseEvent();
     $sendEvent->setResponse($response);
     $this->responseSender->__invoke($sendEvent);
 }