protected function getNotFoundResponse(RequestInterface $request, ResponseInterface $response, \Exception $exception)
 {
     $output = json_encode(['error_message' => $exception->getMessage()], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(404)->withHeader('Content-type', 'application/json')->withBody($body);
 }
Example #2
0
 private function handleError(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
 {
     $output = $this->renderJsonErrorMessage($exception);
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(500)->withHeader('Content-type', 'application/json')->withBody($body);
 }
Example #3
0
 /**
  * Invoke error handler
  *
  * @param  ServerRequestInterface $request  The most recent Request object
  * @param  ResponseInterface      $response The most recent Response object
  * @param  string[]               $methods  Allowed HTTP methods
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
 {
     if ($request->getMethod() === 'OPTIONS') {
         $status = 200;
         $contentType = 'text/plain';
         $output = $this->renderPlainNotAllowedMessage($methods);
     } else {
         $status = 405;
         $contentType = $this->determineContentType($request);
         switch ($contentType) {
             case 'application/json':
                 $output = $this->renderJsonNotAllowedMessage($methods);
                 break;
             case 'text/xml':
             case 'application/xml':
                 $output = $this->renderXmlNotAllowedMessage($methods);
                 break;
             case 'text/html':
                 $output = $this->renderHtmlNotAllowedMessage($methods);
                 break;
         }
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     $allow = implode(', ', $methods);
     return $response->withStatus($status)->withHeader('Content-type', $contentType)->withHeader('Allow', $allow)->withBody($body);
 }
Example #4
0
 /**
  * Invoca la respuesta
  * 
  * @param ServerRequestInterface $request  Instancia de ServerRequestInterface
  * @param ResponseInterface      $response Instancia de ResponseInterface
  * @param array                  $methods  Array de metodos http disponibles
  * 
  * @return mixed
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods = [])
 {
     $output = $this->render($request, $methods);
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(405)->withHeader('Content-type', 'application/json')->withBody($body);
 }
Example #5
0
 /**
  * Invoca la respuesta
  *
  * @param ServerRequestInterface $request  Instancia de ServerRequestInterface
  * @param ResponseInterface      $response Instancia de ResponseInterface
  * @param \Throwable             $error    Instancia de Throwable
  *
  * @return mixed
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Throwable $error)
 {
     $output = $this->render($error);
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(500)->withHeader('Content-type', 'application/json')->withBody($body);
 }
Example #6
0
 /**
  * Invoke "Maintenance" Handler
  *
  * @param  ServerRequestInterface $request  The most recent Request object.
  * @param  ResponseInterface      $response The most recent Response object.
  * @param  string[]               $methods  Allowed HTTP methods.
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
 {
     $this->setMethods($methods);
     if ($request->getMethod() === 'OPTIONS') {
         $contentType = 'text/plain';
         $output = $this->renderPlainOutput();
     } else {
         $contentType = $this->determineContentType($request);
         switch ($contentType) {
             case 'application/json':
                 $output = $this->renderJsonOutput();
                 break;
             case 'text/xml':
             case 'application/xml':
                 $output = $this->renderXmlOutput();
                 break;
             case 'text/html':
             default:
                 $output = $this->renderHtmlOutput();
                 break;
         }
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(503)->withHeader('Content-type', $contentType)->withBody($body);
 }
Example #7
0
 /**
  * @param string                                   $filename
  * @param \Psr\Http\Message\ServerRequestInterface $request
  * @param \Psr\Http\Message\ResponseInterface      $response
  *
  * @return \Psr\Http\Message\ResponseInterface
  * @throws \livetyping\hermitage\foundation\exceptions\ImageNotFoundException
  */
 public function __invoke(string $filename, Request $request, Response $response) : Response
 {
     $this->prepare($filename);
     $image = $this->storage->get($filename);
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($image->getBinary());
     return $response->withHeader('Content-Type', $image->getMimeType())->withBody($body);
 }
    /**
     * Invoke not found handler
     *
     * @param  ServerRequestInterface $request  The most recent Request object
     * @param  ResponseInterface      $response The most recent Response object
     *
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
    {
        $contentType = $this->determineContentType($request->getHeaderLine('Accept'));
        switch ($contentType) {
            case 'application/json':
                $output = '{"message":"Not found"}';
                break;
            case 'text/xml':
            case 'application/xml':
                $output = '<root><message>Not found</message></root>';
                break;
            case 'text/html':
            default:
                $homeUrl = (string) $request->getUri()->withPath('')->withQuery('')->withFragment('');
                $contentType = 'text/html';
                $output = <<<END
<html>
    <head>
        <title>Page Not Found</title>
        <style>
            body{
                margin:0;
                padding:30px;
                font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;
            }
            h1{
                margin:0;
                font-size:48px;
                font-weight:normal;
                line-height:48px;
            }
            strong{
                display:inline-block;
                width:65px;
            }
        </style>
    </head>
    <body>
        <h1>Page Not Found</h1>
        <p>
            The page you are looking for could not be found. Check the address bar
            to ensure your URL is spelled correctly. If all else fails, you can
            visit our home page at the link below.
        </p>
        <a href='{$homeUrl}'>Visit the Home Page</a>
    </body>
</html>
END;
                break;
        }
        $body = new Body(fopen('php://temp', 'r+'));
        $body->write($output);
        return $response->withStatus(404)->withHeader('Content-Type', $contentType)->withBody($body);
    }
Example #9
0
    /**
     * Invoke not found handler
     *
     * @param  ServerRequestInterface $request  The most recent Request object
     * @param  ResponseInterface      $response The most recent Response object
     *
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
    {
        $contentType = $this->determineContentType($request);
        switch ($contentType) {
            case 'application/json':
                $output = '{"message":"Not found"}';
                break;
            case 'text/xml':
            case 'application/xml':
                $output = '<root><message>Not found</message></root>';
                break;
            case 'text/html':
                $contentType = 'text/html';
                $output = <<<END
<!DOCTYPE html>
<html>
    <head>
        <title>Page Not Found</title>
        <style>
            body{
                margin:0;
                padding:30px;
                font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;
            }
            h1{
                margin:0;
                font-size:36px;
                font-weight:normal;
                line-height:36px;
            }
            strong{
                display:inline-block;
                width:65px;
            }
        </style>
    </head>
    <body>
        <h1>Page Not Found</h1>
        <p>
            The page you are looking for could not be found. Check the address bar
            to ensure your URL is spelled correctly. If all else fails, you can
            go to our website's home page, and navigate to the content in question.
        </p>
    </body>
</html>
END;
                break;
        }
        $body = new Body(fopen('php://temp', 'r+'));
        $body->write($output);
        return $response->withStatus(404)->withHeader('Content-Type', $contentType)->withBody($body);
    }
Example #10
0
    /**
     * Invoke error handler
     *
     * @param  ServerRequestInterface $request  The most recent Request object
     * @param  ResponseInterface      $response The most recent Response object
     * @param  string[]               $methods  Allowed HTTP methods
     *
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
    {
        $allow = implode(', ', $methods);
        if ($request->getMethod() === 'OPTIONS') {
            $status = 200;
            $contentType = 'text/plain';
            $output = 'Allowed methods: ' . $allow;
        } else {
            $status = 405;
            $contentType = $this->determineContentType($request);
            switch ($contentType) {
                case 'application/json':
                    $output = '{"message":"Method not allowed. Must be one of: ' . $allow . '"}';
                    break;
                case 'text/xml':
                case 'application/xml':
                    $output = "<root><message>Method not allowed. Must be one of: {$allow}</message></root>";
                    break;
                case 'text/html':
                    $contentType = 'text/html';
                    $output = <<<END
<!DOCTYPE html>
<html>
    <head>
        <title>Method not allowed</title>
        <style>
            body{
                margin:0;
                padding:30px;
                font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;
            }
            h1{
                margin:0;
                font-size:36px;
                font-weight:normal;
                line-height:36px;
            }
        </style>
    </head>
    <body>
        <h1>Method not allowed</h1>
        <p>Method not allowed. Must be one of: <strong>{$allow}</strong></p>
    </body>
</html>
END;
                    break;
            }
        }
        $body = new Body(fopen('php://temp', 'r+'));
        $body->write($output);
        return $response->withStatus($status)->withHeader('Content-type', $contentType)->withHeader('Allow', $allow)->withBody($body);
    }
Example #11
0
 /**
  * Invoke error handler
  *
  * @param ServerRequestInterface $request   The most recent Request object
  * @param ResponseInterface      $response  The most recent Response object
  * @param Exception              $exception The caught Exception object
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
 {
     $title = 'Slim Application Error';
     $html = '<p>The application could not run because of the following error:</p>';
     $html .= '<h2>Details</h2>';
     $html .= $this->renderException($exception);
     while ($exception = $exception->getPrevious()) {
         $html .= '<h2>Previous exception</h2>';
         $html .= $this->renderException($exception);
     }
     $output = sprintf("<html><head><title>%s</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana," . "sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;" . "width:65px;}</style></head><body><h1>%s</h1>%s</body></html>", $title, $title, $html);
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(500)->withHeader('Content-type', 'text/html')->withBody($body);
 }
Example #12
0
 /**
  * Invoke error handler
  *
  * @param  ServerRequestInterface $request  The most recent Request object
  * @param  ResponseInterface      $response The most recent Response object
  * @param  string[]               $methods  Allowed HTTP methods
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $methods)
 {
     $allow = implode(', ', $methods);
     $body = new Body(fopen('php://temp', 'r+'));
     if ($request->getMethod() === 'OPTIONS') {
         $status = 200;
         $contentType = 'text/plain';
         $body->write('Allowed methods: ' . $allow);
     } else {
         $status = 405;
         $contentType = 'text/html';
         $body->write('<p>Method not allowed. Must be one of: ' . $allow . '</p>');
     }
     return $response->withStatus($status)->withHeader('Content-type', $contentType)->withHeader('Allow', $allow)->withBody($body);
 }
 /**
  * Invoke not found handler
  *
  * @param  ServerRequestInterface $request  The most recent Request object
  * @param  ResponseInterface      $response The most recent Response object
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     $contentType = $this->determineContentType($request);
     switch ($contentType) {
         case 'application/json':
             $output = $this->renderJsonNotFoundOutput($request, $response);
             break;
         case 'text/xml':
         case 'application/xml':
             $output = $this->renderXmlNotFoundOutput($request, $response);
             break;
         case 'text/html':
             $output = $this->renderHtmlNotFoundOutput($request, $response);
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(404)->withHeader('Content-Type', $contentType)->withBody($body);
 }
 /**
  * Invoke error handler
  *
  * @param ServerRequestInterface $request   The most recent Request object
  * @param ResponseInterface      $response  The most recent Response object
  * @param \Throwable             $error     The caught Throwable object
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Throwable $error)
 {
     $contentType = $this->determineContentType($request);
     switch ($contentType) {
         case 'application/json':
             $output = $this->renderJsonErrorMessage($error);
             break;
         case 'text/xml':
         case 'application/xml':
             $output = $this->renderXmlErrorMessage($error);
             break;
         case 'text/html':
             $output = $this->renderHtmlErrorMessage($error);
             break;
     }
     $this->writeToErrorLog($error);
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(500)->withHeader('Content-type', $contentType)->withBody($body);
 }
Example #15
0
 /**
  * Invoke error handler
  *
  * @param ServerRequestInterface $request   The most recent Request object
  * @param ResponseInterface      $response  The most recent Response object
  * @param Exception              $exception The caught Exception object
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
 {
     $contentType = $this->determineContentType($request->getHeaderLine('Accept'));
     switch ($contentType) {
         case 'application/json':
             $output = $this->renderJsonErrorMessage($exception);
             break;
         case 'application/xml':
             $output = $this->renderXmlErrorMessage($exception);
             break;
         case 'text/html':
         default:
             $contentType = 'text/html';
             $output = $this->renderHtmlErrorMessage($exception);
             break;
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(500)->withHeader('Content-type', $contentType)->withBody($body);
 }
 public function render($response, array $data)
 {
     $contentType = $this->determineContentType($this->request->getHeaderLine('Accept'));
     switch ($contentType) {
         case 'text/html':
             $output = $this->renderHtml($data);
             break;
         case 'application/xml':
             $xml = Array2XML::createXML('root', $data);
             $output = $xml->saveXML();
             break;
         case 'application/json':
         default:
             $contentType = 'application/json';
             $output = json_encode($data);
             break;
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withHeader('Content-type', $contentType)->withBody($body);
 }
Example #17
0
 /**
  * Invoke error handler
  *
  * @param ServerRequestInterface $request   The most recent Request object
  * @param ResponseInterface      $response  The most recent Response object
  * @param \Exception             $exception The caught Exception object
  *
  * @return ResponseInterface
  * @throws UnexpectedValueException
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Exception $exception)
 {
     $contentType = $this->determineContentType($request);
     switch ($contentType) {
         case 'application/json':
             $output = $this->renderJsonErrorMessage($exception);
             break;
         case 'text/xml':
         case 'application/xml':
             $output = $this->renderXmlErrorMessage($exception);
             break;
         case 'text/html':
             $output = $this->renderHtmlErrorMessage($exception);
             break;
         default:
             throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType);
     }
     $this->writeToErrorLog($exception);
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(500)->withHeader('Content-type', $contentType)->withBody($body);
 }
 /**
  * Invoke not found handler.
  *
  * @param ServerRequestInterface $request  The most recent Request object
  * @param ResponseInterface      $response The most recent Response object
  *
  * @throws UnexpectedValueException
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     $contentType = $this->determineContentType($request);
     switch ($contentType) {
         case 'application/json':
             $output = $this->renderJsonNotFoundOutput();
             break;
         case 'text/xml':
         case 'application/xml':
             $output = $this->renderXmlNotFoundOutput();
             break;
         case 'text/html':
             if ($pageFound = $this->resolvePage($request, $response)) {
                 return $pageFound;
             }
             $output = $this->renderHtmlNotFoundOutput($request);
             break;
         default:
             throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType);
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus(404)->withHeader('Content-Type', $contentType)->withBody($body);
 }
 /**
  * @param \Psr\Http\Message\ServerRequestInterface $request
  * @param \Psr\Http\Message\ResponseInterface      $response
  * @param \Exception                               $exception
  *
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function __invoke(Request $request, Response $response, Exception $exception) : Response
 {
     if (!$exception instanceof HttpException) {
         return call_user_func($this->handler, $request, $response, $exception);
     }
     $contentType = $this->determineContentType($request);
     switch ($contentType) {
         case 'application/json':
             $output = $this->renderJsonErrorMessage($exception);
             break;
         case 'text/xml':
         case 'application/xml':
             $output = $this->renderXmlErrorMessage($exception);
             break;
         case 'text/html':
             $output = $this->renderHtmlErrorMessage($exception);
             break;
         default:
             $output = '';
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus($exception->getStatusCode())->withHeader('Content-Type', $contentType)->withBody($body);
 }
 public function testGetContentsDetachedThrowsRuntimeException()
 {
     $this->stream = $this->resourceFactory();
     $body = new Body($this->stream);
     $body->detach();
     $this->setExpectedException('\\RuntimeException');
     $body->getContents();
 }
Example #21
0
 /**
  * Perform a sub-request from within an application route
  *
  * This method allows you to prepare and initiate a sub-request, run within
  * the context of the current request. This WILL NOT issue a remote HTTP
  * request. Instead, it will route the provided URL, method, headers,
  * cookies, body, and server variables against the set of registered
  * application routes. The result response object is returned.
  *
  * @param  string            $method      The request method (e.g., GET, POST, PUT, etc.)
  * @param  string            $path        The request URI path
  * @param  string            $query       The request URI query string
  * @param  array             $headers     The request headers (key-value array)
  * @param  array             $cookies     The request cookies (key-value array)
  * @param  string            $bodyContent The request body
  * @param  ResponseInterface $response     The response object (optional)
  * @return ResponseInterface
  */
 public function subRequest($method, $path, $query = '', array $headers = [], array $cookies = [], $bodyContent = '', ResponseInterface $response = null)
 {
     $env = $this->container->get('environment');
     $uri = Uri::createFromEnvironment($env)->withPath($path)->withQuery($query);
     $headers = new Headers($headers);
     $serverParams = $env->all();
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($bodyContent);
     $body->rewind();
     $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body);
     if (!$response) {
         $response = $this->container->get('response');
     }
     return $this($request, $response);
 }
Example #22
0
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
 {
     $helper = new ErrorHelper($exception, $this->option('exceptions_with_code'), $this->option('exceptions_without_code'), $this->option('exceptions_with_messages'));
     $helper->setKnownTypes($this->option('allowed_content_types'));
     // Detect content type from header
     $response = $response->withHeader('Content-type', $helper->detectContentType($request));
     // Check for passed content-type callable in options
     if ($this->contentTypeCallable) {
         $response = call_user_func($this->contentTypeCallable, $request, $response);
     }
     $contentType = $response->getHeader('Content-type')[0];
     // Detect status code from the exception
     $statusCode = $helper->detectStatusCode();
     // Use view if available
     $view = null;
     if ($this->view) {
         $view = function ($errorData, $template) {
             return $this->view->fetch($template, $errorData);
         };
     }
     $output = '';
     switch ($contentType) {
         case 'application/json':
             $output = $helper->renderJsonErrorResponse($this->displayErrorDetails, $this->option('json_encode_options'));
             break;
         case 'text/xml':
         case 'application/xml':
             $output = $helper->renderXmlErrorResponse($this->displayErrorDetails, $view, $this->option('xml_template_path'));
             break;
         case 'text/html':
             $output = $helper->renderHtmlErrorResponse($this->displayErrorDetails, $view, $this->option('html_template_path'));
             break;
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withStatus($statusCode)->withBody($body);
 }
Example #23
0
 protected function getSlimTest(string $method, string $url, array $headers = []) : ResponseInterface
 {
     $slim = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);
     // add the CORs middleware
     $cors = $this->getCors($slim->getContainer());
     $slim->add($cors);
     // finish adding the CORs middleware
     // add our own error handler.
     $errorHandler = function (ContainerInterface $container) : callable {
         $handler = function (ServerRequestInterface $request, ResponseInterface $response, \Exception $e) : ResponseInterface {
             $body = new Body(fopen('php://temp', 'r+'));
             $body->write('Error Handler caught exception type ' . get_class($e) . ': ' . $e->getMessage());
             $return = $response->withStatus(500)->withBody($body);
             return $return;
         };
         return $handler;
     };
     // add the error handler.
     $slim->getContainer()['errorHandler'] = $errorHandler;
     // add dummy routes
     $slim->get('/foo', function (Request $req, Response $res) {
         $res->write('getted hi');
         return $res;
     });
     $slim->post('/foo', function (Request $req, Response $res) {
         $res->write('postted hi');
         return $res;
     });
     // Prepare request and response objects
     $uri = Uri::createFromString($url);
     $slimHeaders = new Headers($headers);
     $body = new RequestBody();
     $request = new Request($method, $uri, $slimHeaders, [], [], $body);
     $response = new Response();
     // override the Slim request and responses with our dummies
     $slim->getContainer()['request'] = $request;
     $slim->getContainer()['response'] = $response;
     // invoke Slim
     /* @var \Slim\Http\Response $result */
     $result = $slim->run(true);
     // check we got back what we expected
     $this->assertInstanceOf('\\Psr\\Http\\Message\\ResponseInterface', $result);
     $this->assertInstanceOf('\\Slim\\Http\\Response', $result);
     return $result;
 }
 /**
  * @param ResponseInterface $response
  * @param string $contents
  *
  * @return ResponseInterface
  */
 private function withNewBody(ResponseInterface $response, $contents)
 {
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($contents);
     return $response->withBody($body);
 }