ljoaquim 5 months ago
parent
commit
dcbf74c104
1 changed files with 27 additions and 10 deletions
  1. 27 10
      middlewares/CorsControl.php

+ 27 - 10
middlewares/CorsControl.php

@@ -7,28 +7,45 @@ use React\Http\Message\Response;
 
 class CorsControl
 {
-    public function __invoke(ServerRequestInterface $request, callable $next)
+    public function __invoke(ServerRequestInterface $request, $next)
     {
-        // Cabeçalhos CORS liberais para todas as origens
+        // 1) Configurações CORS completamente abertas:
         $corsHeaders = [
-            'Access-Control-Allow-Origin'      => '*',
-            'Access-Control-Allow-Methods'     => 'GET, POST',
+            'Access-Control-Allow-Origin'      => '0.0.0.0',
+            'Access-Control-Allow-Methods'     => 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
             'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With, Accept, Origin',
             'Access-Control-Allow-Credentials' => 'true',
-            'Access-Control-Max-Age'           => '86400', // 24 horas em segundos
+            'Access-Control-Max-Age'           => '86400', // cache de preflight por 24h
         ];
 
-        // Responde imediatamente a preflight OPTIONS
+        // 2) Responde direto a preflight
         if ($request->getMethod() === 'OPTIONS') {
             return new Response(204, $corsHeaders);
         }
 
-        // Executa o próximo middleware / controlador
+        // 3) Se o “next” vier como string (nome de classe), instancia‑o:
+        if (is_string($next) && class_exists($next)) {
+            $instance = new $next();
+
+            // se tiver __invoke, use-o
+            if (is_callable($instance)) {
+                $next = $instance;
+            }
+            // caso seu controller siga outro padrão PSR-15, adapte aqui:
+            // elseif (method_exists($instance, 'handle')) {
+            //     $next = [$instance, 'handle'];
+            // }
+            else {
+                throw new \RuntimeException("Controller “{$next}” não é callable");
+            }
+        }
+
+        // 4) Chama o próximo handler (agora garantidamente callable)
         $response = $next($request);
 
-        // Injeta os headers CORS na resposta
-        foreach ($corsHeaders as $header => $value) {
-            $response = $response->withHeader($header, $value);
+        // 5) Injeta os headers CORS na resposta
+        foreach ($corsHeaders as $h => $v) {
+            $response = $response->withHeader($h, $v);
         }
 
         return $response;