Completed
Pull Request — master (#271)
by Marco Da
02:54
created

Module::getViewPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace mdm\admin;
4
5
use Yii;
6
use yii\helpers\Inflector;
7
use yii\helpers\ArrayHelper;
8
9
/**
10
 * GUI manager for RBAC.
11
 * 
12
 * Use [[\yii\base\Module::$controllerMap]] to change property of controller. 
13
 * To change listed menu, use property [[$menus]].
14
 * 
15
 * ```
16
 * 'layout' => 'left-menu', // default to null mean use application layout.
17
 * 'controllerMap' => [
18
 *     'assignment' => [
19
 *         'class' => 'mdm\admin\controllers\AssignmentController',
20
 *         'userClassName' => 'app\models\User',
21
 *         'idField' => 'id'
22
 *     ]
23
 * ],
24
 * 'menus' => [
25
 *     'assignment' => [
26
 *         'label' => 'Grand Access' // change label
27
 *     ],
28
 *     'route' => null, // disable menu
29
 * ],
30
 * ```
31
 * 
32
 * @property string $mainLayout Main layout using for module. Default to layout of parent module.
33
 * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
34
 * @property array $menus List avalible menu of module.
35
 * It generated by module items .
36
 * 
37
 * @author Misbahul D Munir <[email protected]>
38
 * @since 1.0
39
 */
40
class Module extends \yii\base\Module
41
{
42
    /**
43
     * @var array Nav bar items.
44
     */
45
    public $navbar;
46
    /**
47
     * @var string Main layout using for module. Default to layout of parent module.
48
     * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
49
     */
50
    public $mainLayout = '@mdm/admin/views/layouts/main.php';
51
    /**
52
     * @var string 
53
     * @see [[Aliases]]
54
     */
55
    public $viewPath = '@mdm/admin/views';
56
    /**
57
     * @var array 
58
     * @see [[menus]]
59
     */
60
    private $_menus = [];
61
    /**
62
     * @var array 
63
     * @see [[menus]]
64
     */
65
    private $_coreItems = [
66
        'user' => 'Users',
67
        'assignment' => 'Assignments',
68
        'role' => 'Roles',
69
        'permission' => 'Permissions',
70
        'route' => 'Routes',
71
        'rule' => 'Rules',
72
        'menu' => 'Menus',
73
    ];
74
    /**
75
     * @var array 
76
     * @see [[items]]
77
     */
78
    private $_normalizeMenus;
79
80
    /**
81
     * @var string Default url for breadcrumb
82
     */
83
    public $defaultUrl;
84
85
    /**
86
     * @var string Default url label for breadcrumb
87
     */
88
    public $defaultUrlLabel;
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function init()
94
    {
95
        parent::init();
96
        if (!isset(Yii::$app->i18n->translations['rbac-admin'])) {
97
            Yii::$app->i18n->translations['rbac-admin'] = [
98
                'class' => 'yii\i18n\PhpMessageSource',
99
                'sourceLanguage' => 'en',
100
                'basePath' => '@mdm/admin/messages'
101
            ];
102
        }
103
        $userClass = ArrayHelper::getValue(Yii::$app->components, 'user.identityClass');
104
        if ($this->defaultRoute == 'default' && $userClass && is_subclass_of($userClass, 'yii\db\BaseActiveRecord')) {
105
            $this->defaultRoute = 'assignment';
106
        }
107
        //user did not define the Navbar?
108
        if ($this->navbar === null && Yii::$app instanceof \yii\web\Application) {
109
            $this->navbar = [
110
                ['label' => Yii::t('rbac-admin', 'Help'), 'url' => ['default/index']],
111
                ['label' => Yii::t('rbac-admin', 'Application'), 'url' => Yii::$app->homeUrl]
112
            ];
113
        }
114
        if (class_exists('yii\jui\JuiAsset')) {
115
            Yii::$container->set('mdm\admin\AutocompleteAsset', 'yii\jui\JuiAsset');
116
        }
117
    }
118
119
    /**
120
     * Get Aliases for views
121
     * @return aliases for render
122
     */
123
    public function getViewPath()
124
    {
125
        return $this->viewPath;
126
    }
127
128
    /**
129
     * Get avalible menu.
130
     * @return array
131
     */
132
    public function getMenus()
133
    {
134
        if ($this->_normalizeMenus === null) {
135
            $mid = '/' . $this->getUniqueId() . '/';
136
            // resolve core menus
137
            $this->_normalizeMenus = [];
138
139
            $config = components\Configs::instance();
140
            $conditions = [
141
                'user' => $config->db && $config->db->schema->getTableSchema($config->userTable),
142
                'assignment' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\db\BaseActiveRecord'),
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
143
                'menu' => $config->db && $config->db->schema->getTableSchema($config->menuTable),
144
            ];
145
            foreach ($this->_coreItems as $id => $lable) {
146
                if (!isset($conditions[$id]) || $conditions[$id]) {
147
                    $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]];
148
                }
149
            }
150
            foreach (array_keys($this->controllerMap) as $id) {
151
                $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]];
152
            }
153
154
            // user configure menus
155
            foreach ($this->_menus as $id => $value) {
156
                if (empty($value)) {
157
                    unset($this->_normalizeMenus[$id]);
158
                    continue;
159
                }
160
                if (is_string($value)) {
161
                    $value = ['label' => $value];
162
                }
163
                $this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value)
164
                        : $value;
165
                if (!isset($this->_normalizeMenus[$id]['url'])) {
166
                    $this->_normalizeMenus[$id]['url'] = [$mid . $id];
167
                }
168
            }
169
        }
170
        return $this->_normalizeMenus;
171
    }
172
173
    /**
174
     * Set or add avalible menu.
175
     * @param array $menus
176
     */
177
    public function setMenus($menus)
178
    {
179
        $this->_menus = array_merge($this->_menus, $menus);
180
        $this->_normalizeMenus = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $_normalizeMenus.

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..

Loading history...
181
    }
182
183
    /**
184
     * @inheritdoc
185
     */
186
    public function beforeAction($action)
187
    {
188
        if (parent::beforeAction($action)) {
189
            /* @var $action \yii\base\Action */
190
            $view = $action->controller->getView();
191
192
            $view->params['breadcrumbs'][] = [
193
                'label' => ($this->defaultUrlLabel ?: Yii::t('rbac-admin', 'Admin')),
194
                'url' => ['/' . ($this->defaultUrl ?: $this->uniqueId)]
195
            ];
196
            return true;
197
        }
198
        return false;
199
    }
200
}
201