2025-10-10 01:18:27 +03:00

170 lines
5.0 KiB
PHP

<?php
namespace drobnitsa\dictionaryGenerator;
use common\models\Addition;
use Yii;
use yii\gii\CodeFile;
use yii\helpers\Inflector;
class Generator extends yii\gii\Generator
{
public $modelNs = 'common\models';
public $modelName = '';
public $attributes;
public $moduleId = 'backend';
public $controllerId = '';
public $generateMigration = true;
public $migrationNs = 'console\migrations';
public $migrationName = '';
public function getName()
{
return 'Dictionary CRUD Generator';
}
public function getDescription()
{
return 'Генерирует контроллер и виды для заданной модели и полей.';
}
public function rules()
{
return [
[['modelNs', 'modelName', 'moduleId', 'controllerId'], 'required'],
[['migrationNs', 'modelNs', 'moduleId'], 'match', 'pattern' => '/^[\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., <code>app\models</code>',
'moduleId' => 'This is the module name for the controller and view to be generated, e.g., <code>app\controllers</code>',
]);
}
/**
* @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]);
}
}