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

Commit cccd2b6

Browse files
committed
Merge branch 'feature/561-response-factories' into release-3.0.0
Close #561 Fixes #560
2 parents f47a912 + bb6b8f3 commit cccd2b6

17 files changed

+150
-146
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@ All notable changes to this project will be documented in this file, in reverse
1515
to the `ApplicationFactory`. It now uses the canonical service name for the
1616
`PathBasedRoutingMiddleware` instead of the `ROUTE_MIDDLEWARE` constant.
1717

18+
- [#561](https://github.com/zendframework/zend-expressive/pull/561) updates to
19+
zend-expressive-router 3.0.0alpha3.
20+
21+
- [#561](https://github.com/zendframework/zend-expressive/pull/561) renames
22+
`Zend\Expressive\Container\ResponseFactory` to `Zend\Expressive\Container\ResponseFactoryFactory`,
23+
and the factory now returns a callable that will return a zend-diactoros
24+
`Response` instance, instead of returning the instance itself. Each of the
25+
various services named after zend-expressive-router response constants were
26+
removed in favor of a single `Psr\Http\Message\ResponseInterface` service
27+
resolving to the `ResponseFactoryFactory`.
28+
29+
- [#561](https://github.com/zendframework/zend-expressive/pull/561) modifies the
30+
`Zend\Expressive\Handler\NotFoundHandler` to compose a response factory
31+
instead of a response prototype. This approach allows it to use the
32+
`Psr\Http\Message\ResponseInterface` service defined per the above note.
33+
34+
- [#561](https://github.com/zendframework/zend-expressive/pull/561) renames
35+
the `Zend\Expressive\Router\IMPLICIT_HEAD_MIDDLEWARE_STREAM_FACTORY` service
36+
to `Psr\Http\Message\StreamInterface`, as this is what zend-expressive-router
37+
now expects.
38+
39+
- [#561](https://github.com/zendframework/zend-expressive/pull/561) renames the
40+
`Zend\Expressive\ServerRequestFactory` service to
41+
`Psr\Http\Message\ServerRequestInterface`. The
42+
`Zend\Expressive\SERVER_REQUEST_FACTORY` constant now resolves to the
43+
interface name.
44+
1845
### Deprecated
1946

2047
- Nothing.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"psr/http-message": "^1.0.1",
2929
"psr/http-server-middleware": "^1.0",
3030
"zendframework/zend-diactoros": "^1.3.10",
31-
"zendframework/zend-expressive-router": "^3.0.0alpha2",
31+
"zendframework/zend-expressive-router": "^3.0.0alpha3",
3232
"zendframework/zend-expressive-template": "^2.0.0alpha1",
3333
"zendframework/zend-httphandlerrunner": "^1.0",
3434
"zendframework/zend-stratigility": "3.0.0alpha3"

composer.lock

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
colors="true">
66
<testsuites>
77
<testsuite name="zend-expressive-response-factory">
8-
<file>./test/Container/ResponseFactoryWithoutDiactorosTest.php</file>
8+
<file>./test/Container/ResponseFactoryFactoryWithoutDiactorosTest.php</file>
99
<file>./test/Container/StreamFactoryFactoryWithoutDiactorosTest.php</file>
1010
</testsuite>
1111
<testsuite name="zend-expressive">
1212
<directory>./test</directory>
13-
<exclude>./test/Container/ResponseFactoryWithoutDiactorosTest.php</exclude>
13+
<exclude>./test/Container/ResponseFactoryFactoryWithoutDiactorosTest.php</exclude>
1414
<exclude>./test/Container/StreamFactoryFactoryWithoutDiactorosTest.php</exclude>
1515
</testsuite>
1616
</testsuites>

src/ConfigProvider.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace Zend\Expressive;
1111

12+
use Psr\Http\Message\ResponseInterface;
13+
use Psr\Http\Message\ServerRequestInterface;
14+
use Psr\Http\Message\StreamInterface;
1215
use Zend\HttpHandlerRunner\Emitter\EmitterInterface;
1316
use Zend\HttpHandlerRunner\RequestHandlerRunner;
1417
use Zend\Stratigility\Middleware\ErrorHandler;
@@ -29,7 +32,6 @@ public function __invoke() : array
2932

3033
public function getDependencies() : array
3134
{
32-
// @codingStandardsIgnoreStart
3335
return [
3436
'aliases' => [
3537
DEFAULT_DELEGATE => Handler\NotFoundHandler::class,
@@ -40,25 +42,21 @@ public function getDependencies() : array
4042
ROUTE_MIDDLEWARE => Router\Middleware\PathBasedRoutingMiddleware::class,
4143
],
4244
'factories' => [
43-
Application::class => Container\ApplicationFactory::class,
44-
ApplicationPipeline::class => Container\ApplicationPipelineFactory::class,
45-
EmitterInterface::class => Container\EmitterFactory::class,
46-
ErrorHandler::class => Container\ErrorHandlerFactory::class,
47-
Handler\NotFoundHandler::class => Container\NotFoundHandlerFactory::class,
48-
MiddlewareContainer::class => Container\MiddlewareContainerFactory::class,
49-
MiddlewareFactory::class => Container\MiddlewareFactoryFactory::class,
45+
Application::class => Container\ApplicationFactory::class,
46+
ApplicationPipeline::class => Container\ApplicationPipelineFactory::class,
47+
EmitterInterface::class => Container\EmitterFactory::class,
48+
ErrorHandler::class => Container\ErrorHandlerFactory::class,
49+
Handler\NotFoundHandler::class => Container\NotFoundHandlerFactory::class,
50+
MiddlewareContainer::class => Container\MiddlewareContainerFactory::class,
51+
MiddlewareFactory::class => Container\MiddlewareFactoryFactory::class,
5052
// Change the following in development to the WhoopsErrorResponseGeneratorFactory:
51-
Middleware\ErrorResponseGenerator::class => Container\ErrorResponseGeneratorFactory::class,
52-
NOT_FOUND_RESPONSE => Container\ResponseFactory::class,
53-
RequestHandlerRunner::class => Container\RequestHandlerRunnerFactory::class,
54-
Router\IMPLICIT_HEAD_MIDDLEWARE_RESPONSE => Container\ResponseFactory::class,
55-
Router\IMPLICIT_HEAD_MIDDLEWARE_STREAM_FACTORY => Container\StreamFactoryFactory::class,
56-
Router\IMPLICIT_OPTIONS_MIDDLEWARE_RESPONSE => Container\ResponseFactory::class,
57-
Router\METHOD_NOT_ALLOWED_MIDDLEWARE_RESPONSE => Container\ResponseFactory::class,
58-
SERVER_REQUEST_ERROR_RESPONSE_GENERATOR => Container\ServerRequestErrorResponseGeneratorFactory::class,
59-
SERVER_REQUEST_FACTORY => Container\ServerRequestFactoryFactory::class,
53+
Middleware\ErrorResponseGenerator::class => Container\ErrorResponseGeneratorFactory::class,
54+
RequestHandlerRunner::class => Container\RequestHandlerRunnerFactory::class,
55+
ResponseInterface::class => Container\ResponseFactoryFactory::class,
56+
SERVER_REQUEST_ERROR_RESPONSE_GENERATOR => Container\ServerRequestErrorResponseGeneratorFactory::class,
57+
ServerRequestInterface::class => Container\ServerRequestFactoryFactory::class,
58+
StreamInterface::class => Container\StreamFactoryFactory::class,
6059
],
6160
];
62-
// @codingStandardsIgnoreEnd
6361
}
6462
}

src/Container/NotFoundHandlerFactory.php

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

1212
use Psr\Container\ContainerInterface;
13+
use Psr\Http\Message\ResponseInterface;
1314
use Zend\Expressive\Handler\NotFoundHandler;
14-
use Zend\Expressive\Response\NotFoundResponseInterface;
1515
use Zend\Expressive\Template\TemplateRendererInterface;
1616

17-
use const Zend\Expressive\NOT_FOUND_RESPONSE;
18-
1917
class NotFoundHandlerFactory
2018
{
2119
public function __invoke(ContainerInterface $container) : NotFoundHandler
@@ -30,7 +28,7 @@ public function __invoke(ContainerInterface $container) : NotFoundHandler
3028
?? NotFoundHandler::LAYOUT_DEFAULT;
3129

3230
return new NotFoundHandler(
33-
$container->get(NOT_FOUND_RESPONSE),
31+
$container->get(ResponseInterface::class),
3432
$renderer,
3533
$template,
3634
$layout

src/Container/RequestHandlerRunnerFactory.php

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

1212
use Psr\Container\ContainerInterface;
13+
use Psr\Http\Message\ServerRequestInterface;
1314
use Zend\Expressive\ApplicationPipeline;
1415
use Zend\Expressive\ServerRequestErrorResponseGenerator;
15-
use Zend\Expressive\ServerRequestFactory;
1616
use Zend\HttpHandlerRunner\Emitter\EmitterInterface;
1717
use Zend\HttpHandlerRunner\RequestHandlerRunner;
1818

@@ -25,7 +25,7 @@
2525
* - Zend\Expressive\ApplicationPipeline, which should resolve to a
2626
* Zend\Stratigility\MiddlewarePipeInterface and/or
2727
* Psr\Http\Server\RequestHandlerInterface instance.
28-
* - Zend\Expressive\ServerRequestFactory, which should resolve to a PHP
28+
* - Psr\Http\Message\ServerRequestInterface, which should resolve to a PHP
2929
* callable that will return a Psr\Http\Message\ServerRequestInterface
3030
* instance.
3131
* - Zend\Expressive\ServerRequestErrorResponseGenerator, which should resolve
@@ -41,7 +41,7 @@ public function __invoke(ContainerInterface $container) : RequestHandlerRunner
4141
return new RequestHandlerRunner(
4242
$container->get(ApplicationPipeline::class),
4343
$container->get(EmitterInterface::class),
44-
$container->get(ServerRequestFactory::class),
44+
$container->get(ServerRequestInterface::class),
4545
$container->get(ServerRequestErrorResponseGenerator::class)
4646
);
4747
}

src/Container/ResponseFactory.php renamed to src/Container/ResponseFactoryFactory.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,27 @@
1414
use Zend\Diactoros\Response;
1515

1616
/**
17-
* Produces a response prototype for use with services that need to
18-
* produce a response. This service should be non-shared, if your
19-
* container supports that possibility, to ensure that the stream
20-
* it composes cannot be written to by any other consumer.
17+
* Produces a callable capable of producing a response prototype for use with
18+
* services that need to produce a response.
2119
*/
22-
class ResponseFactory
20+
class ResponseFactoryFactory
2321
{
24-
public function __invoke(ContainerInterface $container) : ResponseInterface
22+
public function __invoke(ContainerInterface $container) : callable
2523
{
2624
if (! class_exists(Response::class)) {
2725
throw new Exception\InvalidServiceException(sprintf(
28-
'The %s service must map to a factory capable of returning an'
26+
'The %1$s service must map to a factory capable of returning an'
2927
. ' implementation instance. By default, we assume usage of'
3028
. ' zend-diactoros for PSR-7, but it does not appear to be'
3129
. ' present on your system. Please install zendframework/zend-diactoros'
32-
. ' or provide an alternate factory for the %s service that'
33-
. ' can produce an appropriate %s instance.',
34-
ResponseInterface::class,
35-
ResponseInterface::class,
30+
. ' or provide an alternate factory for the %1$s service that'
31+
. ' can produce an appropriate %1$s instance.',
3632
ResponseInterface::class
3733
));
3834
}
3935

40-
return new Response();
36+
return function () : Response {
37+
return new Response();
38+
};
4139
}
4240
}

src/Handler/NotFoundHandler.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ class NotFoundHandler implements RequestHandlerInterface
2626
private $renderer;
2727

2828
/**
29-
* This duplicates the property in StratigilityNotFoundHandler, but is done
30-
* to ensure that we have access to the value in the methods we override.
31-
*
32-
* @var ResponseInterface
29+
* @var callable
3330
*/
34-
protected $responsePrototype;
31+
private $responseFactory;
3532

3633
/**
3734
* @var string
@@ -44,12 +41,15 @@ class NotFoundHandler implements RequestHandlerInterface
4441
private $layout;
4542

4643
public function __construct(
47-
ResponseInterface $responsePrototype,
44+
callable $responseFactory,
4845
TemplateRendererInterface $renderer = null,
4946
string $template = self::TEMPLATE_DEFAULT,
5047
string $layout = self::LAYOUT_DEFAULT
5148
) {
52-
$this->responsePrototype = $responsePrototype;
49+
// Factory cast to closure in order to provide return type safety.
50+
$this->responseFactory = function () use ($responseFactory) : ResponseInterface {
51+
return $responseFactory();
52+
};
5353
$this->renderer = $renderer;
5454
$this->template = $template;
5555
$this->layout = $layout;
@@ -75,7 +75,7 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
7575
*/
7676
private function generatePlainTextResponse(ServerRequestInterface $request) : ResponseInterface
7777
{
78-
$response = $this->responsePrototype->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
78+
$response = ($this->responseFactory)()->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
7979
$response->getBody()
8080
->write(sprintf(
8181
'Cannot %s %s',
@@ -93,7 +93,7 @@ private function generatePlainTextResponse(ServerRequestInterface $request) : Re
9393
*/
9494
private function generateTemplatedResponse(ServerRequestInterface $request) : ResponseInterface
9595
{
96-
$response = $this->responsePrototype->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
96+
$response = ($this->responseFactory)()->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
9797
$response->getBody()->write(
9898
$this->renderer->render($this->template, ['request' => $request, 'layout' => $this->layout])
9999
);

src/constants.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Zend\Expressive;
1111

12+
use Psr\Http\Message\ServerRequestInterface;
13+
1214
/**
1315
* Legacy service name for the default delegate referenced in version 2.
1416
* Should resolve to the Handler\NotFoundHandler class.
@@ -57,14 +59,6 @@
5759
*/
5860
const NOT_FOUND_MIDDLEWARE = __NAMESPACE__ . '\Middleware\NotFoundMiddleware';
5961

60-
/**
61-
* Virtual service name that should resolve to a service returning a PSR-7
62-
* ResponseInterface instance for use with the Handler\NotFoundHandler class.
63-
*
64-
* @var string
65-
*/
66-
const NOT_FOUND_RESPONSE = __NAMESPACE__ . '\Response\NotFoundResponseInterface';
67-
6862
/**
6963
* Legacy service name for the RouteMiddleware referenced in version 2.
7064
* Should resolve to the Zend\Expressive\Router\Middleware\PathBasedRoutingMiddleware
@@ -85,9 +79,11 @@
8579
const SERVER_REQUEST_ERROR_RESPONSE_GENERATOR = __NAMESPACE__ . '\ServerRequestErrorResponseGenerator';
8680

8781
/**
88-
* Virtual service name that should resolve to a service capable of producing
89-
* a PSR-7 ServerRequestInterface instance for the application.
82+
* Legacy/transitional service name for the ServerRequestFactory virtual
83+
* service introduced in 3.0.0alpha6. Should resolve to the
84+
* Psr\Http\Message\ServerRequestInterface service.
9085
*
86+
* @deprecated To remove in version 4.0.0.
9187
* @var string
9288
*/
93-
const SERVER_REQUEST_FACTORY = __NAMESPACE__ . '\ServerRequestFactory';
89+
const SERVER_REQUEST_FACTORY = ServerRequestInterface::class;

0 commit comments

Comments
 (0)