custom/plugins/AsusBuyNow/src/Middleware/CorsMiddleware.php line 34

Open in your IDE?
  1. <?php
  2. namespace AsusBuyNow\Middleware;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Psr\Log\LoggerInterface;
  7. class CorsMiddleware implements EventSubscriberInterface
  8. {
  9.     private const ASUS_ALLOWED_ORIGINS = [
  10.         "https://invoices.hashtages.eu",
  11.         "https://www.asus.com",
  12.         "https://odinview.asus.com",
  13.         "https://rogmars.asus.com",
  14.         "https://rog.asus.com"
  15.     ];
  16.     private $logger;
  17.     public function __construct(
  18.         LoggerInterface $logger
  19.     ) {
  20.         $this->logger $logger;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             KernelEvents::RESPONSE => ['onResponse', -20],
  26.         ];
  27.     }
  28.     public function onResponse(ResponseEvent $event): void
  29.     {
  30.         // Add CORS headers to the response
  31.         $response $event->getResponse();
  32.         $origin $event->getRequest()->headers->get('Origin');
  33.         if(in_array($originself::ASUS_ALLOWED_ORIGINS)) {
  34.             $response->headers->set('Access-Control-Allow-Origin'$origin);
  35.             $response->headers->set('Access-Control-Allow-Methods''GET, POST, OPTIONS');
  36.             $response->headers->set('Access-Control-Allow-Headers''Content-Type');
  37.         }
  38.     }
  39. }