/** * {@inheritdoc} */ public function generate() { $files = []; $relations = $this->generateRelations(); $db = $this->getDbConnection(); foreach ($this->getTableNames() as $tableName) { list($relations, $translations) = array_values($this->extractTranslations($tableName, $relations)); //var_dump($relations,$tableName);exit; $className = php_sapi_name() === 'cli' ? $this->generateClassName($tableName) : $this->modelClass; $queryClassName = $this->generateQuery ? $this->generateQueryClassName($className) : false; $tableSchema = $db->getTableSchema($tableName); $params = ['tableName' => $tableName, 'className' => $className, 'queryClassName' => $queryClassName, 'tableSchema' => $tableSchema, 'labels' => $this->generateLabels($tableSchema), 'hints' => $this->generateHints($tableSchema), 'rules' => $this->generateRules($tableSchema), 'relations' => isset($relations[$tableName]) ? $relations[$tableName] : [], 'ns' => $this->ns, 'enum' => $this->getEnum($tableSchema->columns)]; if (!empty($translations)) { $params['translation'] = $translations; } $params['blameable'] = $this->generateBlameable($tableSchema); $params['timestamp'] = $this->generateTimestamp($tableSchema); $files[] = new CodeFile(Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/base/' . $className . $this->baseClassSuffix . '.php', $this->render('model.php', $params)); $modelClassFile = Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $className . '.php'; if ($this->generateModelClass || !is_file($modelClassFile)) { $files[] = new CodeFile($modelClassFile, $this->render('model-extended.php', $params)); } if ($queryClassName) { $queryClassFile = Yii::getAlias('@' . str_replace('\\', '/', $this->queryNs)) . '/' . $queryClassName . '.php'; if ($this->generateModelClass || !is_file($queryClassFile)) { $params = ['className' => $queryClassName, 'modelClassName' => $className]; $files[] = new CodeFile($queryClassFile, $this->render('query.php', $params)); } } /* * create gii/[name]GiiantModel.json with actual form data */ $suffix = str_replace(' ', '', $this->getName()); $formDataDir = Yii::getAlias('@' . str_replace('\\', '/', $this->ns)); $formDataFile = StringHelper::dirname($formDataDir) . '/gii' . '/' . $tableName . $suffix . '.json'; $formData = json_encode(SaveForm::getFormAttributesValues($this, $this->formAttributes())); $files[] = new CodeFile($formDataFile, $formData); } return $files; }
public function generate() { $accessDefinitions = (require $this->getTemplatePath() . '/access_definition.php'); $this->controllerNs = \yii\helpers\StringHelper::dirname(ltrim($this->controllerClass, '\\')); $this->moduleNs = \yii\helpers\StringHelper::dirname(ltrim($this->controllerNs, '\\')); $controllerName = substr(\yii\helpers\StringHelper::basename($this->controllerClass), 0, -10); if ($this->singularEntities) { $this->modelClass = Inflector::singularize($this->modelClass); $this->controllerClass = Inflector::singularize(substr($this->controllerClass, 0, strlen($this->controllerClass) - 10)) . 'Controller'; $this->searchModelClass = Inflector::singularize($this->searchModelClass); } $controllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php'); $baseControllerFile = StringHelper::dirname($controllerFile) . '/base/' . StringHelper::basename($controllerFile); $restControllerFile = StringHelper::dirname($controllerFile) . '/api/' . StringHelper::basename($controllerFile); /* * search generated migration and overwrite it or create new */ $migrationDir = StringHelper::dirname(StringHelper::dirname($controllerFile)) . '/migrations'; if (file_exists($migrationDir) && ($migrationDirFiles = glob($migrationDir . '/m*_' . $controllerName . '00_access.php'))) { $this->migrationClass = pathinfo($migrationDirFiles[0], PATHINFO_FILENAME); } else { $this->migrationClass = 'm' . date('ymd_Hi') . '00_' . $controllerName . '_access'; } $files[] = new CodeFile($baseControllerFile, $this->render('controller.php', ['accessDefinitions' => $accessDefinitions])); $params['controllerClassName'] = \yii\helpers\StringHelper::basename($this->controllerClass); if ($this->overwriteControllerClass || !is_file($controllerFile)) { $files[] = new CodeFile($controllerFile, $this->render('controller-extended.php', $params)); } if ($this->overwriteRestControllerClass || !is_file($restControllerFile)) { $files[] = new CodeFile($restControllerFile, $this->render('controller-rest.php', $params)); } if (!empty($this->searchModelClass)) { $searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php')); if ($this->overwriteSearchModelClass || !is_file($searchModel)) { $files[] = new CodeFile($searchModel, $this->render('search.php')); } } $viewPath = $this->getViewPath(); $templatePath = $this->getTemplatePath() . '/views'; foreach (scandir($templatePath) as $file) { if (empty($this->searchModelClass) && $file === '_search.php') { continue; } if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') { echo $file; $files[] = new CodeFile("{$viewPath}/{$file}", $this->render("views/{$file}", ['permisions' => $permisions])); } } if ($this->generateAccessFilterMigrations) { /* * access migration */ $migrationFile = $migrationDir . '/' . $this->migrationClass . '.php'; //var_dump($migrationFile);exit; $files[] = new CodeFile($migrationFile, $this->render('migration_access.php', ['accessDefinitions' => $accessDefinitions])); /* * access roles translation */ $forRoleTranslationFile = StringHelper::dirname(StringHelper::dirname($controllerFile)) . '/messages/for-translation/' . $controllerName . '.php'; $files[] = new CodeFile($forRoleTranslationFile, $this->render('roles-translation.php', ['accessDefinitions' => $accessDefinitions])); } /* * create gii/[name]GiantCRUD.json with actual form data */ $suffix = str_replace(' ', '', $this->getName()); $controllerFileinfo = pathinfo($controllerFile); $formDataFile = StringHelper::dirname(StringHelper::dirname($controllerFile)) . '/gii/' . str_replace('Controller', $suffix, $controllerFileinfo['filename']) . '.json'; //$formData = json_encode($this->getFormAttributesValues()); $formData = json_encode(SaveForm::getFormAttributesValues($this, $this->formAttributes())); $files[] = new CodeFile($formDataFile, $formData); return $files; }