1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\Provider\HttpGalaxy; |
19
|
|
|
|
20
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
21
|
|
|
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition; |
22
|
|
|
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Jordi Boggiano <[email protected]> |
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class CorsConfiguration |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
|
|
public static function getConfigNode(): ArrayNodeDefinition |
34
|
|
|
{ |
35
|
|
|
$rootNode = new ArrayNodeDefinition('cors'); |
36
|
|
|
|
37
|
|
|
$rootNode |
38
|
|
|
->addDefaultsIfNotSet() |
39
|
|
|
->children() |
40
|
|
|
->append(self::getAllowCredentials()) |
41
|
|
|
->append(self::getAllowOrigin()) |
42
|
|
|
->append(self::getAllowHeaders()) |
43
|
|
|
->append(self::getAllowMethods()) |
44
|
|
|
->append(self::getExposeHeaders()) |
45
|
|
|
->append(self::getMaxAge()) |
46
|
|
|
->append(self::getHosts()) |
47
|
|
|
->append(self::getOriginRegex()) |
48
|
|
|
->arrayNode('allow_paths') |
49
|
|
|
->useAttributeAsKey('path') |
50
|
|
|
->normalizeKeys(false) |
51
|
|
|
->prototype('array') |
52
|
|
|
->append(self::getAllowCredentials()) |
|
|
|
|
53
|
|
|
->append(self::getAllowOrigin()) |
54
|
|
|
->append(self::getAllowHeaders()) |
55
|
|
|
->append(self::getAllowMethods()) |
56
|
|
|
->append(self::getExposeHeaders()) |
57
|
|
|
->append(self::getMaxAge()) |
58
|
|
|
->append(self::getHosts()) |
59
|
|
|
->append(self::getOriginRegex()) |
60
|
|
|
->end() |
61
|
|
|
->end() |
62
|
|
|
->end() |
63
|
|
|
; |
64
|
|
|
|
65
|
|
|
return $rootNode; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private static function getAllowCredentials(): BooleanNodeDefinition |
69
|
|
|
{ |
70
|
|
|
$node = new BooleanNodeDefinition('allow_credentials'); |
71
|
|
|
$node->defaultFalse(); |
72
|
|
|
|
73
|
|
|
return $node; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private static function getAllowOrigin(): ArrayNodeDefinition |
77
|
|
|
{ |
78
|
|
|
$node = new ArrayNodeDefinition('allow_origin'); |
79
|
|
|
|
80
|
|
|
$node |
81
|
|
|
->beforeNormalization() |
82
|
|
|
->always(function ($v) { |
83
|
|
|
if ($v === '*') { |
84
|
|
|
return ['*']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $v; |
88
|
|
|
}) |
89
|
|
|
->end() |
90
|
|
|
->prototype('scalar')->end() |
|
|
|
|
91
|
|
|
; |
92
|
|
|
|
93
|
|
|
return $node; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private static function getAllowHeaders(): ArrayNodeDefinition |
97
|
|
|
{ |
98
|
|
|
$node = new ArrayNodeDefinition('allow_headers'); |
99
|
|
|
|
100
|
|
|
$node |
101
|
|
|
->beforeNormalization() |
102
|
|
|
->always(function ($v) { |
103
|
|
|
if ($v === '*') { |
104
|
|
|
return ['*']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $v; |
108
|
|
|
}) |
109
|
|
|
->end() |
110
|
|
|
->prototype('scalar')->end(); |
111
|
|
|
|
112
|
|
|
return $node; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private static function getAllowMethods(): ArrayNodeDefinition |
116
|
|
|
{ |
117
|
|
|
$node = new ArrayNodeDefinition('allow_methods'); |
118
|
|
|
|
119
|
|
|
$node->prototype('scalar')->end(); |
120
|
|
|
|
121
|
|
|
return $node; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private static function getExposeHeaders(): ArrayNodeDefinition |
125
|
|
|
{ |
126
|
|
|
$node = new ArrayNodeDefinition('expose_headers'); |
127
|
|
|
|
128
|
|
|
$node->prototype('scalar')->end(); |
129
|
|
|
|
130
|
|
|
return $node; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private static function getMaxAge(): ScalarNodeDefinition |
134
|
|
|
{ |
135
|
|
|
$node = new ScalarNodeDefinition('max_age'); |
136
|
|
|
|
137
|
|
|
$node |
138
|
|
|
->defaultValue(0) |
139
|
|
|
->validate() |
140
|
|
|
->ifTrue(fn ($v) => !is_numeric($v)) |
141
|
|
|
->thenInvalid('max_age must be an integer (seconds)') |
142
|
|
|
->end() |
143
|
|
|
; |
144
|
|
|
|
145
|
|
|
return $node; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private static function getHosts(): ArrayNodeDefinition |
149
|
|
|
{ |
150
|
|
|
$node = new ArrayNodeDefinition('hosts'); |
151
|
|
|
|
152
|
|
|
$node->prototype('scalar')->end(); |
153
|
|
|
|
154
|
|
|
return $node; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private static function getOriginRegex(): BooleanNodeDefinition |
158
|
|
|
{ |
159
|
|
|
$node = new BooleanNodeDefinition('origin_regex'); |
160
|
|
|
$node->defaultFalse(); |
161
|
|
|
|
162
|
|
|
return $node; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|