diff --git a/config/autoload/routes.global.php b/config/autoload/routes.global.php index 06e5f733..87133f09 100644 --- a/config/autoload/routes.global.php +++ b/config/autoload/routes.global.php @@ -23,13 +23,13 @@ return [ 'name' => 'rest-create-shortcode', 'path' => '/rest/short-codes', 'middleware' => Rest\CreateShortcodeMiddleware::class, - 'allowed_methods' => ['POST'], + 'allowed_methods' => ['POST', 'OPTIONS'], ], [ 'name' => 'rest-resolve-url', 'path' => '/rest/short-codes/{shortCode}', 'middleware' => Rest\ResolveUrlMiddleware::class, - 'allowed_methods' => ['GET'], + 'allowed_methods' => ['GET', 'OPTIONS'], ], [ 'name' => 'rest-list-shortened-url', @@ -41,7 +41,7 @@ return [ 'name' => 'rest-get-visits', 'path' => '/rest/visits/{shortCode}', 'middleware' => Rest\GetVisitsMiddleware::class, - 'allowed_methods' => ['GET'], + 'allowed_methods' => ['GET', 'OPTIONS'], ], ], diff --git a/src/Middleware/CheckAuthenticationMiddleware.php b/src/Middleware/CheckAuthenticationMiddleware.php index 92081ad8..8f7327e7 100644 --- a/src/Middleware/CheckAuthenticationMiddleware.php +++ b/src/Middleware/CheckAuthenticationMiddleware.php @@ -59,10 +59,12 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface */ public function __invoke(Request $request, Response $response, callable $out = null) { - // If current route is the authenticate route, continue to the next middleware + // If current route is the authenticate route or an OPTIONS request, continue to the next middleware /** @var RouteResult $routeResult */ $routeResult = $request->getAttribute(RouteResult::class); - if (isset($routeResult) && $routeResult->getMatchedRouteName() === 'rest-authenticate') { + if ((isset($routeResult) && $routeResult->getMatchedRouteName() === 'rest-authenticate') + || strtolower($request->getMethod()) === 'options' + ) { return $out($request, $response); } diff --git a/src/Middleware/Rest/AbstractRestMiddleware.php b/src/Middleware/Rest/AbstractRestMiddleware.php new file mode 100644 index 00000000..1168ff60 --- /dev/null +++ b/src/Middleware/Rest/AbstractRestMiddleware.php @@ -0,0 +1,51 @@ +getMethod()) === 'options') { + return $response; + } + + return $this->dispatch($request, $response, $out); + } + + /** + * @param Request $request + * @param Response $response + * @param callable|null $out + * @return null|Response + */ + abstract protected function dispatch(Request $request, Response $response, callable $out = null); +} diff --git a/src/Middleware/Rest/AuthenticateMiddleware.php b/src/Middleware/Rest/AuthenticateMiddleware.php index 85d12330..88c9df60 100644 --- a/src/Middleware/Rest/AuthenticateMiddleware.php +++ b/src/Middleware/Rest/AuthenticateMiddleware.php @@ -9,9 +9,8 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Zend\Diactoros\Response\JsonResponse; -use Zend\Stratigility\MiddlewareInterface; -class AuthenticateMiddleware implements MiddlewareInterface +class AuthenticateMiddleware extends AbstractRestMiddleware { /** * @var RestTokenServiceInterface @@ -30,36 +29,13 @@ class AuthenticateMiddleware implements MiddlewareInterface } /** - * Process an incoming request and/or response. - * - * Accepts a server-side request and a response instance, and does - * something with them. - * - * If the response is not complete and/or further processing would not - * interfere with the work done in the middleware, or if the middleware - * wants to delegate to another process, it can use the `$out` callable - * if present. - * - * If the middleware does not return a value, execution of the current - * request is considered complete, and the response instance provided will - * be considered the response to return. - * - * Alternately, the middleware may return a response instance. - * - * Often, middleware will `return $out();`, with the assumption that a - * later middleware will return a response. - * * @param Request $request * @param Response $response - * @param null|callable $out + * @param callable|null $out * @return null|Response */ - public function __invoke(Request $request, Response $response, callable $out = null) + public function dispatch(Request $request, Response $response, callable $out = null) { - if (strtolower($request->getMethod()) === 'options') { - return $response; - } - $authData = $request->getParsedBody(); if (! isset($authData['username'], $authData['password'])) { return new JsonResponse([ diff --git a/src/Middleware/Rest/CreateShortcodeMiddleware.php b/src/Middleware/Rest/CreateShortcodeMiddleware.php index b68c551c..f5ee3228 100644 --- a/src/Middleware/Rest/CreateShortcodeMiddleware.php +++ b/src/Middleware/Rest/CreateShortcodeMiddleware.php @@ -10,9 +10,8 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\Uri; -use Zend\Stratigility\MiddlewareInterface; -class CreateShortcodeMiddleware implements MiddlewareInterface +class CreateShortcodeMiddleware extends AbstractRestMiddleware { /** * @var UrlShortener|UrlShortenerInterface @@ -38,31 +37,12 @@ class CreateShortcodeMiddleware implements MiddlewareInterface } /** - * Process an incoming request and/or response. - * - * Accepts a server-side request and a response instance, and does - * something with them. - * - * If the response is not complete and/or further processing would not - * interfere with the work done in the middleware, or if the middleware - * wants to delegate to another process, it can use the `$out` callable - * if present. - * - * If the middleware does not return a value, execution of the current - * request is considered complete, and the response instance provided will - * be considered the response to return. - * - * Alternately, the middleware may return a response instance. - * - * Often, middleware will `return $out();`, with the assumption that a - * later middleware will return a response. - * * @param Request $request * @param Response $response - * @param null|callable $out + * @param callable|null $out * @return null|Response */ - public function __invoke(Request $request, Response $response, callable $out = null) + public function dispatch(Request $request, Response $response, callable $out = null) { $postData = $request->getParsedBody(); if (! isset($postData['longUrl'])) { diff --git a/src/Middleware/Rest/GetVisitsMiddleware.php b/src/Middleware/Rest/GetVisitsMiddleware.php index 1a1b973b..a6ca954e 100644 --- a/src/Middleware/Rest/GetVisitsMiddleware.php +++ b/src/Middleware/Rest/GetVisitsMiddleware.php @@ -9,9 +9,8 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Zend\Diactoros\Response\JsonResponse; -use Zend\Stratigility\MiddlewareInterface; -class GetVisitsMiddleware implements MiddlewareInterface +class GetVisitsMiddleware extends AbstractRestMiddleware { /** * @var VisitsTrackerInterface @@ -30,31 +29,12 @@ class GetVisitsMiddleware implements MiddlewareInterface } /** - * Process an incoming request and/or response. - * - * Accepts a server-side request and a response instance, and does - * something with them. - * - * If the response is not complete and/or further processing would not - * interfere with the work done in the middleware, or if the middleware - * wants to delegate to another process, it can use the `$out` callable - * if present. - * - * If the middleware does not return a value, execution of the current - * request is considered complete, and the response instance provided will - * be considered the response to return. - * - * Alternately, the middleware may return a response instance. - * - * Often, middleware will `return $out();`, with the assumption that a - * later middleware will return a response. - * * @param Request $request * @param Response $response - * @param null|callable $out + * @param callable|null $out * @return null|Response */ - public function __invoke(Request $request, Response $response, callable $out = null) + public function dispatch(Request $request, Response $response, callable $out = null) { $shortCode = $request->getAttribute('shortCode'); diff --git a/src/Middleware/Rest/ListShortcodesMiddleware.php b/src/Middleware/Rest/ListShortcodesMiddleware.php index 6a4a627f..6b74241c 100644 --- a/src/Middleware/Rest/ListShortcodesMiddleware.php +++ b/src/Middleware/Rest/ListShortcodesMiddleware.php @@ -10,9 +10,8 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Zend\Diactoros\Response\JsonResponse; use Zend\Stdlib\ArrayUtils; -use Zend\Stratigility\MiddlewareInterface; -class ListShortcodesMiddleware implements MiddlewareInterface +class ListShortcodesMiddleware extends AbstractRestMiddleware { use PaginatorSerializerTrait; @@ -33,31 +32,12 @@ class ListShortcodesMiddleware implements MiddlewareInterface } /** - * Process an incoming request and/or response. - * - * Accepts a server-side request and a response instance, and does - * something with them. - * - * If the response is not complete and/or further processing would not - * interfere with the work done in the middleware, or if the middleware - * wants to delegate to another process, it can use the `$out` callable - * if present. - * - * If the middleware does not return a value, execution of the current - * request is considered complete, and the response instance provided will - * be considered the response to return. - * - * Alternately, the middleware may return a response instance. - * - * Often, middleware will `return $out();`, with the assumption that a - * later middleware will return a response. - * * @param Request $request * @param Response $response - * @param null|callable $out + * @param callable|null $out * @return null|Response */ - public function __invoke(Request $request, Response $response, callable $out = null) + public function dispatch(Request $request, Response $response, callable $out = null) { try { $query = $request->getQueryParams(); diff --git a/src/Middleware/Rest/ResolveUrlMiddleware.php b/src/Middleware/Rest/ResolveUrlMiddleware.php index 1beee164..4529e973 100644 --- a/src/Middleware/Rest/ResolveUrlMiddleware.php +++ b/src/Middleware/Rest/ResolveUrlMiddleware.php @@ -9,9 +9,8 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Zend\Diactoros\Response\JsonResponse; -use Zend\Stratigility\MiddlewareInterface; -class ResolveUrlMiddleware implements MiddlewareInterface +class ResolveUrlMiddleware extends AbstractRestMiddleware { /** * @var UrlShortenerInterface @@ -30,31 +29,12 @@ class ResolveUrlMiddleware implements MiddlewareInterface } /** - * Process an incoming request and/or response. - * - * Accepts a server-side request and a response instance, and does - * something with them. - * - * If the response is not complete and/or further processing would not - * interfere with the work done in the middleware, or if the middleware - * wants to delegate to another process, it can use the `$out` callable - * if present. - * - * If the middleware does not return a value, execution of the current - * request is considered complete, and the response instance provided will - * be considered the response to return. - * - * Alternately, the middleware may return a response instance. - * - * Often, middleware will `return $out();`, with the assumption that a - * later middleware will return a response. - * * @param Request $request * @param Response $response - * @param null|callable $out + * @param callable|null $out * @return null|Response */ - public function __invoke(Request $request, Response $response, callable $out = null) + public function dispatch(Request $request, Response $response, callable $out = null) { $shortCode = $request->getAttribute('shortCode');