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