neomerx / cors-psr7

Framework agnostic (PSR-7) CORS implementation (www.w3.org/TR/cors)
Apache License 2.0
65 stars 14 forks source link

Update to v2 #32

Closed neomerx closed 5 years ago

neomerx commented 5 years ago
tuupola commented 5 years ago

What are the BC breaks compared to 1.x?

neomerx commented 5 years ago

@tuupola new class for settings. Also strong typing everywhere which might require method signature updates. I've already migrated one of the projects and only settings part needed updates. Overall it was rather easy. BTW settings now support caching.

neomerx commented 5 years ago

@tuupola For you, I think, it will only be needed to change buildSettings to something like

private function buildSettings(ServerRequestInterface $request, ResponseInterface $response): AnalysisStrategyInterface
{
    $cacheIsAvailable = ...;
    if ($cacheIsAvailable === true) {
        $cachedData = $this->getCorsCachedSettings();
        return (new CorsSettings())->setData($cachedData);
    }
    $settings = (new CorsSettings())
        ->setServerOrigin('https', 'api.example.com', 443)
        ->setPreFlightCacheMaxAge(0)
        ->setCredentialsSupported()
        ->setAllowedOrigins(['https://www.example.com', ...]) // or enableAllOriginsAllowed()
        ->setAllowedMethods(['GET', 'POST', 'DELETE', ...])   // or enableAllMethodsAllowed()
        ->setAllowedHeaders(['X-Custom-Header', ...])         // or enableAllHeadersAllowed()
        ->setExposedHeaders(['X-Custom-Header', ...])
        ->disableAddAllowedMethodsToPreFlightResponse()
        ->disableAddAllowedHeadersToPreFlightResponse()
        ->enableCheckHost();

    // cache settings
    $dataToCache = $settings->getData();
    ...

    return $settings;
}
tuupola commented 5 years ago

Quickly testing it also seems that calling ->setServerOrigin() is now mandatory.