Skip to content

Commit

Permalink
Added possibility to set custom cors handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaj committed May 12, 2017
1 parent d0fd7ee commit 9af1fbe
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/ApiDecider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
];
}
}
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 9af1fbe

Please sign in to comment.