|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Neomerx\CorsIlluminate\Providers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2020 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Closure; |
|
22
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
23
|
|
|
use Illuminate\Support\ServiceProvider; |
|
24
|
|
|
use Neomerx\Cors\Analyzer; |
|
25
|
|
|
use Neomerx\Cors\Contracts\AnalysisStrategyInterface; |
|
26
|
|
|
use Neomerx\Cors\Contracts\AnalyzerInterface; |
|
27
|
|
|
use Neomerx\CorsIlluminate\Settings\Settings; |
|
28
|
|
|
use Psr\Log\LoggerInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @package Neomerx\CorsIlluminate |
|
32
|
|
|
*/ |
|
33
|
|
|
class LaravelServiceProvider extends ServiceProvider |
|
34
|
|
|
{ |
|
35
|
|
|
/** Config file name without extension */ |
|
36
|
|
|
const CONFIG_FILE_NAME_WO_EXT = 'cors-illuminate'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $defer = false; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool|array |
|
45
|
|
|
*/ |
|
46
|
|
|
private $settingsData = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
|
|
public function register() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->mergeConfigs(); |
|
54
|
|
|
$this->configureCorsAnalyzer(); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
/** |
|
58
|
1 |
|
* Perform post-registration booting of services. |
|
59
|
1 |
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function boot() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->registerPublishConfig(); |
|
65
|
|
|
} |
|
66
|
1 |
|
|
|
67
|
|
|
/** |
|
68
|
1 |
|
* Merge default config and config from application `config` folder. |
|
69
|
1 |
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function mergeConfigs(): void |
|
73
|
|
|
{ |
|
74
|
1 |
|
$repo = $this->getConfigRepository(); |
|
75
|
|
|
$config = $repo->get(static::CONFIG_FILE_NAME_WO_EXT, []); |
|
76
|
1 |
|
$base = $this->getBaseConfig(); |
|
77
|
1 |
|
$result = $config + $base; |
|
78
|
1 |
|
$repo->set(static::CONFIG_FILE_NAME_WO_EXT, $result); |
|
79
|
1 |
|
} |
|
80
|
1 |
|
|
|
81
|
1 |
|
/** |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function registerPublishConfig(): void |
|
85
|
|
|
{ |
|
86
|
1 |
|
$publishPath = $this->app['path.config'] . DIRECTORY_SEPARATOR . static::CONFIG_FILE_NAME_WO_EXT . '.php'; |
|
87
|
|
|
$this->publishes([ |
|
88
|
1 |
|
$this->getConfigPath() => $publishPath, |
|
89
|
1 |
|
]); |
|
90
|
1 |
|
} |
|
91
|
1 |
|
|
|
92
|
1 |
|
/** |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function configureCorsAnalyzer(): void |
|
96
|
|
|
{ |
|
97
|
2 |
|
$this->app->bind(AnalysisStrategyInterface::class, $this->getCreateAnalysisStrategyClosure()); |
|
98
|
|
|
$this->app->bind(AnalyzerInterface::class, $this->getCreateAnalyzerClosure()); |
|
99
|
2 |
|
} |
|
100
|
2 |
|
|
|
101
|
2 |
|
/** |
|
102
|
|
|
* @return Closure |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getCreateAnalysisStrategyClosure(): Closure |
|
105
|
|
|
{ |
|
106
|
3 |
|
return function (): AnalysisStrategyInterface { |
|
107
|
|
|
$data = $this->getSettingsData(); |
|
108
|
|
|
$strategy = (new Settings())->setData($data); |
|
109
|
1 |
|
|
|
110
|
1 |
|
return $strategy; |
|
111
|
|
|
}; |
|
112
|
1 |
|
} |
|
113
|
3 |
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return Closure |
|
116
|
|
|
* |
|
117
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getCreateAnalyzerClosure(): Closure |
|
120
|
|
|
{ |
|
121
|
|
|
return function ($app): AnalyzerInterface { |
|
122
|
|
|
/** @var AnalysisStrategyInterface $strategy */ |
|
123
|
3 |
|
$strategy = $app[AnalysisStrategyInterface::class]; |
|
124
|
|
|
$analyzer = Analyzer::instance($strategy); |
|
125
|
1 |
|
|
|
126
|
1 |
|
/** @var Settings $strategy */ |
|
127
|
|
|
|
|
128
|
1 |
|
if ($strategy->isLogEnabled() === true) { |
|
129
|
1 |
|
/** @var LoggerInterface $logger */ |
|
130
|
|
|
$logger = $app[LoggerInterface::class]; |
|
131
|
1 |
|
$analyzer->setLogger($logger); |
|
132
|
3 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $analyzer; |
|
135
|
|
|
}; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
/** |
|
139
|
|
|
* @return string |
|
140
|
2 |
|
*/ |
|
141
|
2 |
|
protected function getConfigPath(): string |
|
142
|
|
|
{ |
|
143
|
2 |
|
$root = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR; |
|
144
|
|
|
$path = $root . 'config' . DIRECTORY_SEPARATOR . static::CONFIG_FILE_NAME_WO_EXT . '.php'; |
|
145
|
|
|
|
|
146
|
|
|
return $path; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return array |
|
151
|
1 |
|
* |
|
152
|
|
|
* PHPMD do not work with ['key' => $var] = ...; |
|
153
|
|
|
* @SuppressWarnings(PHPMD.UndefinedVariable) |
|
154
|
|
|
*/ |
|
155
|
1 |
|
protected function getSettingsData(): array |
|
156
|
1 |
|
{ |
|
157
|
|
|
if ($this->settingsData === false) { |
|
158
|
1 |
|
$configFile = $this->getConfigRepository()->get(static::CONFIG_FILE_NAME_WO_EXT, []); |
|
159
|
1 |
|
|
|
160
|
|
|
// server origin should be in parse_url() result format. |
|
161
|
1 |
|
\assert( |
|
162
|
1 |
|
\array_key_exists(Settings::KEY_SERVER_ORIGIN, $configFile) && |
|
163
|
|
|
\is_array($configFile[Settings::KEY_SERVER_ORIGIN]), |
|
164
|
1 |
|
'Server origin must be array in `parse_url()` format.' |
|
165
|
|
|
); |
|
166
|
|
|
\assert(\array_key_exists('scheme', $configFile[Settings::KEY_SERVER_ORIGIN])); |
|
167
|
|
|
\assert(\array_key_exists('host', $configFile[Settings::KEY_SERVER_ORIGIN])); |
|
168
|
|
|
\assert(\array_key_exists('port', $configFile[Settings::KEY_SERVER_ORIGIN])); |
|
169
|
|
|
[ |
|
170
|
2 |
|
'scheme' => $serverOriginScheme, |
|
|
|
|
|
|
171
|
|
|
'host' => $serverOriginHost, |
|
|
|
|
|
|
172
|
|
|
'port' => $serverOriginPort, |
|
|
|
|
|
|
173
|
|
|
] = $configFile[Settings::KEY_SERVER_ORIGIN]; |
|
174
|
2 |
|
|
|
175
|
2 |
|
$settings = new Settings(); |
|
176
|
2 |
|
$settings->init($serverOriginScheme, $serverOriginHost, $serverOriginPort); |
|
177
|
|
|
|
|
178
|
2 |
|
$origins = $configFile[Settings::KEY_ALLOWED_ORIGINS] ?? null; |
|
179
|
|
|
$origins !== null ? $settings->setAllowedOrigins($origins) : $settings->enableAllOriginsAllowed(); |
|
180
|
|
|
|
|
181
|
|
|
$settings->setAllowedMethods($configFile[Settings::KEY_ALLOWED_METHODS] ?? []); |
|
182
|
|
|
$settings->setAllowedHeaders($configFile[Settings::KEY_ALLOWED_HEADERS] ?? []); |
|
183
|
|
|
$settings->setExposedHeaders($configFile[Settings::KEY_EXPOSED_HEADERS] ?? []); |
|
184
|
3 |
|
$settings->setPreFlightCacheMaxAge($configFile[Settings::KEY_FLIGHT_CACHE_MAX_AGE] ?? 0); |
|
185
|
|
|
|
|
186
|
|
|
\boolval($configFile[Settings::KEY_IS_USING_CREDENTIALS] ?? false) === true ? |
|
187
|
3 |
|
$settings->setCredentialsSupported() : $settings->setCredentialsNotSupported(); |
|
188
|
|
|
|
|
189
|
3 |
|
\boolval($configFile[Settings::KEY_IS_FORCE_ADD_METHODS] ?? false) === true ? |
|
190
|
|
|
$settings->enableAddAllowedMethodsToPreFlightResponse() : |
|
191
|
|
|
$settings->disableAddAllowedMethodsToPreFlightResponse(); |
|
192
|
|
|
|
|
193
|
|
|
\boolval($configFile[Settings::KEY_IS_FORCE_ADD_HEADERS] ?? false) === true ? |
|
194
|
|
|
$settings->enableAddAllowedHeadersToPreFlightResponse() : |
|
195
|
1 |
|
$settings->disableAddAllowedHeadersToPreFlightResponse(); |
|
196
|
|
|
|
|
197
|
1 |
|
\boolval($configFile[Settings::KEY_IS_CHECK_HOST] ?? false) === true ? |
|
198
|
|
|
$settings->enableCheckHost() : $settings->disableCheckHost(); |
|
199
|
1 |
|
|
|
200
|
|
|
|
|
201
|
1 |
|
\boolval($configFile[Settings::KEY_LOGS_ENABLED] ?? false) === true ? |
|
202
|
|
|
$settings->enableLog() : $settings->disableLog(); |
|
203
|
|
|
|
|
204
|
|
|
$this->settingsData = $settings->getData(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return $this->settingsData; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return Repository |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function getConfigRepository() |
|
214
|
|
|
{ |
|
215
|
|
|
/** @var Repository $config */ |
|
216
|
|
|
$config = $this->app['config']; |
|
217
|
|
|
|
|
218
|
|
|
return $config; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function getBaseConfig(): array |
|
225
|
|
|
{ |
|
226
|
|
|
$path = $this->getConfigPath(); |
|
227
|
|
|
/** @noinspection PhpIncludeInspection */ |
|
228
|
|
|
$base = require $path; |
|
229
|
|
|
|
|
230
|
|
|
return $base; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.