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
|
|
|
/** |
15
|
|
|
* @var ApiDecider @inject |
16
|
|
|
*/ |
17
|
|
|
public $apiDecider; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var IpDetectorInterface @inject |
21
|
|
|
*/ |
22
|
|
|
public $ipDetector; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Nette render default method |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function renderDefault() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$start = microtime(true); |
32
|
|
|
|
33
|
|
|
$this->getHttpResponse()->addHeader('Access-Control-Allow-Origin', '*'); |
34
|
|
|
|
35
|
|
|
$logger = null; |
36
|
|
|
if ($this->context->hasService('apiLogger')) { |
|
|
|
|
37
|
|
|
$logger = $this->context->getService('apiLogger'); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// get handler |
41
|
|
|
$hand = $this->apiDecider->getApiHandler( |
42
|
|
|
$this->request->getMethod(), |
|
|
|
|
43
|
|
|
$this->params['version'], |
44
|
|
|
$this->params['package'], |
45
|
|
|
$this->params['apiAction'] |
46
|
|
|
); |
47
|
|
|
$handler = $hand['handler']; |
48
|
|
|
$authorization = $hand['authorization']; |
49
|
|
|
|
50
|
|
|
// check authorization |
51
|
|
|
if (!$authorization->authorized()) { |
52
|
|
|
$this->getHttpResponse()->setCode(Response::S403_FORBIDDEN); |
53
|
|
|
$this->sendResponse(new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()])); |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// process params |
58
|
|
|
$paramsProcessor = new ParamsProcessor($handler->params()); |
59
|
|
|
if ($paramsProcessor->isError()) { |
60
|
|
|
$this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR); |
61
|
|
|
$this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input'])); |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
$params = $paramsProcessor->getValues(); |
65
|
|
|
|
66
|
|
|
// process handler |
67
|
|
|
$response = $handler->handle($params); |
68
|
|
|
$code = $response->getCode(); |
69
|
|
|
|
70
|
|
|
$end = microtime(true); |
71
|
|
|
|
72
|
|
|
if ($logger) { |
73
|
|
|
$headers = []; |
74
|
|
|
if (function_exists('getallheaders')) { |
75
|
|
|
$headers = getallheaders(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$requestHeaders = ''; |
79
|
|
|
foreach ($headers as $key => $value) { |
80
|
|
|
$requestHeaders .= "$key: $value\n"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$logger->log( |
84
|
|
|
$code, |
85
|
|
|
$this->request->getMethod(), |
|
|
|
|
86
|
|
|
$requestHeaders, |
87
|
|
|
$_SERVER['REQUEST_URI'], |
88
|
|
|
$this->ipDetector->getRequestIp(), |
89
|
|
|
isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null, |
90
|
|
|
($end-$start) * 1000 |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// output to nette |
95
|
|
|
$this->getHttpResponse()->setCode($code); |
96
|
|
|
$this->sendResponse($response); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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: