Completed
Push — master ( 5d8f1a...6c6657 )
by zacksleo
01:34
created

RestController::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace zacksleo\yii2\oauth2\api\components;
4
5
use yii\rest\Controller;
6
use yii\helpers\ArrayHelper;
7
use zacksleo\yii2\oauth2\api\filters\ErrorToExceptionFilter;
8
use yii\filters\ContentNegotiator;
9
use yii\web\Response;
10
11
/**
12
 * Class AuthController
13
 */
14
class RestController extends Controller
15
{
16
    /**
17
     * @inheritdoc
18
     */
19
    public function behaviors()
20
    {
21
        $behaviors = ArrayHelper::merge(parent::behaviors(), [
22
            'exceptionFilter' => [
23
                'class' => ErrorToExceptionFilter::className()
24
            ],
25
            [
26
                'class' => ContentNegotiator::className(),
27
                'formats' => [
28
                    'application/json' => Response::FORMAT_JSON,
29
                ],
30
                'languages' => [
31
                    'zh-CN',
32
                    'zh-TW',
33
                    'en-US',
34
                ],
35
            ],
36
        ]);
37
        $auth = $behaviors['authenticator'];
38
        unset($behaviors['authenticator']);
39
        $behaviors['corsFilter'] = [
40
            'class' => \yii\filters\Cors::className(),
41
        ];
42
        $behaviors['authenticator'] = $auth;
43
        $behaviors['authenticator']['except'] = ['options'];
44
        return $behaviors;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function actions()
51
    {
52
        return [
53
            'options' => [
54
                'class' => 'yii\rest\OptionsAction',
55
            ],
56
        ];
57
    }
58
}
59