1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Service\NeomerxCors; |
3
|
|
|
|
4
|
|
|
use Neomerx\Cors\Analyzer; |
5
|
|
|
use Neomerx\Cors\Contracts\AnalysisStrategyInterface; |
6
|
|
|
use Neomerx\Cors\Contracts\AnalyzerInterface; |
7
|
|
|
use Neomerx\Cors\Contracts\Strategies\SettingsStrategyInterface; |
8
|
|
|
use Neomerx\Cors\Strategies\Settings; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Wandu\Config\Contracts\Config; |
11
|
|
|
use Wandu\DI\ContainerInterface; |
12
|
|
|
use Wandu\DI\ServiceProviderInterface; |
13
|
|
|
|
14
|
|
|
class CorsServiceProvider implements ServiceProviderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
public function register(ContainerInterface $app) |
20
|
|
|
{ |
21
|
|
|
$app->bind(Settings::class)->after(function (Settings $settings, Config $config) { |
22
|
|
|
$settings->setServerOrigin($config->get('cors.server_origin')); |
23
|
|
|
$settings->setRequestAllowedOrigins($config->get('cors.allowed_origins', [])); |
24
|
|
|
$settings->setRequestAllowedMethods($config->get('cors.allowed_methods', [])); |
25
|
|
|
$settings->setRequestAllowedHeaders($config->get('cors.allowed_headers', [])); |
26
|
|
|
}); |
27
|
|
|
$app->alias(AnalysisStrategyInterface::class, Settings::class); |
28
|
|
|
$app->alias(SettingsStrategyInterface::class, Settings::class); |
29
|
|
|
|
30
|
|
|
$app->bind(Analyzer::class, function (AnalysisStrategyInterface $strategy) { |
31
|
|
|
return Analyzer::instance($strategy); |
32
|
|
|
})->after(function (Analyzer $analyzer) use ($app) { |
33
|
|
|
if ($app->has(LoggerInterface::class)) { |
34
|
|
|
$analyzer->setLogger($app->get(LoggerInterface::class)); |
35
|
|
|
} |
36
|
|
|
}); |
37
|
|
|
$app->alias(AnalyzerInterface::class, Analyzer::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function boot(ContainerInterface $app) |
44
|
|
|
{ |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|