BaseController::actions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
crap 6
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 hiqdev\yii\compat\yii;
14
use yii\helpers\Inflector;
15
16
abstract class BaseController extends \yii\web\Controller
17
{
18
    public function behaviors()
19
    {
20
        return [
21
            [
22
                'class' => \hiapi\filters\ContentNegotiator::class,
23
            ],
24
        ];
25
    }
26
27
    protected $_actions;
28
29
    public function actions()
30
    {
31
        if ($this->_actions === null) {
32
            $this->_actions = $this->findActions();
33
        }
34
35
        return $this->_actions;
36
    }
37
38
    public function findActions()
39
    {
40
        $actions = [];
41
        foreach ($this->commands() as $name => $config) {
42
            $actions[$name] = [
43
                'class' => CommandAction::class,
44
                'command' => $config ?: $this->getCommandClass($name),
45
            ];
46
        }
47
48
        return $actions;
49
    }
50
51
    protected $_scannedCommands;
52
53
    public function commands()
54
    {
55
        if ($this->_scannedCommands === null) {
56
            $this->_scannedCommands = $this->scanCommands($this->getCommandNamespace());
57
        }
58
59
        return $this->_scannedCommands;
60
    }
61
62
    public function scanCommands($namespace)
63
    {
64
        $dir = yii::getAlias('@' . strtr($namespace, '\\', '/'));
65
        if (!is_dir($dir)) {
66
            return [];
67
        }
68
69
        $commands = [];
70
        $files = scandir($dir);
71
        foreach ($files as $file) {
72
            if (substr_compare($file, 'Command.php', -11, 11) === 0) {
73
                $command = Inflector::camel2id(substr(basename($file), 0, -11));
74
                $commands[$command] = '';
75
            }
76
        }
77
78
        return $commands;
79
    }
80
81
    public function getCommandClass($name)
82
    {
83
        return $this->getCommandNamespace() . '\\' . Inflector::id2camel($name) . 'Command';
84
    }
85
86
    protected $_commandNamespace;
87
88
    public function getCommandNamespace()
89
    {
90
        if ($this->_commandNamespace === null) {
91
            $this->_commandNamespace = $this->findCommandNamespace();
92
        }
93
94
        return $this->_commandNamespace;
95
    }
96
97
    public function findCommandNamespace()
98
    {
99
        $nss = explode('\\', get_called_class());
100
        array_pop($nss);
101
        array_pop($nss);
102
        array_push($nss, 'commands');
103
        array_push($nss, $this->id);
104
105
        return implode('\\', $nss);
106
    }
107
108
    protected $entityClass;
109
110
    public function getEntityClass()
111
    {
112
        return $this->entityClass;
113
    }
114
}
115