'/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'], ['modelName', 'validateModelClass'], ['generateMigration', 'boolean'], [['migrationName', 'controllerId'], 'string'], ['attributes', 'safe'] // ['attributes', 'string', 'length' => [0, 1024]], ]; } public function validateModelClass($attribute, $params) { if (!class_exists($this->modelClass)) { $this->addError($attribute, 'Модель не найдена.'); } } public function getModelClass() { return $this->modelNs . '\\' . $this->modelName; } public function getControllerNs() { return $this->moduleId . '\controllers'; } public function generate() { $controllerName = Inflector::id2camel($this->controllerId).'Controller'; $params = [ 'modelName' => $this->modelName, 'modelClass' => $this->modelClass, 'controllerNs' => $this->controllerNs, 'controllerId' => $this->controllerId, 'controllerName' => Inflector::id2camel($this->controllerId), 'attributes' => $this->attributes, 'migrationName' => $this->migrationName, 'moduleId' => $this->moduleId ]; $controllerCode = $this->render('controller.php', $params); $viewCode = $this->render('view.php', $params); $searchCode = $this->render('_search.php', $params); $nsPath = str_replace('\\', '/', $this->controllerNs); $controllerPath = \Yii::getAlias("@$nsPath/{$controllerName}.php"); $viewDir = \Yii::getAlias("@backend/views/{$this->controllerId}"); $files = [ new CodeFile($controllerPath, $controllerCode), new CodeFile("$viewDir/index.php", $viewCode), new CodeFile("$viewDir/_search.php", $searchCode), ]; if($this->generateMigration && $this->migrationName) { $nsPath = str_replace('\\', '/', $this->migrationNs); $migrationPath = \Yii::getAlias("@$nsPath/{$this->migrationName}.php"); $migrationCode = $this->render('migration.php', $params); $files[] = new CodeFile($migrationPath, $migrationCode); } return $files; } /** * Возвращает список атрибутов модели для выбора в форме */ public function getModelAttributes($plain = true) { if (!class_exists($this->modelClass)) { return []; } $model = new $this->modelClass(); if(!$plain) { $result = []; foreach ($model->attributes() as $attribute) { $result[$attribute] = $attribute; } return $result; } return $model->attributes(); // return $model->attributeLabels(); } /** * @inheritdoc */ public function attributeLabels() { return array_merge(parent::attributeLabels(), [ 'modelNs' => 'Model namespace', 'moduleId' => 'Module', ]); } /** * @inheritdoc */ public function hints() { return array_merge(parent::hints(), [ 'modelNs' => 'This is the namespace for the source model, e.g., app\models', 'moduleId' => 'This is the module name for the controller and view to be generated, e.g., app\controllers', ]); } /** * @inheritdoc */ public function stickyAttributes() { return array_merge( parent::stickyAttributes(), [ 'modelNs', 'moduleId', 'migrationNs' ] ); } public function actionGetAttributes() { if(class_exists($this->modelClass)) { return json_encode([ 'result' => true, 'controller_id' => Inflector::pluralize(Inflector::camel2id($this->modelName)), 'attributes' => $this->getModelAttributes() ]); } return json_encode(['result' => false]); } }