4
4
5
5
class CORS
6
6
{
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 ;
14
14
15
15
// Method to set allowed origins
16
- public function origin ($ origins )
16
+ public static function origin ($ origins )
17
17
{
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 ;
20
22
}
21
23
22
24
// Method to set allowed methods
23
- public function methods ($ methods )
25
+ public static function methods ($ methods )
24
26
{
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 ;
27
31
}
28
32
29
33
// Method to set allowed headers
30
- public function headers ($ headers )
34
+ public static function headers ($ headers )
31
35
{
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 ;
34
40
}
35
41
36
42
// Method to set exposed headers
37
- public function expose ($ headers )
43
+ public static function expose ($ headers )
38
44
{
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 ;
41
49
}
42
50
43
51
// Method to set max age
44
- public function maxAge ($ age )
52
+ public static function maxAge ($ age )
45
53
{
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 ;
48
58
}
49
59
50
60
// Method to set allow credentials
51
- public function credentials ($ credentials )
61
+ public static function credentials ($ credentials )
52
62
{
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 ;
55
67
}
56
68
57
69
// Method to set CORS headers based on the properties set in the constructor
58
- public function setHeaders ()
70
+ public static function setHeaders ()
59
71
{
60
72
// 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 )) {
62
74
// Allow all origins with a wildcard
63
75
header ('Access-Control-Allow-Origin: * ' );
64
76
} else {
65
77
// Check if the origin of the request is in the allowed origins array
66
78
$ origin = isset ($ _SERVER ['HTTP_ORIGIN ' ]) ? $ _SERVER ['HTTP_ORIGIN ' ] : '' ;
67
- if (in_array ($ origin , $ this -> allowedOrigins )) {
79
+ if (in_array ($ origin , self :: $ allowedOrigins )) {
68
80
// Set the allowed origin to the origin of the request
69
81
header ("Access-Control-Allow-Origin: $ origin " );
70
82
}
71
83
}
72
84
// Check if credentials are allowed and set the allow credentials header if true
73
- if ($ this -> allowCredentials ) {
85
+ if (self :: $ allowCredentials ) {
74
86
header ('Access-Control-Allow-Credentials: true ' );
75
87
}
76
88
// 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 ));
79
91
}
80
92
// 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 );
83
95
}
84
96
// 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 ));
87
99
}
88
100
// 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 ));
91
103
}
92
104
}
93
105
}
0 commit comments