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

Commit

Permalink
Merge branch 'hotfix/165'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Apr 4, 2018
2 parents 0fd13bb + e08233a commit 3e6bef7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,26 @@ details.

### Fixed

- Nothing.
- [#165](https://github.com/zendframework/zend-stratigility/pull/165) fixes an
issue with the `PathMiddlewareDecorator` whereby it was using the original
request when invoking the handler it creates, instead of prepending the
configured path prefix to the request URI created. With the fix, if middleware
alters the request path passed to the handler, the changes will now propagate
to later middleware. As an example:

```php
new PathMiddlewareDecorator('/api', middleware(function ($request, $handler) {
$uri = $request->getUri();
if (! preg_match('#^/v\d+/#', $uri->getPath())) {
$request = $request->withUri($uri->withPath('/v1' . $uri->getPath()));
}
return $handler->handle($request);
}));
```

For the request path `/api/books`, the above will now correctly result in
`/api/v1/books` being propagated to lower layers of the application, instead
of `/api/books`.

## 3.0.0 - 2018-03-15

Expand Down
22 changes: 10 additions & 12 deletions src/Middleware/PathMiddlewareDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// layer.
return $this->middleware->process(
$requestToProcess,
$this->prepareHandlerForOriginalRequest($handler, $request)
$this->prepareHandlerForOriginalRequest($handler)
);
}

Expand Down Expand Up @@ -97,21 +97,19 @@ private function getTruncatedPath(string $segment, string $path) : string
return substr($path, strlen($segment));
}

private function prepareHandlerForOriginalRequest(
RequestHandlerInterface $handler,
ServerRequestInterface $originalRequest
) : RequestHandlerInterface {
return new class ($handler, $originalRequest) implements RequestHandlerInterface {
private function prepareHandlerForOriginalRequest(RequestHandlerInterface $handler) : RequestHandlerInterface
{
return new class ($handler, $this->prefix) implements RequestHandlerInterface {
/** @var RequestHandlerInterface */
private $handler;

/** @var ServerRequestInterface */
private $originalRequest;
/** @var string */
private $prefix;

public function __construct(RequestHandlerInterface $handler, ServerRequestInterface $originalRequest)
public function __construct(RequestHandlerInterface $handler, string $prefix)
{
$this->handler = $handler;
$this->originalRequest = $originalRequest;
$this->prefix = $prefix;
}

/**
Expand All @@ -126,8 +124,8 @@ public function __construct(RequestHandlerInterface $handler, ServerRequestInter
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$uri = $request->getUri()
->withPath($this->originalRequest->getUri()->getPath());
$uri = $request->getUri();
$uri = $uri->withPath($this->prefix . $uri->getPath());
return $this->handler->handle($request->withUri($uri));
}
};
Expand Down
22 changes: 22 additions & 0 deletions test/Middleware/PathMiddlewareDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use function sprintf;
use function var_export;
use function Zend\Stratigility\middleware;
use function Zend\Stratigility\path;

class PathMiddlewareDecoratorTest extends TestCase
Expand Down Expand Up @@ -435,4 +436,25 @@ public function testPathFunction()
self::assertAttributeSame('/foo', 'prefix', $middleware);
self::assertAttributeSame($toDecorate, 'middleware', $middleware);
}

public function testUpdatesInPathInsideNestedMiddlewareAreRespected()
{
$request = new ServerRequest([], [], 'http://local.example.com/foo/bar', 'GET', 'php://memory');
$decoratedMiddleware = middleware(function (
ServerRequestInterface $request,
RequestHandlerInterface $handler
) {
return $handler->handle($request->withUri(new Uri('/changed/path')));
});
$middleware = new PathMiddlewareDecorator('/foo', $decoratedMiddleware);

$handler = $this->prophesize(RequestHandlerInterface::class);
$handler->handle(Argument::that(function (ServerRequestInterface $received) {
Assert::assertEquals('/foo/changed/path', $received->getUri()->getPath());

return $received;
}));

$middleware->process($request, $handler->reveal());
}
}

0 comments on commit 3e6bef7

Please sign in to comment.