1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Controller\Hooks; |
4
|
|
|
use PhpBoot\Controller\ExceptionRenderer; |
5
|
|
|
use PhpBoot\DI\Traits\EnableDIAnnotations; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Cors |
11
|
|
|
* copy from https://github.com/palanik/lumen-cors/blob/master/LumenCors.php |
12
|
|
|
*/ |
13
|
|
|
class Cors |
14
|
|
|
{ |
15
|
|
|
use EnableDIAnnotations; |
16
|
|
|
|
17
|
|
|
protected $settings = array( |
18
|
|
|
'origin' => '*', // Wide Open! |
19
|
|
|
'allowMethods' => 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS', |
20
|
|
|
); |
21
|
|
View Code Duplication |
protected function setOrigin(Request $req, Response $rsp) { |
|
|
|
|
22
|
|
|
$origin = $this->settings['origin']; |
23
|
|
|
if (is_callable($origin)) { |
24
|
|
|
// Call origin callback with request origin |
25
|
|
|
$origin = call_user_func($origin, |
26
|
|
|
$req->headers->get("Origin") |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
$rsp->headers->set('Access-Control-Allow-Origin', $origin); |
30
|
|
|
} |
31
|
|
|
protected function setExposeHeaders(Request $req, Response $rsp) { |
|
|
|
|
32
|
|
|
if (isset($this->settings->exposeHeaders)) { |
33
|
|
|
$exposeHeaders = $this->settings->exposeHeaders; |
34
|
|
|
if (is_array($exposeHeaders)) { |
35
|
|
|
$exposeHeaders = implode(", ", $exposeHeaders); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$rsp->headers->set('Access-Control-Expose-Headers', $exposeHeaders); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
protected function setMaxAge(Request $req, Response $rsp) { |
|
|
|
|
42
|
|
|
if (isset($this->settings['maxAge'])) { |
43
|
|
|
$rsp->headers->set('Access-Control-Max-Age', $this->settings['maxAge']); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
protected function setAllowCredentials(Request $req, Response $rsp) { |
|
|
|
|
47
|
|
|
if (isset($this->settings['allowCredentials']) && $this->settings['allowCredentials'] === True) { |
48
|
|
|
$rsp->headers->set('Access-Control-Allow-Credentials', 'true'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
View Code Duplication |
protected function setAllowMethods(Request $req, Response $rsp) { |
|
|
|
|
52
|
|
|
if (isset($this->settings['allowMethods'])) { |
53
|
|
|
$allowMethods = $this->settings['allowMethods']; |
54
|
|
|
if (is_array($allowMethods)) { |
55
|
|
|
$allowMethods = implode(", ", $allowMethods); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$rsp->headers->set('Access-Control-Allow-Methods', $allowMethods); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
protected function setAllowHeaders(Request $req, Response $rsp) { |
62
|
|
|
if (isset($this->settings['allowHeaders'])) { |
63
|
|
|
$allowHeaders = $this->settings['allowHeaders']; |
64
|
|
|
if (is_array($allowHeaders)) { |
65
|
|
|
$allowHeaders = implode(", ", $allowHeaders); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
else { // Otherwise, use request headers |
69
|
|
|
$allowHeaders = $req->headers->get("Access-Control-Request-Headers", null ,false); |
70
|
|
|
} |
71
|
|
|
if (isset($allowHeaders)) { |
72
|
|
|
$rsp->headers->set('Access-Control-Allow-Headers', $allowHeaders); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
protected function setCorsHeaders(Request $req, Response $rsp) { |
76
|
|
|
// http://www.html5rocks.com/static/images/cors_server_flowchart.png |
77
|
|
|
// Pre-flight |
78
|
|
|
if ($req->isMethod('OPTIONS')) { |
79
|
|
|
$this->setOrigin($req, $rsp); |
80
|
|
|
$this->setMaxAge($req, $rsp); |
81
|
|
|
$this->setAllowCredentials($req, $rsp); |
82
|
|
|
$this->setAllowMethods($req, $rsp); |
83
|
|
|
$this->setAllowHeaders($req, $rsp); |
84
|
|
|
} |
85
|
|
|
else { |
86
|
|
|
$this->setOrigin($req, $rsp); |
87
|
|
|
$this->setExposeHeaders($req, $rsp); |
88
|
|
|
$this->setAllowCredentials($req, $rsp); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* Handle an incoming request. |
93
|
|
|
* |
94
|
|
|
* @param \Closure $next |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function handle(Request $request, \Closure $next) { |
98
|
|
|
if ($request->getMethod() == 'OPTIONS') { |
99
|
|
|
$response = new Response("", 200); |
100
|
|
|
} |
101
|
|
|
else { |
102
|
|
|
try{ |
103
|
|
|
$response = $next($request); |
104
|
|
|
}catch(\Exception $e){ |
105
|
|
|
$response = $this->exceptionRenderer->render($e); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
$this->setCorsHeaders($request, $response); |
109
|
|
|
return $response; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inject |
114
|
|
|
* @var ExceptionRenderer |
115
|
|
|
*/ |
116
|
|
|
private $exceptionRenderer; |
117
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.