1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi; |
4
|
|
|
|
5
|
|
|
use Nette\Http\Response; |
6
|
|
|
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface; |
7
|
|
|
use Tomaj\NetteApi\Authorization\NoAuthorization; |
8
|
|
|
use Tomaj\NetteApi\Handlers\ApiHandlerInterface; |
9
|
|
|
use Tomaj\NetteApi\Handlers\CorsPreflightHandler; |
10
|
|
|
use Tomaj\NetteApi\Handlers\DefaultHandler; |
11
|
|
|
|
12
|
|
|
class ApiDecider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ApiHandlerInterface[] |
16
|
|
|
*/ |
17
|
|
|
private $handlers = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $globalPreflight = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get api handler that match input method, version, package and apiAction. |
26
|
|
|
* If decider cannot find handler for given handler, returns defaults. |
27
|
|
|
* |
28
|
|
|
* @param string $method |
29
|
|
|
* @param integer $version |
30
|
|
|
* @param string $package |
31
|
|
|
* @param string $apiAction |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
24 |
|
public function getApiHandler($method, $version, $package, $apiAction = '') |
36
|
|
|
{ |
37
|
24 |
|
foreach ($this->handlers as $handler) { |
38
|
21 |
|
$identifier = $handler['endpoint']; |
39
|
21 |
|
if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) { |
40
|
18 |
|
$endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction); |
41
|
18 |
|
$handler['handler']->setEndpointIdentifier($endpointIdentifier); |
42
|
18 |
|
return $handler; |
43
|
|
|
} |
44
|
9 |
|
if ($method == 'OPTIONS' && $this->globalPreflight && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) { |
45
|
|
|
return [ |
46
|
3 |
|
'endpoint' => new EndpointIdentifier('OPTION', $version, $package, $apiAction), |
47
|
3 |
|
'authorization' => new NoAuthorization(), |
48
|
7 |
|
'handler' => new CorsPreflightHandler(new Response()), |
49
|
1 |
|
]; |
50
|
|
|
} |
51
|
3 |
|
} |
52
|
|
|
return [ |
53
|
3 |
|
'endpoint' => new EndpointIdentifier($method, $version, $package, $apiAction), |
54
|
3 |
|
'authorization' => new NoAuthorization(), |
55
|
3 |
|
'handler' => new DefaultHandler($version, $package, $apiAction) |
|
|
|
|
56
|
1 |
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
public function enableGlobalPreflight() |
60
|
|
|
{ |
61
|
3 |
|
$this->globalPreflight = true; |
62
|
3 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Register new api handler |
66
|
|
|
* |
67
|
|
|
* @param EndpointInterface $endpointIdentifier |
68
|
|
|
* @param ApiHandlerInterface $handler |
69
|
|
|
* @param ApiAuthorizationInterface $apiAuthorization |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
24 |
|
public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization) |
74
|
|
|
{ |
75
|
24 |
|
$this->handlers[] = [ |
76
|
24 |
|
'endpoint' => $endpointIdentifier, |
77
|
24 |
|
'handler' => $handler, |
78
|
24 |
|
'authorization' => $apiAuthorization, |
79
|
|
|
]; |
80
|
24 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get all registered handlers |
85
|
|
|
* |
86
|
|
|
* @return Handlers\ApiHandlerInterface[] |
87
|
|
|
*/ |
88
|
12 |
|
public function getHandlers() |
89
|
|
|
{ |
90
|
12 |
|
return $this->handlers; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.