Пример #1
0
 /**
  * NotFound invoke.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return \Psr\Http\Message\ResponseInterface
  * @throws \Unicorn\Http\RuntimeException
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     $contentType = $this->getContentType($request, $response);
     error_log('[NotFound] content type: ' . var_export($contentType, true));
     switch ($contentType) {
         case 'application/json':
             $output = $this->renderJsonOutput($request, $response);
             break;
         case 'text/xml':
         case 'application/xml':
             $output = $this->renderXmlOutput($request, $response);
             break;
         case 'text/html':
             $output = $this->renderHtmlOutput($request, $response);
             break;
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(200)->withHeader('Content-Type', $contentType)->withBody($body);
 }
Пример #2
0
 /**
  * Error invoke.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param Exception $ex
  * @return \Psr\Http\Message\ResponseInterface
  * @throws \Unicorn\Http\RuntimeException
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $ex)
 {
     $this->exception = $ex;
     $contentType = $this->getContentType($request, $response);
     error_log('[Error] content type: ' . $contentType);
     switch ($contentType) {
         case 'application/json':
             $output = $this->renderJsonOutput($request, $response);
             break;
         case 'text/xml':
         case 'application/xml':
             $output = $this->renderXmlOutput($request, $response);
             break;
         case 'text/html':
             $output = $this->renderHtmlOutput($request, $response);
             break;
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(200)->withHeader('Content-Type', $contentType . ';charset=utf-8')->withBody($body);
 }