/** * Send the response content * * Sets the composed ApiProblem's flag for including the stack trace in the * detail based on the display exceptions flag, and then sends content. * * @param SendResponseEvent $e * @return self * * @throws \Zend\Config\Exception\RuntimeException */ public function sendContent(SendResponseEvent $e) { $response = $e->getResponse(); if (!$response instanceof ApiProblemResponse) { return $this; } $response->getApiProblem()->setDetailIncludesStackTrace($this->displayExceptions()); /** @var Request $request */ $request = $this->getMvcEvent()->getRequest(); /** @var Accept $accept */ $accept = $request->getHeader('Accept'); if ($accept instanceof Accept && $accept->hasMediaType('text/xml')) { $arrayResponse = $response->getApiProblem()->toArray(); $xmlWriter = new XmlWriter(); if (array_key_exists('trace', $arrayResponse)) { array_walk($arrayResponse['trace'], function (&$item) { unset($item['args']); }); } if (array_key_exists('exception_stack', $arrayResponse)) { array_walk($arrayResponse['exception_stack'], function (&$item) { array_walk($item['trace'], function (&$trace) { unset($trace['args']); }); }); } $output = $xmlWriter->processConfig($arrayResponse); echo $output; $e->setContentSent(); return $this; } return parent::sendHeaders($e); }
/** * @param ViewEvent $e * * @return null * * @throws \Zend\Config\Exception\RuntimeException * @throws \Zend\Http\Exception\InvalidArgumentException */ public function injectResponse(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof ApiProblemModel) { // Model is not an ApiProblemModel; we cannot handle it here return null; } /** @var Request $request */ $request = $e->getRequest(); /** @var Accept $accept */ $accept = $request->getHeader('Accept'); if (!($accept instanceof Accept && $accept->hasMediaType('text/xml'))) { return null; } $problem = $model->getApiProblem(); $statusCode = $this->getStatusCodeFromApiProblem($problem); $contentType = 'text/xml'; /** @var Response $response */ $response = $e->getResponse(); $problemData = $problem->toArray(); $xmlWriter = new XmlWriter(); $output = $xmlWriter->processConfig($problemData); $response->setStatusCode($statusCode); $response->setContent($output); $headers = $response->getHeaders(); $headers->addHeaderLine('Content-Type', $contentType); }