RouteDispatcher: Allow installation into sub directory

This commit is contained in:
Igor Scheller 2018-09-26 20:49:31 +02:00
parent 66038eda14
commit aa2124f655
2 changed files with 13 additions and 3 deletions

View File

@ -2,6 +2,7 @@
namespace Engelsystem\Middleware;
use Engelsystem\Http\Request;
use FastRoute\Dispatcher as FastRouteDispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@ -44,7 +45,12 @@ class RouteDispatcher implements MiddlewareInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$route = $this->dispatcher->dispatch($request->getMethod(), urldecode($request->getUri()->getPath()));
$path = $request->getUri()->getPath();
if ($request instanceof Request) {
$path = $request->getPathInfo();
}
$route = $this->dispatcher->dispatch($request->getMethod(), urldecode($path));
$status = $route[0];
if ($status == FastRouteDispatcher::NOT_FOUND) {

View File

@ -2,6 +2,7 @@
namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Http\Request;
use Engelsystem\Middleware\RouteDispatcher;
use FastRoute\Dispatcher as FastRouteDispatcher;
use PHPUnit\Framework\MockObject\MockObject;
@ -127,7 +128,7 @@ class RouteDispatcherTest extends TestCase
/** @var ResponseInterface|MockObject $response */
$response = $this->getMockForAbstractClass(ResponseInterface::class);
/** @var ServerRequestInterface|MockObject $request */
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
$request = $this->createMock(Request::class);
/** @var RequestHandlerInterface|MockObject $handler */
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
/** @var UriInterface|MockObject $uriInterface */
@ -139,9 +140,12 @@ class RouteDispatcherTest extends TestCase
$request->expects($this->atLeastOnce())
->method('getUri')
->willReturn($uriInterface);
$request->expects($this->atLeastOnce())
->method('getPathInfo')
->willReturn('/foo%21bar');
$uriInterface->expects($this->atLeastOnce())
->method('getPath')
->willReturn('/foo%21bar');
->willReturn('/lorem/foo%21bar');
return [$dispatcher, $response, $request, $handler];
}