@@ -26,9 +26,7 @@ public function __construct(
2626 protected RequestContract $ request ,
2727 protected Cors $ cors ,
2828 ) {
29- $ this ->cors ->setOptions (
30- $ this ->config = $ container ->get (ConfigInterface::class)->get ('cors ' , [])
31- );
29+ $ this ->config = $ container ->get (ConfigInterface::class)->get ('cors ' , []);
3230 }
3331
3432 public function process (ServerRequestInterface $ request , RequestHandlerInterface $ handler ): ResponseInterface
@@ -37,9 +35,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3735 return $ handler ->handle ($ request );
3836 }
3937
40- if ($ this ->cors ->isPreflightRequest ($ this ->request )) {
41- $ response = $ this ->cors ->handlePreflightRequest ($ this ->request );
42- return $ this ->cors ->varyHeader ($ response , 'Access-Control-Request-Method ' );
38+ $ cors = $ this ->getCors ();
39+
40+ if ($ cors ->isPreflightRequest ($ this ->request )) {
41+ $ response = $ cors ->handlePreflightRequest ($ this ->request );
42+ return $ cors ->varyHeader ($ response , 'Access-Control-Request-Method ' );
4343 }
4444
4545 try {
@@ -60,11 +60,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6060 */
6161 protected function addRequestHeaders (ResponseInterface $ response ): ResponseInterface
6262 {
63+ $ cors = $ this ->getCors ();
64+
6365 if ($ this ->request ->getMethod () === 'OPTIONS ' ) {
64- $ response = $ this -> cors ->varyHeader ($ response , 'Access-Control-Request-Method ' );
66+ $ response = $ cors ->varyHeader ($ response , 'Access-Control-Request-Method ' );
6567 }
6668
67- return $ this -> cors ->addActualRequestHeaders ($ response , $ this ->request );
69+ return $ cors ->addActualRequestHeaders ($ response , $ this ->request );
6870 }
6971
7072 /**
@@ -101,4 +103,20 @@ protected function getPathsByHost(string $host): array
101103 return is_string ($ path );
102104 });
103105 }
106+
107+ /**
108+ * Get the Cors service instance.
109+ */
110+ protected function getCors (): Cors
111+ {
112+ return new Cors ($ this ->getCorsConfig ());
113+ }
114+
115+ /**
116+ * Get the CORS configuration.
117+ */
118+ protected function getCorsConfig (): array
119+ {
120+ return $ this ->config ;
121+ }
104122}
0 commit comments