-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Cors.php
52 lines (42 loc) · 1.99 KB
/
Cors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Foundation;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use function count;
use function flextype;
use function implode;
class Cors
{
/**
* Init CORS
*/
public function init(): void
{
if (! flextype('registry')->get('flextype.settings.cors.enabled')) {
return;
}
flextype()->options('/{routes:.+}', function (ServerRequestInterface $request, ResponseInterface $response) {
return $response;
});
flextype()->add(function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
$response = $next($request, $response);
// Set variables
$origin = flextype('registry')->get('flextype.settings.cors.origin');
$headers = count(flextype('registry')->get('flextype.settings.cors.headers')) ? implode(', ', flextype('registry')->get('flextype.settings.cors.headers')) : '';
$methods = count(flextype('registry')->get('flextype.settings.cors.methods')) ? implode(', ', flextype('registry')->get('flextype.settings.cors.methods')) : '';
$expose = count(flextype('registry')->get('flextype.settings.cors.expose')) ? implode(', ', flextype('registry')->get('flextype.settings.cors.expose')) : '';
$credentials = flextype('registry')->get('flextype.settings.cors.credentials') ? true : false;
return $response
->withHeader('Access-Control-Allow-Origin', $origin)
->withHeader('Access-Control-Allow-Headers', $headers)
->withHeader('Access-Control-Allow-Methods', $methods)
->withHeader('Access-Control-Allow-Expose', $expose)
->withHeader('Access-Control-Allow-Credentials', $credentials);
});
}
}