From 1eac23003f41a5edfe541a715e6aba75aa23b2ad Mon Sep 17 00:00:00 2001 From: Calin Date: Sun, 4 Jul 2021 11:01:36 +0300 Subject: [PATCH 1/2] Replace tuupola/cors with mezzio/cors. --- composer.json | 4 +-- config/autoload/cors.global.php | 26 +++++++++------- config/autoload/dependencies.global.php | 2 -- config/config.php | 2 ++ config/pipeline.php | 2 +- src/App/src/Factory/CorsFactory.php | 40 ------------------------- src/App/src/RoutesDelegator.php | 3 ++ 7 files changed, 24 insertions(+), 55 deletions(-) delete mode 100644 src/App/src/Factory/CorsFactory.php diff --git a/composer.json b/composer.json index 0b87cb02..8f9a7406 100644 --- a/composer.json +++ b/composer.json @@ -62,13 +62,13 @@ "laminas/laminas-stdlib": "^3.2", "mezzio/mezzio": "^3.2", "mezzio/mezzio-authorization-rbac": "^1.0", + "mezzio/mezzio-cors": "^1.0", "mezzio/mezzio-fastroute": "^3.0", "mezzio/mezzio-helpers": "^5.3", "mezzio/mezzio-twigrenderer": "^2.2", "ramsey/uuid-doctrine": "^1.6", "roave/psr-container-doctrine": "^2.2", - "robmorgan/phinx": "^0.12", - "tuupola/cors-middleware": "^1.1" + "robmorgan/phinx": "^0.12" }, "require-dev": { "laminas/laminas-development-mode": "^3.2", diff --git a/config/autoload/cors.global.php b/config/autoload/cors.global.php index 6f924b97..a252337b 100644 --- a/config/autoload/cors.global.php +++ b/config/autoload/cors.global.php @@ -2,16 +2,22 @@ declare(strict_types=1); +use Mezzio\Cors\Configuration\ConfigurationInterface; + return [ - 'cors' => [ - 'origin' => ['*'], - 'methods' => ['DELETE', 'GET', 'OPTIONS', 'PATCH', 'POST', 'PUT'], - 'headers.allow' => ['Accept', 'Content-Type', 'Authorization'], - 'headers.expose' => [], - 'credentials' => false, - 'cache' => 0, - 'error' => [ - Frontend\App\Factory\CorsFactory::class, 'error' - ] + ConfigurationInterface::CONFIGURATION_IDENTIFIER => [ + 'allowed_origins' => [ + /** + * Leaving this line here makes your application accessible by any origin. + * + * To restrict, replace this line with a list of origins that should have access to your application. + * Example: "domain1.com", "domain2.com" + */ + ConfigurationInterface::ANY_ORIGIN + ], + 'allowed_headers' => ['Accept', 'Content-Type', 'Authorization'], // Custom headers + 'allowed_max_age' => '600', // 10 minutes + 'credentials_allowed' => true, // Allow cookies + 'exposed_headers' => [], // Tell client that the API will always return this header ], ]; diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index 128adfb8..2777182e 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -5,7 +5,6 @@ use Dot\Mail\Factory\MailOptionsAbstractFactory; use Dot\Mail\Factory\MailServiceAbstractFactory; use Dot\Mail\Service\MailService; -use Frontend\App\Factory\CorsFactory; use Dot\ErrorHandler\ErrorHandlerInterface; use Dot\ErrorHandler\LogErrorHandler; use Frontend\App\Middleware\AuthMiddleware; @@ -33,7 +32,6 @@ 'factories' => [ 'dot-mail.options.default' => MailOptionsAbstractFactory::class, 'dot-mail.service.default' => MailServiceAbstractFactory::class, - Tuupola\Middleware\CorsMiddleware::class => CorsFactory::class, AuthMiddleware::class => AuthMiddlewareFactory::class, ], ], diff --git a/config/config.php b/config/config.php index f33d5a0f..9a0625ac 100644 --- a/config/config.php +++ b/config/config.php @@ -14,6 +14,7 @@ $aggregator = new ConfigAggregator([ \Laminas\HttpHandlerRunner\ConfigProvider::class, + \Laminas\Diactoros\ConfigProvider::class, \Mezzio\Twig\ConfigProvider::class, \Mezzio\Router\FastRouteRouter\ConfigProvider::class, // Include cache configuration @@ -22,6 +23,7 @@ \Mezzio\Helper\ConfigProvider::class, \Mezzio\ConfigProvider::class, \Mezzio\Router\ConfigProvider::class, + \Mezzio\Cors\ConfigProvider::class, // Swoole config to overwrite some services (if installed) class_exists(\Mezzio\Swoole\ConfigProvider::class) diff --git a/config/pipeline.php b/config/pipeline.php index 3f5df32c..4809835d 100644 --- a/config/pipeline.php +++ b/config/pipeline.php @@ -5,6 +5,7 @@ use Dot\ErrorHandler\ErrorHandlerInterface; use Dot\ResponseHeader\Middleware\ResponseHeaderMiddleware; use Mezzio\Application; +use Mezzio\Cors\Middleware\CorsMiddleware; use Mezzio\Handler\NotFoundHandler; use Mezzio\Helper\ServerUrlMiddleware; use Mezzio\Helper\UrlHelperMiddleware; @@ -15,7 +16,6 @@ use Mezzio\Router\Middleware\MethodNotAllowedMiddleware; use Mezzio\Router\Middleware\RouteMiddleware; use Psr\Container\ContainerInterface; -use Tuupola\Middleware\CorsMiddleware; use Frontend\App\Middleware\TranslatorMiddleware; use Dot\Rbac\Guard\Middleware\ForbiddenHandler; use Dot\Rbac\Guard\Middleware\RbacGuardMiddleware; diff --git a/src/App/src/Factory/CorsFactory.php b/src/App/src/Factory/CorsFactory.php deleted file mode 100644 index 81553b3b..00000000 --- a/src/App/src/Factory/CorsFactory.php +++ /dev/null @@ -1,40 +0,0 @@ -get('config')['cors'] - ); - } - - /** - * @param RequestInterface $request - * @param ResponseInterface $response - * @param array $arguments - * @return JsonResponse - */ - public static function error(RequestInterface $request, ResponseInterface $response, array $arguments = []) - { - return new JsonResponse($arguments); - } -} diff --git a/src/App/src/RoutesDelegator.php b/src/App/src/RoutesDelegator.php index 7003e256..03cabbdc 100644 --- a/src/App/src/RoutesDelegator.php +++ b/src/App/src/RoutesDelegator.php @@ -4,9 +4,12 @@ use Fig\Http\Message\RequestMethodInterface; use Frontend\App\Controller\LanguageController; +use Laminas\Diactoros\Response\JsonResponse; use Mezzio\Application; use Psr\Container\ContainerInterface; +use function var_dump; + /** * Class RoutesDelegator * @package Frontend\App From e4ce948b21c029c3ecf9c3e9b9f9a4d2f59b79c8 Mon Sep 17 00:00:00 2001 From: Calin Date: Sun, 4 Jul 2021 11:37:30 +0300 Subject: [PATCH 2/2] Remove residual code. --- src/App/src/RoutesDelegator.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/App/src/RoutesDelegator.php b/src/App/src/RoutesDelegator.php index 03cabbdc..7003e256 100644 --- a/src/App/src/RoutesDelegator.php +++ b/src/App/src/RoutesDelegator.php @@ -4,12 +4,9 @@ use Fig\Http\Message\RequestMethodInterface; use Frontend\App\Controller\LanguageController; -use Laminas\Diactoros\Response\JsonResponse; use Mezzio\Application; use Psr\Container\ContainerInterface; -use function var_dump; - /** * Class RoutesDelegator * @package Frontend\App