From 7f7d0cb8726a9dbb94a4b8210fe99853f3de62fc Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 6 Feb 2018 08:44:55 -0600 Subject: [PATCH 1/6] Removes concept of "default handler" Because the `MiddlewarePipe` does not compose a "default handler", and because the `MiddlewarePipe` is what the request handler runner dispatches, there is no longer a concept of a "default delegate" (v2) or "default handler" (v3); there is only middleware. As such, this patch merges the `NotFoundHandler` into the `NotFoundMiddleware`. A key is kept for the `DefaultDelegate` service, but it will now resolve to the `NotFoundMiddleware`. Fixes #441. --- src/ConfigProvider.php | 3 +- src/Container/NotFoundHandlerFactory.php | 37 ------- src/Container/NotFoundMiddlewareFactory.php | 19 +++- src/Handler/NotFoundHandler.php | 100 ------------------ src/Middleware/NotFoundMiddleware.php | 79 ++++++++++++-- test/ConfigProviderTest.php | 4 +- test/Container/NotFoundHandlerFactoryTest.php | 83 --------------- .../NotFoundMiddlewareFactoryTest.php | 67 ++++++++++-- test/Handler/NotFoundHandlerTest.php | 98 ----------------- test/Middleware/NotFoundMiddlewareTest.php | 86 ++++++++++++--- 10 files changed, 220 insertions(+), 356 deletions(-) delete mode 100644 src/Container/NotFoundHandlerFactory.php delete mode 100644 src/Handler/NotFoundHandler.php delete mode 100644 test/Container/NotFoundHandlerFactoryTest.php delete mode 100644 test/Handler/NotFoundHandlerTest.php diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index febcc245..f22f7907 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -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, ], @@ -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, diff --git a/src/Container/NotFoundHandlerFactory.php b/src/Container/NotFoundHandlerFactory.php deleted file mode 100644 index fd82ad8e..00000000 --- a/src/Container/NotFoundHandlerFactory.php +++ /dev/null @@ -1,37 +0,0 @@ -has('config') ? $container->get('config') : []; - $renderer = $container->has(TemplateRendererInterface::class) - ? $container->get(TemplateRendererInterface::class) - : null; - $template = $config['zend-expressive']['error_handler']['template_404'] - ?? NotFoundHandler::TEMPLATE_DEFAULT; - $layout = $config['zend-expressive']['error_handler']['layout'] - ?? NotFoundHandler::LAYOUT_DEFAULT; - - return new NotFoundHandler( - $container->get(ResponseInterface::class), - $renderer, - $template, - $layout - ); - } -} diff --git a/src/Container/NotFoundMiddlewareFactory.php b/src/Container/NotFoundMiddlewareFactory.php index 9f74862b..055fcb0e 100644 --- a/src/Container/NotFoundMiddlewareFactory.php +++ b/src/Container/NotFoundMiddlewareFactory.php @@ -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 + ); } } diff --git a/src/Handler/NotFoundHandler.php b/src/Handler/NotFoundHandler.php deleted file mode 100644 index a98f40f7..00000000 --- a/src/Handler/NotFoundHandler.php +++ /dev/null @@ -1,100 +0,0 @@ -responsePrototype = $responsePrototype; - $this->renderer = $renderer; - $this->template = $template; - $this->layout = $layout; - } - - /** - * Creates and returns a 404 response. - */ - public function handle(ServerRequestInterface $request) : ResponseInterface - { - 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; - } -} diff --git a/src/Middleware/NotFoundMiddleware.php b/src/Middleware/NotFoundMiddleware.php index 929c71ff..0dfd5f41 100644 --- a/src/Middleware/NotFoundMiddleware.php +++ b/src/Middleware/NotFoundMiddleware.php @@ -1,7 +1,7 @@ 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; } /** @@ -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; } } diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index b4362ced..9bf03b66 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -14,7 +14,6 @@ use Zend\Expressive\Application; use Zend\Expressive\ApplicationPipeline; use Zend\Expressive\ConfigProvider; -use Zend\Expressive\Handler; use Zend\Expressive\Middleware; use Zend\Expressive\MiddlewareContainer; use Zend\Expressive\MiddlewareFactory; @@ -38,7 +37,7 @@ public function testProviderDefinesExpectedAliases() { $config = $this->provider->getDependencies(); $aliases = $config['aliases']; - $this->assertArrayHasKey(Handler\DefaultHandler::class, $aliases); + $this->assertArrayHasKey('Zend\Expressive\Delegate\DefaultDelegate', $aliases); $this->assertArrayHasKey(Middleware\DispatchMiddleware::class, $aliases); $this->assertArrayHasKey(Middleware\RouteMiddleware::class, $aliases); } @@ -53,7 +52,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); diff --git a/test/Container/NotFoundHandlerFactoryTest.php b/test/Container/NotFoundHandlerFactoryTest.php deleted file mode 100644 index db98b3e5..00000000 --- a/test/Container/NotFoundHandlerFactoryTest.php +++ /dev/null @@ -1,83 +0,0 @@ -container = $this->prophesize(ContainerInterface::class); - $this->response = $this->prophesize(ResponseInterface::class)->reveal(); - $this->container->get(ResponseInterface::class)->willReturn($this->response); - } - - public function testFactoryCreatesInstanceWithoutRendererIfRendererServiceIsMissing() - { - $this->container->has('config')->willReturn(false); - $this->container->has(TemplateRendererInterface::class)->willReturn(false); - $factory = new NotFoundHandlerFactory(); - - $handler = $factory($this->container->reveal()); - $this->assertInstanceOf(NotFoundHandler::class, $handler); - $this->assertAttributeSame($this->response, 'responsePrototype', $handler); - $this->assertAttributeEmpty('renderer', $handler); - } - - public function testFactoryCreatesInstanceUsingRendererServiceWhenPresent() - { - $renderer = $this->prophesize(TemplateRendererInterface::class)->reveal(); - $this->container->has('config')->willReturn(false); - $this->container->has(TemplateRendererInterface::class)->willReturn(true); - $this->container->get(TemplateRendererInterface::class)->willReturn($renderer); - $factory = new NotFoundHandlerFactory(); - - $handler = $factory($this->container->reveal()); - $this->assertAttributeSame($renderer, 'renderer', $handler); - } - - public function testFactoryUsesConfigured404TemplateWhenPresent() - { - $config = [ - 'zend-expressive' => [ - 'error_handler' => [ - 'layout' => 'layout::error', - 'template_404' => 'foo::bar', - ], - ], - ]; - $this->container->has('config')->willReturn(true); - $this->container->get('config')->willReturn($config); - $this->container->has(TemplateRendererInterface::class)->willReturn(false); - $factory = new NotFoundHandlerFactory(); - - $handler = $factory($this->container->reveal()); - $this->assertAttributeEquals( - $config['zend-expressive']['error_handler']['layout'], - 'layout', - $handler - ); - $this->assertAttributeEquals( - $config['zend-expressive']['error_handler']['template_404'], - 'template', - $handler - ); - } -} diff --git a/test/Container/NotFoundMiddlewareFactoryTest.php b/test/Container/NotFoundMiddlewareFactoryTest.php index 45830dac..c25fcefb 100644 --- a/test/Container/NotFoundMiddlewareFactoryTest.php +++ b/test/Container/NotFoundMiddlewareFactoryTest.php @@ -10,23 +10,74 @@ namespace ZendTest\Expressive\Container; use PHPUnit\Framework\TestCase; +use Prophecy\Prophecy\ObjectProphecy; use Psr\Container\ContainerInterface; +use Psr\Http\Message\ResponseInterface; use Zend\Expressive\Container\NotFoundMiddlewareFactory; -use Zend\Expressive\Handler\NotFoundHandler; use Zend\Expressive\Middleware\NotFoundMiddleware; +use Zend\Expressive\Template\TemplateRendererInterface; class NotFoundMiddlewareFactoryTest extends TestCase { - public function testUsesComposedNotFoundHandlerServiceToCreateNotFoundHandlerMiddleware() + /** @var ContainerInterface|ObjectProphecy */ + private $container; + + protected function setUp() { - $handler = $this->prophesize(NotFoundHandler::class)->reveal(); - $container = $this->prophesize(ContainerInterface::class); - $container->get(NotFoundHandler::class)->willReturn($handler); - $factory = new NotFoundMiddlewareFactory(); + $this->container = $this->prophesize(ContainerInterface::class); + $this->response = $this->prophesize(ResponseInterface::class)->reveal(); + $this->container->get(ResponseInterface::class)->willReturn($this->response); + } - $middleware = $factory($container->reveal()); + public function testFactoryCreatesInstanceWithoutRendererIfRendererServiceIsMissing() + { + $this->container->has('config')->willReturn(false); + $this->container->has(TemplateRendererInterface::class)->willReturn(false); + $factory = new NotFoundMiddlewareFactory(); + $middleware = $factory($this->container->reveal()); $this->assertInstanceOf(NotFoundMiddleware::class, $middleware); - $this->assertAttributeSame($handler, 'internalHandler', $middleware); + $this->assertAttributeSame($this->response, 'responsePrototype', $middleware); + $this->assertAttributeEmpty('renderer', $middleware); + } + + public function testFactoryCreatesInstanceUsingRendererServiceWhenPresent() + { + $renderer = $this->prophesize(TemplateRendererInterface::class)->reveal(); + $this->container->has('config')->willReturn(false); + $this->container->has(TemplateRendererInterface::class)->willReturn(true); + $this->container->get(TemplateRendererInterface::class)->willReturn($renderer); + $factory = new NotFoundMiddlewareFactory(); + + $middleware = $factory($this->container->reveal()); + $this->assertAttributeSame($renderer, 'renderer', $middleware); + } + + public function testFactoryUsesConfigured404TemplateWhenPresent() + { + $config = [ + 'zend-expressive' => [ + 'error_handler' => [ + 'layout' => 'layout::error', + 'template_404' => 'foo::bar', + ], + ], + ]; + $this->container->has('config')->willReturn(true); + $this->container->get('config')->willReturn($config); + $this->container->has(TemplateRendererInterface::class)->willReturn(false); + $factory = new NotFoundMiddlewareFactory(); + + $middleware = $factory($this->container->reveal()); + $this->assertAttributeEquals( + $config['zend-expressive']['error_handler']['layout'], + 'layout', + $middleware + ); + $this->assertAttributeEquals( + $config['zend-expressive']['error_handler']['template_404'], + 'template', + $middleware + ); } } diff --git a/test/Handler/NotFoundHandlerTest.php b/test/Handler/NotFoundHandlerTest.php deleted file mode 100644 index d79981fb..00000000 --- a/test/Handler/NotFoundHandlerTest.php +++ /dev/null @@ -1,98 +0,0 @@ -response = $this->prophesize(ResponseInterface::class); - } - - public function testConstructorDoesNotRequireARenderer() - { - $handler = new NotFoundHandler($this->response->reveal()); - $this->assertInstanceOf(NotFoundHandler::class, $handler); - $this->assertAttributeSame($this->response->reveal(), 'responsePrototype', $handler); - } - - public function testConstructorCanAcceptRendererAndTemplate() - { - $renderer = $this->prophesize(TemplateRendererInterface::class)->reveal(); - $template = 'foo::bar'; - $layout = 'layout::error'; - - $handler = new NotFoundHandler($this->response->reveal(), $renderer, $template, $layout); - - $this->assertInstanceOf(NotFoundHandler::class, $handler); - $this->assertAttributeSame($renderer, 'renderer', $handler); - $this->assertAttributeEquals($template, 'template', $handler); - $this->assertAttributeEquals($layout, 'layout', $handler); - } - - public function testRendersDefault404ResponseWhenNoRendererPresent() - { - $request = $this->prophesize(ServerRequestInterface::class); - $request->getMethod()->willReturn(RequestMethod::METHOD_POST); - $request->getUri()->willReturn('https://example.com/foo/bar'); - - $stream = $this->prophesize(StreamInterface::class); - $stream->write('Cannot POST https://example.com/foo/bar')->shouldBeCalled(); - $this->response->withStatus(StatusCode::STATUS_NOT_FOUND)->will([$this->response, 'reveal']); - $this->response->getBody()->will([$stream, 'reveal']); - - $handler = new NotFoundHandler($this->response->reveal()); - - $response = $handler->handle($request->reveal()); - - $this->assertSame($this->response->reveal(), $response); - } - - public function testUsesRendererToGenerateResponseContentsWhenPresent() - { - $request = $this->prophesize(ServerRequestInterface::class)->reveal(); - - $renderer = $this->prophesize(TemplateRendererInterface::class); - $renderer - ->render( - NotFoundHandler::TEMPLATE_DEFAULT, - [ - 'request' => $request, - 'layout' => NotFoundHandler::LAYOUT_DEFAULT, - ] - ) - ->willReturn('CONTENT'); - - $stream = $this->prophesize(StreamInterface::class); - $stream->write('CONTENT')->shouldBeCalled(); - - $this->response->withStatus(StatusCode::STATUS_NOT_FOUND)->will([$this->response, 'reveal']); - $this->response->getBody()->will([$stream, 'reveal']); - - $handler = new NotFoundHandler($this->response->reveal(), $renderer->reveal()); - - $response = $handler->handle($request); - - $this->assertSame($this->response->reveal(), $response); - } -} diff --git a/test/Middleware/NotFoundMiddlewareTest.php b/test/Middleware/NotFoundMiddlewareTest.php index 17eb21a8..10f97ea8 100644 --- a/test/Middleware/NotFoundMiddlewareTest.php +++ b/test/Middleware/NotFoundMiddlewareTest.php @@ -9,31 +9,34 @@ namespace ZendTest\Expressive\Middleware; +use Fig\Http\Message\RequestMethodInterface as RequestMethod; +use Fig\Http\Message\StatusCodeInterface as StatusCode; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\StreamInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use Zend\Expressive\Handler\NotFoundHandler; use Zend\Expressive\Middleware\NotFoundMiddleware; +use Zend\Expressive\Template\TemplateRendererInterface; class NotFoundMiddlewareTest extends TestCase { - /** @var NotFoundHandler|ObjectProphecy */ - private $internal; + /** @var RequestHandlerInterface|ObjectProphecy */ + private $handler; /** @var ServerRequestInterface|ObjectProphecy */ private $request; - /** @var RequestHandlerInterface|ObjectProphecy */ - private $handler; + /** @var ResponseInterface|ObjectProphecy */ + private $response; public function setUp() { - $this->internal = $this->prophesize(NotFoundHandler::class); $this->request = $this->prophesize(ServerRequestInterface::class); + $this->response = $this->prophesize(ResponseInterface::class); $this->handler = $this->prophesize(RequestHandlerInterface::class); $this->handler->handle(Argument::type(ServerRequestInterface::class))->shouldNotBeCalled(); @@ -41,19 +44,74 @@ public function setUp() public function testImplementsInteropMiddleware() { - $handler = new NotFoundMiddleware($this->internal->reveal()); + $handler = new NotFoundMiddleware($this->response->reveal()); $this->assertInstanceOf(MiddlewareInterface::class, $handler); } - public function testProxiesToInternalHandler() + public function testConstructorDoesNotRequireARenderer() + { + $middleware = new NotFoundMiddleware($this->response->reveal()); + $this->assertInstanceOf(NotFoundMiddleware::class, $middleware); + $this->assertAttributeSame($this->response->reveal(), 'responsePrototype', $middleware); + } + + public function testConstructorCanAcceptRendererAndTemplate() + { + $renderer = $this->prophesize(TemplateRendererInterface::class)->reveal(); + $template = 'foo::bar'; + $layout = 'layout::error'; + + $middleware = new NotFoundMiddleware($this->response->reveal(), $renderer, $template, $layout); + + $this->assertInstanceOf(NotFoundMiddleware::class, $middleware); + $this->assertAttributeSame($renderer, 'renderer', $middleware); + $this->assertAttributeEquals($template, 'template', $middleware); + $this->assertAttributeEquals($layout, 'layout', $middleware); + } + + public function testRendersDefault404ResponseWhenNoRendererPresent() { - $response = $this->prophesize(ResponseInterface::class)->reveal(); + $request = $this->prophesize(ServerRequestInterface::class); + $request->getMethod()->willReturn(RequestMethod::METHOD_POST); + $request->getUri()->willReturn('https://example.com/foo/bar'); + + $stream = $this->prophesize(StreamInterface::class); + $stream->write('Cannot POST https://example.com/foo/bar')->shouldBeCalled(); + $this->response->withStatus(StatusCode::STATUS_NOT_FOUND)->will([$this->response, 'reveal']); + $this->response->getBody()->will([$stream, 'reveal']); + + $middleware = new NotFoundMiddleware($this->response->reveal()); + + $response = $middleware->process($request->reveal(), $this->handler->reveal()); + + $this->assertSame($this->response->reveal(), $response); + } + + public function testUsesRendererToGenerateResponseContentsWhenPresent() + { + $request = $this->prophesize(ServerRequestInterface::class)->reveal(); + + $renderer = $this->prophesize(TemplateRendererInterface::class); + $renderer + ->render( + NotFoundMiddleware::TEMPLATE_DEFAULT, + [ + 'request' => $request, + 'layout' => NotFoundMiddleware::LAYOUT_DEFAULT, + ] + ) + ->willReturn('CONTENT'); + + $stream = $this->prophesize(StreamInterface::class); + $stream->write('CONTENT')->shouldBeCalled(); + + $this->response->withStatus(StatusCode::STATUS_NOT_FOUND)->will([$this->response, 'reveal']); + $this->response->getBody()->will([$stream, 'reveal']); + + $middleware = new NotFoundMiddleware($this->response->reveal(), $renderer->reveal()); - $this->internal - ->handle(Argument::that([$this->request, 'reveal'])) - ->willReturn($response); + $response = $middleware->process($request, $this->handler->reveal()); - $handler = new NotFoundMiddleware($this->internal->reveal()); - $this->assertEquals($response, $handler->process($this->request->reveal(), $this->handler->reveal())); + $this->assertSame($this->response->reveal(), $response); } } From 2c80390647a6f45c23e8e805ebd254378a6354c0 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 6 Feb 2018 08:55:09 -0600 Subject: [PATCH 2/6] Adds CHANGELOG entries for #546 Removes the #543 entry indicating the changed `NotFoundDelegate`, instead adding a "Removed" entry for that class. Updates the "Changed" entry for the `NotFoundMiddleware` to indicate its new constructor arguments. --- CHANGELOG.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a74ce85..af4f50b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,18 +97,19 @@ All notable changes to this project will be documented in this file, in reverse and removes the dependency http-interop/http-server-middleware. -- [#543](https://github.com/zendframework/zend-expressive/pull/543) renames +- [#543](https://github.com/zendframework/zend-expressive/pull/543) and + [#546](https://github.com/zendframework/zend-expressive/pull/546) renames the `Zend\Expressive\Middleware\NotFoundHandler` to `Zend\Expressive\Middleware\NotFoundMiddleware`, and its accompanying factory `Zend\Expressive\Container\NotFoundHandlerFactory` to - `Zend\Expressive\Container\NotFoundMiddlewareFactory`. + `Zend\Expressive\Container\NotFoundMiddlewareFactory`. Additionally, the + class now does the work of the former `Zend\Expressive\Delegate\NotFoundDelegate`, + and, as such, accepts the following constructor arguments: -- [#543](https://github.com/zendframework/zend-expressive/pull/543) renames - `Zend\Expressive\Delegate\NotFoundDelegate` to - `Zend\Expressive\Handler\NotFoundHandler`, updating it to implement the PSR-15 - `RequestHandlerInterface`. It also renames the factory - `Zend\Expressive\Container\NotFoundDelegateFactory` to - `Zend\Expressive\Container\NotFoundHandlerFactory`. + - 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") - [#543](https://github.com/zendframework/zend-expressive/pull/543) refactors `Zend\Expressive\Application` completely. @@ -187,6 +188,13 @@ All notable changes to this project will be documented in this file, in reverse - [#543](https://github.com/zendframework/zend-expressive/pull/543) removes support for http-interop/http-server-middleware. +- [#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. + - [#543](https://github.com/zendframework/zend-expressive/pull/543) removes the class `Zend\Expressive\Middleware\RouteMiddleware`. Use the `PathBasedRoutingMiddleware` or `RouteMiddleware` from zend-expressive-router From 7fd33d66013fca81f909309b673ee1dcb5088493 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 6 Feb 2018 09:42:32 -0600 Subject: [PATCH 3/6] Give class constants explicity visibility --- src/Middleware/NotFoundMiddleware.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Middleware/NotFoundMiddleware.php b/src/Middleware/NotFoundMiddleware.php index 0dfd5f41..32daae57 100644 --- a/src/Middleware/NotFoundMiddleware.php +++ b/src/Middleware/NotFoundMiddleware.php @@ -18,8 +18,8 @@ class NotFoundMiddleware implements MiddlewareInterface { - const TEMPLATE_DEFAULT = 'error::404'; - const LAYOUT_DEFAULT = 'layout::default'; + public const TEMPLATE_DEFAULT = 'error::404'; + public const LAYOUT_DEFAULT = 'layout::default'; /** * @var TemplateRendererInterface From 5b0650f91f1b2f47f4a687192ba7b2cb330e8fdf Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 6 Feb 2018 09:43:16 -0600 Subject: [PATCH 4/6] Use consistent `::class` notation --- test/ConfigProviderTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index 9bf03b66..0577ab51 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -14,6 +14,7 @@ use Zend\Expressive\Application; use Zend\Expressive\ApplicationPipeline; use Zend\Expressive\ConfigProvider; +use Zend\Expressive\Delegate\DefaultDelegate; use Zend\Expressive\Middleware; use Zend\Expressive\MiddlewareContainer; use Zend\Expressive\MiddlewareFactory; @@ -37,7 +38,7 @@ public function testProviderDefinesExpectedAliases() { $config = $this->provider->getDependencies(); $aliases = $config['aliases']; - $this->assertArrayHasKey('Zend\Expressive\Delegate\DefaultDelegate', $aliases); + $this->assertArrayHasKey(DefaultDelegate::class, $aliases); $this->assertArrayHasKey(Middleware\DispatchMiddleware::class, $aliases); $this->assertArrayHasKey(Middleware\RouteMiddleware::class, $aliases); } From ba80db2fc3985b042847094df4cd5cad88a974ff Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 6 Feb 2018 09:44:51 -0600 Subject: [PATCH 5/6] Refactor NotFoundMiddlewareFactoryTest setup - Adds property for `$response` - Alters order of operations in `setUp` for consistency --- test/Container/NotFoundMiddlewareFactoryTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/Container/NotFoundMiddlewareFactoryTest.php b/test/Container/NotFoundMiddlewareFactoryTest.php index c25fcefb..9ccfcab0 100644 --- a/test/Container/NotFoundMiddlewareFactoryTest.php +++ b/test/Container/NotFoundMiddlewareFactoryTest.php @@ -22,10 +22,13 @@ class NotFoundMiddlewareFactoryTest extends TestCase /** @var ContainerInterface|ObjectProphecy */ private $container; + /** @var ResponseInterface|ObjectProphecy */ + private $response; + protected function setUp() { - $this->container = $this->prophesize(ContainerInterface::class); $this->response = $this->prophesize(ResponseInterface::class)->reveal(); + $this->container = $this->prophesize(ContainerInterface::class); $this->container->get(ResponseInterface::class)->willReturn($this->response); } From 5fdb648d6f139550043199cf31769f97259eae35 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 6 Feb 2018 09:56:36 -0600 Subject: [PATCH 6/6] Extracts CHANGELOG entries for #546 into 3.0.0alpha3 release --- CHANGELOG.md | 62 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af4f50b5..1578de1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -97,19 +135,18 @@ All notable changes to this project will be documented in this file, in reverse and removes the dependency http-interop/http-server-middleware. -- [#543](https://github.com/zendframework/zend-expressive/pull/543) and - [#546](https://github.com/zendframework/zend-expressive/pull/546) renames the +- [#543](https://github.com/zendframework/zend-expressive/pull/543) renames `Zend\Expressive\Middleware\NotFoundHandler` to `Zend\Expressive\Middleware\NotFoundMiddleware`, and its accompanying factory `Zend\Expressive\Container\NotFoundHandlerFactory` to - `Zend\Expressive\Container\NotFoundMiddlewareFactory`. Additionally, the - class now does the work of the former `Zend\Expressive\Delegate\NotFoundDelegate`, - and, as such, accepts the following constructor arguments: + `Zend\Expressive\Container\NotFoundMiddlewareFactory`. - - 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") +- [#543](https://github.com/zendframework/zend-expressive/pull/543) renames + `Zend\Expressive\Delegate\NotFoundDelegate` to + `Zend\Expressive\Handler\NotFoundHandler`, updating it to implement the PSR-15 + `RequestHandlerInterface`. It also renames the factory + `Zend\Expressive\Container\NotFoundDelegateFactory` to + `Zend\Expressive\Container\NotFoundHandlerFactory`. - [#543](https://github.com/zendframework/zend-expressive/pull/543) refactors `Zend\Expressive\Application` completely. @@ -188,13 +225,6 @@ All notable changes to this project will be documented in this file, in reverse - [#543](https://github.com/zendframework/zend-expressive/pull/543) removes support for http-interop/http-server-middleware. -- [#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. - - [#543](https://github.com/zendframework/zend-expressive/pull/543) removes the class `Zend\Expressive\Middleware\RouteMiddleware`. Use the `PathBasedRoutingMiddleware` or `RouteMiddleware` from zend-expressive-router