1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Module.php |
4
|
|
|
* @author Revin Roman |
5
|
|
|
* @link https://rmrevin.ru |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace rmrevin\yii\module\Comments; |
9
|
|
|
|
10
|
|
|
use rmrevin\yii\module\Comments; |
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Module |
15
|
|
|
* @package rmrevin\yii\module\Comments |
16
|
|
|
*/ |
17
|
|
|
class Module extends \yii\base\Module |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** @var string module name */ |
21
|
|
|
public static $moduleName = 'comments'; |
22
|
|
|
|
23
|
|
|
/** @var string|null */ |
24
|
|
|
public $userIdentityClass = null; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
public $useRbac = true; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Array that will store the models used in the package |
31
|
|
|
* e.g. : |
32
|
|
|
* [ |
33
|
|
|
* 'Comment' => 'frontend/models/comments/CommentModel' |
34
|
|
|
* ] |
35
|
|
|
* |
36
|
|
|
* The classes defined here will be merged with getDefaultModels() |
37
|
|
|
* having he manually defined by the user preference. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
public $modelMap = []; |
42
|
|
|
|
43
|
1 |
|
public function init() |
44
|
|
|
{ |
45
|
1 |
|
parent::init(); |
46
|
|
|
|
47
|
1 |
|
if ($this->userIdentityClass === null) { |
48
|
|
|
$this->userIdentityClass = \Yii::$app->getUser()->identityClass; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Merge the default model classes |
52
|
|
|
// with the user defined ones. |
53
|
1 |
|
$this->defineModelClasses(); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
|
|
public static function instance() |
60
|
|
|
{ |
61
|
|
|
return \Yii::$app->getModule(static::$moduleName); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Merges the default and user defined model classes |
66
|
|
|
* Also let's the developer to set new ones with the |
67
|
|
|
* parameter being those the ones with most preference. |
68
|
|
|
* |
69
|
|
|
* @param array $modelClasses |
70
|
|
|
*/ |
71
|
1 |
|
public function defineModelClasses($modelClasses = []) |
72
|
|
|
{ |
73
|
1 |
|
$this->modelMap = ArrayHelper::merge( |
74
|
1 |
|
$this->getDefaultModels(), |
75
|
1 |
|
$this->modelMap, |
76
|
|
|
$modelClasses |
77
|
1 |
|
); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get default model classes |
82
|
|
|
*/ |
83
|
1 |
|
protected function getDefaultModels() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
1 |
|
'Comment' => Comments\models\Comment::className(), |
87
|
1 |
|
'CommentQuery' => Comments\models\queries\CommentQuery::className(), |
88
|
1 |
|
'CommentCreateForm' => Comments\forms\CommentCreateForm::className(), |
89
|
1 |
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get defined className of model |
94
|
|
|
* |
95
|
|
|
* Returns an string or array compatible |
96
|
|
|
* with the Yii::createObject method. |
97
|
|
|
* |
98
|
|
|
* @param string $name |
99
|
|
|
* @param array $config // You should never send an array with a key defined as "class" since this will |
100
|
|
|
* // overwrite the main className defined by the system. |
101
|
|
|
* @return string|array |
102
|
|
|
*/ |
103
|
|
|
public function model($name, $config = []) |
104
|
|
|
{ |
105
|
|
|
$modelData = $this->modelMap[ucfirst($name)]; |
106
|
|
|
|
107
|
|
|
if (!empty($config)) { |
108
|
|
|
if (is_string($modelData)) { |
109
|
|
|
$modelData = ['class' => $modelData]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$modelData = ArrayHelper::merge( |
113
|
|
|
$modelData, |
114
|
|
|
$config |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $modelData; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: