|
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\console; |
|
12
|
|
|
|
|
13
|
|
|
use hiapi\bus\ApiCommandsBusInterface; |
|
14
|
|
|
use yii\base\Module; |
|
15
|
|
|
use yii\console\Controller; |
|
16
|
|
|
use yii\helpers\Inflector; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ApiController |
|
20
|
|
|
* |
|
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class ApiController extends Controller |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var AutoBusInterface|BranchedAutoBus |
|
28
|
|
|
*/ |
|
29
|
|
|
private $autoBus; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
string $id, |
|
33
|
|
|
Module $module, |
|
34
|
|
|
ApiCommandsBusInterface $autoBus, |
|
35
|
|
|
array $config = [] |
|
36
|
|
|
) { |
|
37
|
|
|
parent::__construct($id, $module, $config); |
|
38
|
|
|
$this->autoBus = $autoBus; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function runAction($id, $params = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$args = [$params[0], $params['_aliases'] ?? []]; |
|
44
|
|
|
|
|
45
|
|
|
return \yii\base\Controller::runAction($id, $args); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function actionCommand($route, $args) |
|
49
|
|
|
{ |
|
50
|
|
|
[$resource, $action] = explode('/', $route, 2); |
|
|
|
|
|
|
51
|
|
|
$result = $this->autoBus->runCommand($this->buildCommandName($resource, $action), $args); |
|
52
|
|
|
|
|
53
|
|
|
echo print_r($result, true); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function buildCommandName($resource, $action, $bulk = false) // todo use $bulk |
|
57
|
|
|
{ |
|
58
|
|
|
return $resource . ucfirst(Inflector::id2camel($action)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..