|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiAPI Yii2 base project for building API |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hiapi |
|
6
|
|
|
* @package hiapi |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiapi\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use hiapi\bus\ApiCommandsBusInterface; |
|
14
|
|
|
use hiapi\components\QueryParamAuth; |
|
15
|
|
|
use hiapi\yii; |
|
16
|
|
|
use hiqdev\yii2\autobus\components\AutoBusInterface; |
|
17
|
|
|
use hiqdev\yii2\autobus\components\BranchedAutoBus; |
|
18
|
|
|
use yii\base\Module; |
|
19
|
|
|
use yii\filters\auth\CompositeAuth; |
|
20
|
|
|
use yii\filters\auth\HttpBearerAuth; |
|
21
|
|
|
use yii\filters\ContentNegotiator; |
|
22
|
|
|
use yii\web\Controller; |
|
23
|
|
|
use yii\web\Response; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class ApiController |
|
27
|
|
|
* |
|
28
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class ApiController extends Controller |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var \yii\web\Response |
|
34
|
|
|
*/ |
|
35
|
|
|
private $response; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var AutoBusInterface|BranchedAutoBus |
|
38
|
|
|
*/ |
|
39
|
|
|
private $autoBus; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct( |
|
42
|
|
|
string $id, |
|
43
|
|
|
Module $module, |
|
44
|
|
|
ApiCommandsBusInterface $autoBus, |
|
45
|
|
|
array $config = [] |
|
46
|
|
|
) { |
|
47
|
|
|
parent::__construct($id, $module, $config); |
|
48
|
|
|
$this->autoBus = $autoBus; |
|
49
|
|
|
$this->response = yii::getApp()->getResponse(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function behaviors() |
|
53
|
|
|
{ |
|
54
|
|
|
return array_merge(parent::behaviors(), [ |
|
55
|
|
|
'contentNegotiator' => [ |
|
56
|
|
|
'class' => ContentNegotiator::class, |
|
57
|
|
|
'formats' => [ |
|
58
|
|
|
'application/json' => Response::FORMAT_JSON, |
|
59
|
|
|
'application/vnd.api+json' => Response::FORMAT_JSON, |
|
60
|
|
|
], |
|
61
|
|
|
], |
|
62
|
|
|
'auth' => [ |
|
63
|
|
|
'class' => CompositeAuth::class, |
|
64
|
|
|
'optional' => ['command'], |
|
65
|
|
|
'authMethods' => [ |
|
66
|
|
|
HttpBearerAuth::class, |
|
67
|
|
|
[ |
|
68
|
|
|
'class' => QueryParamAuth::class, |
|
69
|
|
|
'tokenParam' => 'access_token', |
|
70
|
|
|
], |
|
71
|
|
|
], |
|
72
|
|
|
], |
|
73
|
|
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function actionCommand($version, $resource, $action, $bulk = false) // todo: use $version, $bulk |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$handledCommand = $this->autoBus->runCommand($this->buildCommandName($resource, $action, $bulk), []); |
|
79
|
|
|
|
|
80
|
|
|
$this->response->setHeaders($handledCommand->getHeaders()); |
|
81
|
|
|
$this->response->setStatusCode($handledCommand->getStatusCode()); |
|
82
|
|
|
$this->response->setBody($handledCommand->getBody()); |
|
83
|
|
|
|
|
84
|
|
|
return $this->response->send(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function buildCommandName($resource, $action, $bulk = false) // todo use $bulk |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
return $resource . ucfirst($action); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.