-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExtDirectAction.php
60 lines (52 loc) · 1.5 KB
/
ExtDirectAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace iqria\extdirect;
use yii;
use yii\base\Action;
use yii\helpers\Json;
use yii\web\Response;
/**
* Class ExtDirectAction returns server's API and handles the request
*
* @author Stanislav Hudkov <[email protected]>
* @version 1.0
* @package iqria\extdirect
*/
class ExtDirectAction extends Action
{
protected $requestBody;
/**
* Attach response behavior to controller where the action is.
* - raw text in case of getting API
* - json when the data need to be processed
*/
public function init()
{
$this->requestBody = Json::decode(Yii::$app->request->getRawBody());
if (!$this->controller->getBehavior('responseFormatter')) {
$this->controller->attachBehavior('responseFormatter', [
'class' => 'yii\filters\ContentNegotiator',
'formats' => [
!$this->requestBody ? Response::FORMAT_RAW : Response::FORMAT_JSON,
]
]);
}
parent::init();
}
/**
* @param string $api js|json are allowed parameters
* @see ExtDirectManager API constants
* @return mixed
*/
public function run($api = null)
{
if (!$this->requestBody) {
return Yii::$app->extDirect->getApi($api);
} else {
return $this->processRequest($this->requestBody);
}
}
protected function processRequest($body)
{
return Yii::$app->extDirect->processRequest($body);
}
}