-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActiveAction.php
98 lines (84 loc) · 2.35 KB
/
ActiveAction.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* Active Action
*
*/
namespace vuongminh\ac;
use Yii;
use yii\base\Action;
use yii\base\InvalidConfigException;
/**
* @author Vương Xương Minh <[email protected]>
* @since 1.0
*
* @property ActiveController $controller
*/
class ActiveAction extends Action {
use AppComponent;
/**
*
* @var string suffix of model class
*/
public $modelPrefix = "";
/**
* A model of action
* @var mixed [[\yii\base\Model]], or [[\yii\db\ActiveRecord]];
*/
public $model;
/**
* A response format of action if not set, response format will be inherit
* @var string
*/
public $reponseFormat;
/**
* A layout of action if not set, layout will be use of owner controller
* @var string
*/
public $layout;
/**
*
* @throws InvalidConfigException
*/
public function init() {
$this->initModel();
if (!empty($this->layout)) {
$this->controller->layout = $this->layout;
}
if (!empty($this->reponseFormat)) {
$this->response->format = $this->reponseFormat;
}
}
/**
* void method init model
*/
private function initModel() {
$model = $this->model;
if (!empty($model) && (is_string($model) || is_array($model))) {
$this->model = Yii::createObject($model);
} elseif (empty($model)) {
$modelNamespace = $this->getModelNamespace();
$modelClassName = $this->modelPrefix . str_replace(" ", "", ucwords(implode(" ", explode("-", $this->id))));
$modelClass = $modelNamespace . "\\" . $modelClassName;
if (class_exists($modelClass)) {
$this->model = Yii::createObject($modelClass);
}
}
}
/**
* @return string path store model of action in models directory.
*/
private function getPathModel() {
return str_replace("-", "", $this->controller->id);
}
/**
* Support find model namespace
* @return string
*/
private function getModelNamespace() {
$moduleClass = get_class($this->controller->module);
if (($pos = strrpos($moduleClass, '\\')) !== false) {
return substr($moduleClass, 0, $pos) . '\\models\\' . $this->getPathModel();
}
return "models" . "\\" . $this->getPathModel();
}
}