1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Middlewares; |
6
|
|
|
|
7
|
|
|
use Nymfonya\Component\Http\Request; |
8
|
|
|
use Nymfonya\Component\Http\Response; |
9
|
|
|
use Nymfonya\Component\Http\Interfaces\MiddlewareInterface; |
10
|
|
|
use Nymfonya\Component\Container; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* App\Middleware\Cors |
14
|
|
|
* |
15
|
|
|
* Patch initial response to accept CORS requests |
16
|
|
|
*/ |
17
|
|
|
class Cors implements MiddlewareInterface |
18
|
|
|
{ |
19
|
|
|
use \App\Middlewares\Reuse\TInit; |
20
|
|
|
|
21
|
|
|
const _PREFLIGHT = 'preflight'; |
22
|
|
|
const _SIGN = 'X-Middleware-Cors'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* peel |
26
|
|
|
* |
27
|
|
|
* @param Container $container |
28
|
|
|
* @param \Closure $next |
29
|
|
|
* @return \Closure |
30
|
|
|
*/ |
31
|
8 |
|
public function peel(Container $container, \Closure $next) |
32
|
|
|
{ |
33
|
8 |
|
$this->init($container); |
34
|
8 |
|
$this->process(); |
35
|
8 |
|
return $next($container); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* process |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
2 |
|
private function process() |
43
|
|
|
{ |
44
|
2 |
|
if ($this->enabled) { |
45
|
2 |
|
$this->response->getHeaderManager()->add( |
46
|
2 |
|
self::_SIGN, |
47
|
2 |
|
strval(microtime(true)) |
48
|
|
|
); |
49
|
2 |
|
if ($this->required()) { |
50
|
2 |
|
if (Request::METHOD_OPTIONS == $this->request->getMethod()) { |
51
|
1 |
|
$pureCa = preg_replace('/\?.*/', '', $this->caUri()) . '/'; |
52
|
1 |
|
$caFrags = explode('/', $pureCa); |
53
|
1 |
|
$controller = $caFrags[0]; |
54
|
1 |
|
$this->kernel->setAction([$controller, self::_PREFLIGHT]); |
55
|
|
|
} |
56
|
2 |
|
$this->response |
57
|
2 |
|
->setCode(Response::HTTP_NOT_FOUND) |
58
|
2 |
|
->setContent([ |
59
|
2 |
|
Response::_ERROR_CODE => Response::HTTP_NOT_FOUND, |
60
|
2 |
|
Response::_ERROR_MSG => 'Not found' |
61
|
|
|
]) |
62
|
2 |
|
->getHeaderManager() |
63
|
2 |
|
->addMany($this->configParams['headers']); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* required |
70
|
|
|
* |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
1 |
|
protected function required(): bool |
74
|
|
|
{ |
75
|
1 |
|
return (!$this->isExclude() |
76
|
1 |
|
&& $this->requestUriPrefix() === $this->prefix); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* isExclude |
81
|
|
|
* |
82
|
|
|
* @return Boolean |
83
|
|
|
*/ |
84
|
1 |
|
protected function isExclude(): bool |
85
|
|
|
{ |
86
|
1 |
|
$disallowed = $this->configParams['exclude']; |
87
|
1 |
|
$countEx = count($disallowed); |
88
|
1 |
|
for ($c = 0; $c < $countEx; ++$c) { |
89
|
1 |
|
$excludeUri = $this->prefix . $disallowed[$c]; |
90
|
1 |
|
$isExclude = ($excludeUri == $this->request->getUri()); |
91
|
1 |
|
if ($isExclude) { |
92
|
1 |
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
1 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* return controller action from uri |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
1 |
|
protected function caUri(): string |
104
|
|
|
{ |
105
|
1 |
|
return str_replace($this->prefix, '', $this->request->getUri()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* uriPrefix |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
protected function requestUriPrefix(): string |
114
|
|
|
{ |
115
|
1 |
|
return substr($this->request->getUri(), 0, strlen($this->prefix)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|