From 9af1fbe51bc901916237821865cce9dc226ea7a3 Mon Sep 17 00:00:00 2001 From: Tomas Majer Date: Fri, 12 May 2017 08:35:39 +0200 Subject: [PATCH] Added possibility to set custom cors handler --- src/ApiDecider.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ApiDecider.php b/src/ApiDecider.php index 5a901b2..2de9243 100644 --- a/src/ApiDecider.php +++ b/src/ApiDecider.php @@ -17,9 +17,9 @@ class ApiDecider private $handlers = []; /** - * @var bool + * @var ApiHandlerInterface */ - private $globalPreflight = false; + private $globalPreflightHandler = null; /** * Get api handler that match input method, version, package and apiAction. @@ -41,11 +41,11 @@ public function getApiHandler($method, $version, $package, $apiAction = '') $handler['handler']->setEndpointIdentifier($endpointIdentifier); return $handler; } - if ($method == 'OPTIONS' && $this->globalPreflight && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) { + if ($method == 'OPTIONS' && $this->globalPreflightHandler && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) { return [ 'endpoint' => new EndpointIdentifier('OPTION', $version, $package, $apiAction), 'authorization' => new NoAuthorization(), - 'handler' => new CorsPreflightHandler(new Response()), + 'handler' => $this->globalPreflightHandler, ]; } } @@ -56,9 +56,12 @@ public function getApiHandler($method, $version, $package, $apiAction = '') ]; } - public function enableGlobalPreflight() + public function enableGlobalPreflight(ApiHandlerInterface $corsHandler = null) { - $this->globalPreflight = true; + if (!$corsHandler) { + $corsHandler = new CorsPreflightHandler(new Response()); + } + $this->globalPreflightHandler = $corsHandler; } /**