Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Closed
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
24 changes: 24 additions & 0 deletions src/Container/ErrorHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Container;

use Interop\Container\ContainerInterface;
use Zend\Diactoros\Response;
use Zend\Expressive\TemplatedErrorResponseGenerator;
use Zend\Stratigility\Middleware\ErrorHandler;

class ErrorHandlerFactory
{
public function __invoke(ContainerInterface $container)
{
return new ErrorHandler(
new Response(),
$container->get(TemplatedErrorResponseGenerator::class)
);
}
}
33 changes: 33 additions & 0 deletions src/Container/NotFoundHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Container;

use Interop\Container\ContainerInterface;
use Zend\Diactoros\Response;
use Zend\Expressive\NotFoundHandler;
use Zend\Expressive\Template\TemplateRendererInterface;

class NotFoundHandlerFactory
{
public function __invoke(ContainerInterface $container)
{
$config = $container->has('config')
? $container->get('config')
: [];

$template = isset($config['zend-expressive']['error_handler']['template_404'])
? $config['zend-expressive']['error_handler']['template_404']
: 'error/404';

return new NotFoundHandler(
$container->get(TemplateRendererInterface::class),
new Response(),
$template
);
}
}
31 changes: 31 additions & 0 deletions src/Container/TemplatedErrorResponseGeneratorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Container;

use Interop\Container\ContainerInterface;
use Zend\Expressive\Template\TemplateRendererInterface;
use Zend\Expressive\TemplatedErrorResponseGenerator;

class TemplatedErrorResponseGeneratorFactory
{
public function __invoke(ContainerInterface $container)
{
$config = $container->has('config') ? $container->get('config') : [];
$debug = isset($config['debug']) ? $config['debug'] : false;

$template = isset($config['zend-expressive']['error_handler']['template_error'])
? $config['zend-expressive']['error_handler']['template_error']
: 'error/error';

return new TemplatedErrorResponseGenerator(
$container->get(TemplateRendererInterface::class),
$template,
$debug
);
}
}
72 changes: 72 additions & 0 deletions src/NotFoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive;

use Interop\Http\Middleware\DelegateInterface;
use Interop\Http\Middleware\ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Template\TemplateRendererInterface;
use Zend\Stratigility\Delegate\CallableDelegateDecorator;

class NotFoundHandler implements ServerMiddlewareInterface
{
private $renderer;

private $responsePrototype;

private $template;

/**
* NotFoundHandler constructor.
*
* @param TemplateRendererInterface $renderer
* @param ResponseInterface $responsePrototype
*/
public function __construct(TemplateRendererInterface $renderer, ResponseInterface $responsePrototype, $template)
{
$this->renderer = $renderer;
$this->responsePrototype = $responsePrototype;
$this->template = $template;
}

/**
* Proxy to process()
*
* Proxies to process, after first wrapping the `$next` argument using the
* CallableDelegateDecorator.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
return $this->process($request, new CallableDelegateDecorator($next, $response));
}

/**
* Creates and returns a 404 response.
*
* @param ServerRequestInterface $request Ignored.
* @param DelegateInterface $delegate Ignored.
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$response = $this->responsePrototype->withStatus(404);
$response->getBody()->write(
$this->renderer->render($this->template)
);

return $response;
}
}
41 changes: 41 additions & 0 deletions src/TemplatedErrorResponseGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Template\TemplateRendererInterface;

class TemplatedErrorResponseGenerator
{
private $isDevelopmentMode;

private $renderer;

private $template;

public function __construct(TemplateRendererInterface $renderer, $template, $isDevelopmentMode = false)
{
$this->renderer = $renderer;
$this->isDevelopmentMode = $isDevelopmentMode;
$this->template = $template;
}

public function __invoke($e, ServerRequestInterface $request, ResponseInterface $response)
{
$response = $response->withStatus(500);
$response->getBody()->write($this->renderer->render('error::error', [
'exception' => $e,
'development_mode' => $this->isDevelopmentMode,
'status' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
]));

return $response;
}
}