1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Presenters; |
4
|
|
|
|
5
|
|
|
use Nette\Application\Responses\JsonResponse; |
6
|
|
|
use Nette\Application\UI\Presenter; |
7
|
|
|
use Nette\Http\Response; |
8
|
|
|
use Tomaj\NetteApi\ApiDecider; |
9
|
|
|
use Tomaj\NetteApi\Misc\IpDetectorInterface; |
10
|
|
|
use Tomaj\NetteApi\Params\ParamsProcessor; |
11
|
|
|
|
12
|
|
|
class ApiPresenter extends Presenter |
13
|
|
|
{ |
14
|
|
|
/** @var ApiDecider @inject */ |
15
|
|
|
public $apiDecider; |
16
|
|
|
|
17
|
|
|
/** @var IpDetectorInterface @inject */ |
18
|
|
|
public $ipDetector; |
19
|
|
|
|
20
|
|
|
public function renderList($version = null) |
21
|
|
|
{ |
22
|
|
|
$list = $this->apiDecider->getHandlersList($version); |
23
|
|
|
$this->sendJson($list); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function renderDefault() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$start = microtime(true); |
29
|
|
|
|
30
|
|
|
$this->getHttpResponse()->addHeader('Access-Control-Allow-Origin', '*'); |
31
|
|
|
|
32
|
|
|
$logger = null; |
33
|
|
|
if ($this->context->hasService('apiLogger')) { |
|
|
|
|
34
|
|
|
$logger = $this->context->getService('apiLogger'); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// get handler |
38
|
|
|
$hand = $this->apiDecider->getApiHandler( |
39
|
|
|
$this->request->getMethod(), |
|
|
|
|
40
|
|
|
$this->params['version'], |
41
|
|
|
$this->params['package'], |
42
|
|
|
$this->params['apiAction'] |
43
|
|
|
); |
44
|
|
|
$handler = $hand['handler']; |
45
|
|
|
$authorization = $hand['authorization']; |
46
|
|
|
|
47
|
|
|
// check authorization |
48
|
|
|
if (!$authorization->authorized()) { |
49
|
|
|
$this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR); |
50
|
|
|
$this->sendResponse(new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()])); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// process params |
55
|
|
|
$paramsProcessor = new ParamsProcessor($handler->params()); |
56
|
|
|
if ($paramsProcessor->isError()) { |
57
|
|
|
$this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR); |
58
|
|
|
$this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input'])); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
$params = $paramsProcessor->getValues(); |
62
|
|
|
|
63
|
|
|
// process handler |
64
|
|
|
$response = $handler->handle($params); |
65
|
|
|
$code = $response->getCode(); |
66
|
|
|
|
67
|
|
|
$end = microtime(true); |
68
|
|
|
|
69
|
|
|
if ($logger) { |
70
|
|
|
$headers = []; |
71
|
|
|
if (function_exists('getallheaders')) { |
72
|
|
|
$headers = getallheaders(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$requestHeaders = ''; |
76
|
|
|
foreach ($headers as $key => $value) { |
77
|
|
|
$requestHeaders .= "$key: $value\n"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$logger->log( |
81
|
|
|
$code, |
82
|
|
|
$this->request->getMethod(), |
|
|
|
|
83
|
|
|
$requestHeaders, |
84
|
|
|
$_SERVER['REQUEST_URI'], |
85
|
|
|
$this->ipDetector->getRequestIp(), |
86
|
|
|
isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null, |
87
|
|
|
($end-$start) * 1000 |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// output to nette |
92
|
|
|
$this->getHttpResponse()->setCode($code); |
93
|
|
|
$this->sendResponse($response); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: