44
55class CORS
66{
7- // Initialize private properties to store CORS settings
8- private $ allowedOrigins = array ();
9- private $ allowedMethods = array ();
10- private $ allowedHeaders = array ();
11- private $ exposedHeaders = array ();
12- private $ maxAge = 0 ;
13- private $ allowCredentials = false ;
7+ // Define private static properties to store CORS settings
8+ static private $ allowedOrigins = array ();
9+ static private $ allowedMethods = array ();
10+ static private $ allowedHeaders = array ();
11+ static private $ exposedHeaders = array ();
12+ static private $ maxAge = 0 ;
13+ static private $ allowCredentials = false ;
1414
1515 // Method to set allowed origins
16- public function origin ($ origins )
16+ public static function origin ($ origins )
1717 {
18- $ this ->allowedOrigins = $ origins ;
19- return $ this ;
18+ // Assign passed origins to the static property
19+ self ::$ allowedOrigins = $ origins ;
20+ // Return new instance of the class for method chaining
21+ return new static ;
2022 }
2123
2224 // Method to set allowed methods
23- public function methods ($ methods )
25+ public static function methods ($ methods )
2426 {
25- $ this ->allowedMethods = $ methods ;
26- return $ this ;
27+ // Assign passed methods to the static property
28+ self ::$ allowedMethods = $ methods ;
29+ // Return new instance of the class for method chaining
30+ return new static ;
2731 }
2832
2933 // Method to set allowed headers
30- public function headers ($ headers )
34+ public static function headers ($ headers )
3135 {
32- $ this ->allowedHeaders = $ headers ;
33- return $ this ;
36+ // Assign passed headers to the static property
37+ self ::$ allowedHeaders = $ headers ;
38+ // Return new instance of the class for method chaining
39+ return new static ;
3440 }
3541
3642 // Method to set exposed headers
37- public function expose ($ headers )
43+ public static function expose ($ headers )
3844 {
39- $ this ->exposedHeaders = $ headers ;
40- return $ this ;
45+ // Assign passed headers to the static property
46+ self ::$ exposedHeaders = $ headers ;
47+ // Return new instance of the class for method chaining
48+ return new static ;
4149 }
4250
4351 // Method to set max age
44- public function maxAge ($ age )
52+ public static function maxAge ($ age )
4553 {
46- $ this ->maxAge = $ age ;
47- return $ this ;
54+ // Assign passed age to the static property
55+ self ::$ maxAge = $ age ;
56+ // Return new instance of the class for method chaining
57+ return new static ;
4858 }
4959
5060 // Method to set allow credentials
51- public function credentials ($ credentials )
61+ public static function credentials ($ credentials )
5262 {
53- $ this ->allowCredentials = $ credentials ;
54- return $ this ;
63+ // Assign passed credentials to the static property
64+ self ::$ allowCredentials = $ credentials ;
65+ // Return new instance of the class for method chaining
66+ return new static ;
5567 }
5668
5769 // Method to set CORS headers based on the properties set in the constructor
58- public function setHeaders ()
70+ public static function setHeaders ()
5971 {
6072 // Check if the allowed origins include all origins by checking if '*' is in the array
61- if (in_array ('* ' , $ this -> allowedOrigins )) {
73+ if (in_array ('* ' , self :: $ allowedOrigins )) {
6274 // Allow all origins with a wildcard
6375 header ('Access-Control-Allow-Origin: * ' );
6476 } else {
6577 // Check if the origin of the request is in the allowed origins array
6678 $ origin = isset ($ _SERVER ['HTTP_ORIGIN ' ]) ? $ _SERVER ['HTTP_ORIGIN ' ] : '' ;
67- if (in_array ($ origin , $ this -> allowedOrigins )) {
79+ if (in_array ($ origin , self :: $ allowedOrigins )) {
6880 // Set the allowed origin to the origin of the request
6981 header ("Access-Control-Allow-Origin: $ origin " );
7082 }
7183 }
7284 // Check if credentials are allowed and set the allow credentials header if true
73- if ($ this -> allowCredentials ) {
85+ if (self :: $ allowCredentials ) {
7486 header ('Access-Control-Allow-Credentials: true ' );
7587 }
7688 // Set the exposed headers header if there are any exposed headers
77- if (!empty ($ this -> exposedHeaders )) {
78- header ('Access-Control-Expose-Headers: ' . implode (', ' , $ this -> exposedHeaders ));
89+ if (!empty (self :: $ exposedHeaders )) {
90+ header ('Access-Control-Expose-Headers: ' . implode (', ' , self :: $ exposedHeaders ));
7991 }
8092 // Set the max age header if the max age is greater than 0
81- if ($ this -> maxAge > 0 ) {
82- header ("Access-Control-Max-Age: $ this -> maxAge " );
93+ if (self :: $ maxAge > 0 ) {
94+ header ("Access-Control-Max-Age: " . self :: $ maxAge );
8395 }
8496 // Set the allowed methods header if there are any allowed methods
85- if (!empty ($ this -> allowedMethods )) {
86- header ('Access-Control-Allow-Methods: ' . implode (', ' , $ this -> allowedMethods ));
97+ if (!empty (self :: $ allowedMethods )) {
98+ header ('Access-Control-Allow-Methods: ' . implode (', ' , self :: $ allowedMethods ));
8799 }
88100 // Set the allowed headers header if there are any allowed headers
89- if (!empty ($ this -> allowedHeaders )) {
90- header ('Access-Control-Allow-Headers: ' . implode (', ' , $ this -> allowedHeaders ));
101+ if (!empty (self :: $ allowedHeaders )) {
102+ header ('Access-Control-Allow-Headers: ' . implode (', ' , self :: $ allowedHeaders ));
91103 }
92104 }
93105}
0 commit comments