Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/merge-not-found-handler-and-middleware' into re…
Browse files Browse the repository at this point in the history
…lease-3.0.0

Close #546
Fixes #441
  • Loading branch information
weierophinney committed Feb 6, 2018
2 parents a387dda + 5fdb648 commit 3c365f1
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 356 deletions.
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 3.0.0alpha3 - 2018-02-06

### Added

- Nothing.

### Changed

- [#546](https://github.com/zendframework/zend-expressive/pull/546) merges
`Zend\Expressive\Middleware\NotFoundHandler` into
`Zend\Expressive\Middleware\NotFoundMiddleware`, as well as merges
`Zend\Expressive\Container\NotFoundHandlerFactory` into
`Zend\Expressive\Container\NotFoundMiddlewareFactory`. `NotFoundMiddleware`
now does the work of the former `Zend\Expressive\Delegate\NotFoundDelegate`,
and, as such, accepts the following constructor arguments:

- PSR-7 `ResponseInterface $responsePrototype` (required)
- `Zend\Expressive\Template\TemplateRendererInterface $renderer` (optional)
- `string $template = self::TEMPLATE_DEFAULT` (optional; defaults to "error::404")
- `string $layout = self::LAYOUT_DEFAULT` (optional; defaults to "layout::default")

### Deprecated

- Nothing.

### Removed

- [#546](https://github.com/zendframework/zend-expressive/pull/546) removes the
class `Zend\Expressive\Delegate\DefaultDelegate`, as there is no longer a
concept of a default handler invoked by the application. Instead, developers
MUST pipe middleware at the innermost layer of the pipeline guaranteed to
return a response; we recommend using `Zend\Expressive\Middleware\NotFoundMiddleware`
for this purpose.

### Fixed

- Nothing.

## 3.0.0alpha2 - 2018-02-05

### Added
Expand Down
3 changes: 1 addition & 2 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getDependencies() : array
// @codingStandardsIgnoreStart
return [
'aliases' => [
Handler\DefaultHandler::class => Handler\NotFoundHandler::class,
Delegate\DefaultDelegate::class => Middleware\NotFoundMiddleware::class,
Middleware\DispatchMiddleware::class => Router\DispatchMiddleware::class,
Middleware\RouteMiddleware::class => Router\PathBasedRoutingMiddleware::class,
],
Expand All @@ -45,7 +45,6 @@ public function getDependencies() : array
ErrorHandler::class => Container\ErrorHandlerFactory::class,
// Change the following in development to the WhoopsErrorResponseGeneratorFactory:
ErrorResponseGenerator::class => Container\ErrorResponseGeneratorFactory::class,
Handler\NotFoundHandler::class => Handler\NotFoundHandlerFactory::class,
MiddlewareContainer::class => Container\MiddlewareContainerFactory::class,
MiddlewareFactory::class => Container\MiddlewareFactoryFactory::class,
Middleware\NotFoundMiddleware::class => Container\NotFoundMiddlewareFactory::class,
Expand Down
37 changes: 0 additions & 37 deletions src/Container/NotFoundHandlerFactory.php

This file was deleted.

19 changes: 17 additions & 2 deletions src/Container/NotFoundMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@
namespace Zend\Expressive\Container;

use Psr\Container\ContainerInterface;
use Zend\Expressive\Handler\NotFoundHandler;
use Psr\Http\Message\ResponseInterface;
use Zend\Expressive\Middleware\NotFoundMiddleware;
use Zend\Expressive\Template\TemplateRendererInterface;

class NotFoundMiddlewareFactory
{
public function __invoke(ContainerInterface $container) : NotFoundMiddleware
{
return new NotFoundMiddleware($container->get(NotFoundHandler::class));
$config = $container->has('config') ? $container->get('config') : [];
$renderer = $container->has(TemplateRendererInterface::class)
? $container->get(TemplateRendererInterface::class)
: null;
$template = $config['zend-expressive']['error_handler']['template_404']
?? NotFoundMiddleware::TEMPLATE_DEFAULT;
$layout = $config['zend-expressive']['error_handler']['layout']
?? NotFoundMiddleware::LAYOUT_DEFAULT;

return new NotFoundMiddleware(
$container->get(ResponseInterface::class),
$renderer,
$template,
$layout
);
}
}
100 changes: 0 additions & 100 deletions src/Handler/NotFoundHandler.php

This file was deleted.

79 changes: 70 additions & 9 deletions src/Middleware/NotFoundMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc. (https://www.zend.com)
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace Zend\Expressive\Middleware;

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Handler\NotFoundHandler;
use Zend\Expressive\Template\TemplateRendererInterface;

class NotFoundMiddleware implements MiddlewareInterface
{
public const TEMPLATE_DEFAULT = 'error::404';
public const LAYOUT_DEFAULT = 'layout::default';

/**
* @var NotFoundHandler
* @var TemplateRendererInterface
*/
private $internalHandler;
private $renderer;

/**
* @param NotFoundHandler $internalHandler
* This duplicates the property in StratigilityNotFoundHandler, but is done
* to ensure that we have access to the value in the methods we override.
*
* @var ResponseInterface
*/
public function __construct(NotFoundHandler $internalHandler)
{
$this->internalHandler = $internalHandler;
protected $responsePrototype;

/**
* @var string
*/
private $template;

/**
* @var string
*/
private $layout;

public function __construct(
ResponseInterface $responsePrototype,
TemplateRendererInterface $renderer = null,
string $template = self::TEMPLATE_DEFAULT,
string $layout = self::LAYOUT_DEFAULT
) {
$this->responsePrototype = $responsePrototype;
$this->renderer = $renderer;
$this->template = $template;
$this->layout = $layout;
}

/**
Expand All @@ -38,6 +64,41 @@ public function __construct(NotFoundHandler $internalHandler)
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
return $this->internalHandler->handle($request);
if (! $this->renderer) {
return $this->generatePlainTextResponse($request);
}

return $this->generateTemplatedResponse($request);
}

/**
* Generates a plain text response indicating the request method and URI.
*/
private function generatePlainTextResponse(ServerRequestInterface $request) : ResponseInterface
{
$response = $this->responsePrototype->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
$response->getBody()
->write(sprintf(
'Cannot %s %s',
$request->getMethod(),
(string) $request->getUri()
));

return $response;
}

/**
* Generates a response using a template.
*
* Template will receive the current request via the "request" variable.
*/
private function generateTemplatedResponse(ServerRequestInterface $request) : ResponseInterface
{
$response = $this->responsePrototype->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
$response->getBody()->write(
$this->renderer->render($this->template, ['request' => $request, 'layout' => $this->layout])
);

return $response;
}
}
5 changes: 2 additions & 3 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Zend\Expressive\Application;
use Zend\Expressive\ApplicationPipeline;
use Zend\Expressive\ConfigProvider;
use Zend\Expressive\Handler;
use Zend\Expressive\Delegate\DefaultDelegate;
use Zend\Expressive\Middleware;
use Zend\Expressive\MiddlewareContainer;
use Zend\Expressive\MiddlewareFactory;
Expand All @@ -38,7 +38,7 @@ public function testProviderDefinesExpectedAliases()
{
$config = $this->provider->getDependencies();
$aliases = $config['aliases'];
$this->assertArrayHasKey(Handler\DefaultHandler::class, $aliases);
$this->assertArrayHasKey(DefaultDelegate::class, $aliases);
$this->assertArrayHasKey(Middleware\DispatchMiddleware::class, $aliases);
$this->assertArrayHasKey(Middleware\RouteMiddleware::class, $aliases);
}
Expand All @@ -53,7 +53,6 @@ public function testProviderDefinesExpectedFactoryServices()
$this->assertArrayHasKey(EmitterInterface::class, $factories);
$this->assertArrayHasKey(ErrorHandler::class, $factories);
$this->assertArrayHasKey(ErrorResponseGenerator::class, $factories);
$this->assertArrayHasKey(Handler\NotFoundHandler::class, $factories);
$this->assertArrayHasKey(MiddlewareContainer::class, $factories);
$this->assertArrayHasKey(MiddlewareFactory::class, $factories);
$this->assertArrayHasKey(DispatchMiddleware::class, $factories);
Expand Down
Loading

0 comments on commit 3c365f1

Please sign in to comment.