1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cors |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Cors; |
13
|
|
|
|
14
|
|
|
use Dotenv\Dotenv; |
15
|
|
|
use Slick\Cors\Infrastructure\Converter; |
16
|
|
|
use Slick\Cors\Infrastructure\CorsMiddleware; |
|
|
|
|
17
|
|
|
use Slick\Di\Definition\ObjectDefinition; |
18
|
|
|
use Slick\ModuleApi\Infrastructure\AbstractModule; |
19
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandler; |
20
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandlerInterface; |
21
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewarePosition; |
|
|
|
|
22
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\Position; |
23
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\WebModuleInterface; |
24
|
|
|
use function Slick\ModuleApi\importSettingsFile; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* CorsModule |
28
|
|
|
* |
29
|
|
|
* @package Slick\Cors |
30
|
|
|
*/ |
31
|
|
|
final class CorsModule extends AbstractModule implements WebModuleInterface |
32
|
|
|
{ |
33
|
|
|
/** @var array<array<string, mixed>> */ |
34
|
|
|
public static array $defaultConfig = [ |
35
|
|
|
'cors' => [ |
36
|
|
|
'methods' => 'GET, POST, PATCH, PUT, HEAD, DELETE, OPTIONS', |
37
|
|
|
'headers' => 'origin, x-requested-with, content-type, authorization', |
38
|
|
|
'credentials' => 'true' |
39
|
|
|
] |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
public function description(): string |
43
|
|
|
{ |
44
|
|
|
return "Enables Cross-Origin Resource Sharing (CORS) for secure and flexible API interactions."; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieves the settings from a configuration file. |
49
|
|
|
* |
50
|
|
|
* @param Dotenv $dotenv The Dotenv instance used for loading environment variables. |
51
|
|
|
* @return array<string|mixed> The array containing the settings. |
52
|
|
|
* @SuppressWarnings("PHPMD.UnusedFormalParameter") |
53
|
|
|
*/ |
54
|
|
|
public function settings(Dotenv $dotenv): array |
55
|
|
|
{ |
56
|
|
|
$file = APP_ROOT . '/config/modules/cors.php'; |
57
|
|
|
return importSettingsFile($file, self::$defaultConfig); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function services(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
Converter::class => ObjectDefinition::create(Converter\JsonApiConverter::class) |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieves an array of middleware handlers. |
70
|
|
|
* |
71
|
|
|
* @return array<MiddlewareHandlerInterface> The array of middleware handlers. |
72
|
|
|
*/ |
73
|
|
|
public function middlewareHandlers(): array |
74
|
|
|
{ |
75
|
|
|
$position = new MiddlewarePosition(Position::Top); |
76
|
|
|
|
77
|
|
|
return [ |
78
|
|
|
new MiddlewareHandler( |
79
|
|
|
'cors', |
80
|
|
|
$position, |
81
|
|
|
CorsMiddleware::class |
82
|
|
|
) |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths