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

Commit 3c365f1

Browse files
committed
Merge branch 'feature/merge-not-found-handler-and-middleware' into release-3.0.0
Close #546 Fixes #441
2 parents a387dda + 5fdb648 commit 3c365f1

File tree

11 files changed

+262
-356
lines changed

11 files changed

+262
-356
lines changed

CHANGELOG.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
22

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

5+
## 3.0.0alpha3 - 2018-02-06
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- [#546](https://github.com/zendframework/zend-expressive/pull/546) merges
14+
`Zend\Expressive\Middleware\NotFoundHandler` into
15+
`Zend\Expressive\Middleware\NotFoundMiddleware`, as well as merges
16+
`Zend\Expressive\Container\NotFoundHandlerFactory` into
17+
`Zend\Expressive\Container\NotFoundMiddlewareFactory`. `NotFoundMiddleware`
18+
now does the work of the former `Zend\Expressive\Delegate\NotFoundDelegate`,
19+
and, as such, accepts the following constructor arguments:
20+
21+
- PSR-7 `ResponseInterface $responsePrototype` (required)
22+
- `Zend\Expressive\Template\TemplateRendererInterface $renderer` (optional)
23+
- `string $template = self::TEMPLATE_DEFAULT` (optional; defaults to "error::404")
24+
- `string $layout = self::LAYOUT_DEFAULT` (optional; defaults to "layout::default")
25+
26+
### Deprecated
27+
28+
- Nothing.
29+
30+
### Removed
31+
32+
- [#546](https://github.com/zendframework/zend-expressive/pull/546) removes the
33+
class `Zend\Expressive\Delegate\DefaultDelegate`, as there is no longer a
34+
concept of a default handler invoked by the application. Instead, developers
35+
MUST pipe middleware at the innermost layer of the pipeline guaranteed to
36+
return a response; we recommend using `Zend\Expressive\Middleware\NotFoundMiddleware`
37+
for this purpose.
38+
39+
### Fixed
40+
41+
- Nothing.
42+
543
## 3.0.0alpha2 - 2018-02-05
644

745
### Added

src/ConfigProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getDependencies() : array
3434
// @codingStandardsIgnoreStart
3535
return [
3636
'aliases' => [
37-
Handler\DefaultHandler::class => Handler\NotFoundHandler::class,
37+
Delegate\DefaultDelegate::class => Middleware\NotFoundMiddleware::class,
3838
Middleware\DispatchMiddleware::class => Router\DispatchMiddleware::class,
3939
Middleware\RouteMiddleware::class => Router\PathBasedRoutingMiddleware::class,
4040
],
@@ -45,7 +45,6 @@ public function getDependencies() : array
4545
ErrorHandler::class => Container\ErrorHandlerFactory::class,
4646
// Change the following in development to the WhoopsErrorResponseGeneratorFactory:
4747
ErrorResponseGenerator::class => Container\ErrorResponseGeneratorFactory::class,
48-
Handler\NotFoundHandler::class => Handler\NotFoundHandlerFactory::class,
4948
MiddlewareContainer::class => Container\MiddlewareContainerFactory::class,
5049
MiddlewareFactory::class => Container\MiddlewareFactoryFactory::class,
5150
Middleware\NotFoundMiddleware::class => Container\NotFoundMiddlewareFactory::class,

src/Container/NotFoundHandlerFactory.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Container/NotFoundMiddlewareFactory.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,28 @@
1010
namespace Zend\Expressive\Container;
1111

1212
use Psr\Container\ContainerInterface;
13-
use Zend\Expressive\Handler\NotFoundHandler;
13+
use Psr\Http\Message\ResponseInterface;
1414
use Zend\Expressive\Middleware\NotFoundMiddleware;
15+
use Zend\Expressive\Template\TemplateRendererInterface;
1516

1617
class NotFoundMiddlewareFactory
1718
{
1819
public function __invoke(ContainerInterface $container) : NotFoundMiddleware
1920
{
20-
return new NotFoundMiddleware($container->get(NotFoundHandler::class));
21+
$config = $container->has('config') ? $container->get('config') : [];
22+
$renderer = $container->has(TemplateRendererInterface::class)
23+
? $container->get(TemplateRendererInterface::class)
24+
: null;
25+
$template = $config['zend-expressive']['error_handler']['template_404']
26+
?? NotFoundMiddleware::TEMPLATE_DEFAULT;
27+
$layout = $config['zend-expressive']['error_handler']['layout']
28+
?? NotFoundMiddleware::LAYOUT_DEFAULT;
29+
30+
return new NotFoundMiddleware(
31+
$container->get(ResponseInterface::class),
32+
$renderer,
33+
$template,
34+
$layout
35+
);
2136
}
2237
}

src/Handler/NotFoundHandler.php

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,59 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
4-
* @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (https://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
66
*/
77

88
declare(strict_types=1);
99

1010
namespace Zend\Expressive\Middleware;
1111

12+
use Fig\Http\Message\StatusCodeInterface;
1213
use Psr\Http\Message\ResponseInterface;
1314
use Psr\Http\Message\ServerRequestInterface;
1415
use Psr\Http\Server\MiddlewareInterface;
1516
use Psr\Http\Server\RequestHandlerInterface;
16-
use Zend\Expressive\Handler\NotFoundHandler;
17+
use Zend\Expressive\Template\TemplateRendererInterface;
1718

1819
class NotFoundMiddleware implements MiddlewareInterface
1920
{
21+
public const TEMPLATE_DEFAULT = 'error::404';
22+
public const LAYOUT_DEFAULT = 'layout::default';
23+
2024
/**
21-
* @var NotFoundHandler
25+
* @var TemplateRendererInterface
2226
*/
23-
private $internalHandler;
27+
private $renderer;
2428

2529
/**
26-
* @param NotFoundHandler $internalHandler
30+
* This duplicates the property in StratigilityNotFoundHandler, but is done
31+
* to ensure that we have access to the value in the methods we override.
32+
*
33+
* @var ResponseInterface
2734
*/
28-
public function __construct(NotFoundHandler $internalHandler)
29-
{
30-
$this->internalHandler = $internalHandler;
35+
protected $responsePrototype;
36+
37+
/**
38+
* @var string
39+
*/
40+
private $template;
41+
42+
/**
43+
* @var string
44+
*/
45+
private $layout;
46+
47+
public function __construct(
48+
ResponseInterface $responsePrototype,
49+
TemplateRendererInterface $renderer = null,
50+
string $template = self::TEMPLATE_DEFAULT,
51+
string $layout = self::LAYOUT_DEFAULT
52+
) {
53+
$this->responsePrototype = $responsePrototype;
54+
$this->renderer = $renderer;
55+
$this->template = $template;
56+
$this->layout = $layout;
3157
}
3258

3359
/**
@@ -38,6 +64,41 @@ public function __construct(NotFoundHandler $internalHandler)
3864
*/
3965
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
4066
{
41-
return $this->internalHandler->handle($request);
67+
if (! $this->renderer) {
68+
return $this->generatePlainTextResponse($request);
69+
}
70+
71+
return $this->generateTemplatedResponse($request);
72+
}
73+
74+
/**
75+
* Generates a plain text response indicating the request method and URI.
76+
*/
77+
private function generatePlainTextResponse(ServerRequestInterface $request) : ResponseInterface
78+
{
79+
$response = $this->responsePrototype->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
80+
$response->getBody()
81+
->write(sprintf(
82+
'Cannot %s %s',
83+
$request->getMethod(),
84+
(string) $request->getUri()
85+
));
86+
87+
return $response;
88+
}
89+
90+
/**
91+
* Generates a response using a template.
92+
*
93+
* Template will receive the current request via the "request" variable.
94+
*/
95+
private function generateTemplatedResponse(ServerRequestInterface $request) : ResponseInterface
96+
{
97+
$response = $this->responsePrototype->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
98+
$response->getBody()->write(
99+
$this->renderer->render($this->template, ['request' => $request, 'layout' => $this->layout])
100+
);
101+
102+
return $response;
42103
}
43104
}

test/ConfigProviderTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Zend\Expressive\Application;
1515
use Zend\Expressive\ApplicationPipeline;
1616
use Zend\Expressive\ConfigProvider;
17-
use Zend\Expressive\Handler;
17+
use Zend\Expressive\Delegate\DefaultDelegate;
1818
use Zend\Expressive\Middleware;
1919
use Zend\Expressive\MiddlewareContainer;
2020
use Zend\Expressive\MiddlewareFactory;
@@ -38,7 +38,7 @@ public function testProviderDefinesExpectedAliases()
3838
{
3939
$config = $this->provider->getDependencies();
4040
$aliases = $config['aliases'];
41-
$this->assertArrayHasKey(Handler\DefaultHandler::class, $aliases);
41+
$this->assertArrayHasKey(DefaultDelegate::class, $aliases);
4242
$this->assertArrayHasKey(Middleware\DispatchMiddleware::class, $aliases);
4343
$this->assertArrayHasKey(Middleware\RouteMiddleware::class, $aliases);
4444
}
@@ -53,7 +53,6 @@ public function testProviderDefinesExpectedFactoryServices()
5353
$this->assertArrayHasKey(EmitterInterface::class, $factories);
5454
$this->assertArrayHasKey(ErrorHandler::class, $factories);
5555
$this->assertArrayHasKey(ErrorResponseGenerator::class, $factories);
56-
$this->assertArrayHasKey(Handler\NotFoundHandler::class, $factories);
5756
$this->assertArrayHasKey(MiddlewareContainer::class, $factories);
5857
$this->assertArrayHasKey(MiddlewareFactory::class, $factories);
5958
$this->assertArrayHasKey(DispatchMiddleware::class, $factories);

0 commit comments

Comments
 (0)