diff --git a/src/EventSubscriber/ApiExceptionSubscriber.php b/src/EventSubscriber/ApiExceptionSubscriber.php index 01b4e31a8..4f9bcdfe4 100644 --- a/src/EventSubscriber/ApiExceptionSubscriber.php +++ b/src/EventSubscriber/ApiExceptionSubscriber.php @@ -27,6 +27,8 @@ */ final class ApiExceptionSubscriber implements EventSubscriberInterface { + use StudioApiPathTrait; + public static function getSubscribedEvents(): array { return [ @@ -39,7 +41,7 @@ public function onKernelException(ExceptionEvent $event): void $exception = $event->getThrowable(); $request = $event->getRequest(); - if(!$exception instanceof ApiExceptionInterface && !$this->isStudioApiCall($request->getPathInfo())) { + if(!$exception instanceof ApiExceptionInterface && !$this->isStudioApiPath($request->getPathInfo())) { return; } /** @var ApiExceptionInterface $exception */ @@ -52,9 +54,4 @@ public function onKernelException(ExceptionEvent $event): void $event->setResponse($response); } - - private function isStudioApiCall(string $path): bool - { - return str_starts_with($path, AbstractApiController::API_PATH); - } } diff --git a/src/EventSubscriber/CorsSubscriber.php b/src/EventSubscriber/CorsSubscriber.php index 3106ad26d..435a48b3e 100644 --- a/src/EventSubscriber/CorsSubscriber.php +++ b/src/EventSubscriber/CorsSubscriber.php @@ -25,6 +25,8 @@ final readonly class CorsSubscriber implements EventSubscriberInterface { + use StudioApiPathTrait; + public function __construct(private array $allowedHosts = []) { } @@ -46,7 +48,7 @@ public function onKernelRequest(RequestEvent $event): void $request = $event->getRequest(); - if(!$this->checkStudioApiPath($request->getPathInfo())) { + if(!$this->isStudioApiPath($request->getPathInfo())) { return; } @@ -67,11 +69,11 @@ public function onKernelResponse(ResponseEvent $event): void { $request = $event->getRequest(); - if(!$this->checkStudioApiPath($request->getPathInfo())) { + if (!$this->isStudioApiPath($request->getPathInfo())) { return; } // Run CORS check in here to ensure domain is in the system - $corsOrigin = $request->headers->get('origin'); + $corsOrigin = $request->headers->get('origin'); if (in_array($corsOrigin, $this->allowedHosts, true)) { $response = $event->getResponse(); @@ -84,9 +86,4 @@ public function onKernelResponse(ResponseEvent $event): void } } - - private function checkStudioApiPath(string $path): bool - { - return str_contains($path, AbstractApiController::API_PATH); - } } diff --git a/src/EventSubscriber/StudioApiPathTrait.php b/src/EventSubscriber/StudioApiPathTrait.php new file mode 100644 index 000000000..233d9c477 --- /dev/null +++ b/src/EventSubscriber/StudioApiPathTrait.php @@ -0,0 +1,30 @@ +