|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Router; |
|
3
|
|
|
|
|
4
|
|
|
use Closure; |
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use Wandu\Router\Contracts\Dispatchable; |
|
7
|
|
|
use Wandu\Router\Contracts\LoaderInterface; |
|
8
|
|
|
use Wandu\Router\Contracts\ResponsifierInterface; |
|
9
|
|
|
use Wandu\Router\Loader\SimpleLoader; |
|
10
|
|
|
use Wandu\Router\Responsifier\NullResponsifier; |
|
11
|
|
|
|
|
12
|
|
|
class Dispatcher |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \Wandu\Router\Contracts\LoaderInterface */ |
|
15
|
|
|
protected $loader; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Wandu\Router\Responsifier\NullResponsifier */ |
|
18
|
|
|
protected $responsifier; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
protected $options; |
|
22
|
|
|
|
|
23
|
5 |
|
public function __construct( |
|
24
|
|
|
LoaderInterface $loader = null, |
|
25
|
|
|
ResponsifierInterface $responsifier = null, |
|
26
|
|
|
array $options = [] |
|
27
|
|
|
) { |
|
28
|
5 |
|
$this->loader = $loader ?: new SimpleLoader(); |
|
29
|
5 |
|
$this->responsifier = $responsifier ?: new NullResponsifier(); |
|
|
|
|
|
|
30
|
5 |
|
$this->setOptions($options); |
|
31
|
5 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $options |
|
35
|
|
|
*/ |
|
36
|
5 |
|
public function setOptions(array $options) |
|
37
|
|
|
{ |
|
38
|
5 |
|
$this->options = array_merge([ |
|
39
|
5 |
|
'method_override_enabled' => true, |
|
40
|
|
|
'method_spoofing_enabled' => false, |
|
41
|
|
|
'defined_prefix' => '', |
|
42
|
|
|
'defined_middlewares' => [], |
|
43
|
|
|
'defined_domains' => [], |
|
44
|
5 |
|
], $options); |
|
45
|
5 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return \Wandu\Router\RouteCollection |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function createRouteCollection(): RouteCollection |
|
51
|
|
|
{ |
|
52
|
4 |
|
return new RouteCollection( |
|
53
|
4 |
|
$this->options['defined_prefix'], |
|
54
|
4 |
|
$this->options['defined_middlewares'], |
|
55
|
4 |
|
$this->options['defined_domains'] |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param \Wandu\Router\Contracts\Dispatchable $dispatcher |
|
61
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
62
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function dispatch(Dispatchable $dispatcher, ServerRequestInterface $request) |
|
65
|
|
|
{ |
|
66
|
9 |
|
return $dispatcher->dispatch($this->loader, $this->responsifier, $this->applyVirtualMethod($request)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
|
71
|
|
|
* @return \Psr\Http\Message\ServerRequestInterface |
|
72
|
|
|
*/ |
|
73
|
9 |
|
protected function applyVirtualMethod(ServerRequestInterface $request) |
|
74
|
|
|
{ |
|
75
|
9 |
|
if ($this->options['method_override_enabled'] && $request->hasHeader('X-Http-Method-Override')) { |
|
76
|
2 |
|
return $request->withMethod(strtoupper($request->getHeaderLine('X-Http-Method-Override'))); |
|
77
|
|
|
} |
|
78
|
7 |
|
if ($this->options['method_spoofing_enabled']) { |
|
79
|
1 |
|
$parsedBody = $request->getParsedBody(); |
|
80
|
1 |
|
if (isset($parsedBody['_method'])) { |
|
81
|
1 |
|
return $request->withMethod($parsedBody['_method']); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
6 |
|
return $request; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.