public function generate() { $code = $this->security . ' '; $code .= $this->static ? 'static ' : ''; $code .= '$' . $this->name . ' = '; if (is_array($this->value)) { $code .= \CodeGenerator::genArray($this->value); } else { $code .= '"' . str_replace('"', '\\"', $this->value) . '";'; } return $code; }
public function generateModel($module, $name, $codeName, $options) { $codeName = ucfirst($codeName); $class = new CodeGenerator\ClassGenerator(); $class->name = $codeName; $class->extends = '\\Model'; $modelCols = $this->parseColsForModel(); if (!empty($options['cols'])) { $modelCols = $this->parseColsForModel($options['cols']); $tableName = strtolower($module) . '_' . strtolower($codeName); $tableCols = $this->parseColsForTable($options['cols'], strtolower($codeName) . '_', $tableName); if (App::$cur->db->tableExist($tableName)) { foreach ($tableCols as $colKey => $params) { App::$cur->db->add_col($tableName, $colKey, $params); } } else { App::$cur->db->createTable($tableName, $tableCols); } } $class->addProperty('objectName', $name, true); $class->addProperty('cols', $modelCols['cols'], true); $class->addProperty('labels', $modelCols['labels'], true); $class->addMethod('relations', 'return ' . CodeGenerator::genArray($modelCols['relations']), [], true); $modelCode = "<?php \n\nnamespace {$module};\n\n" . $class->generate(); $modulePath = Module::getModulePath($module); Tools::createDir($modulePath . '/models'); file_put_contents($modulePath . '/models/' . $codeName . '.php', $modelCode); $config = Config::custom($modulePath . '/generatorHash.php'); $config['models/' . $codeName . '.php'] = md5($modelCode); Config::save($modulePath . '/generatorHash.php', $config); }
/** * Save config * * @param string $type * @param array $data * @param string $module * @param \App $app */ public static function save($type, $data, $module = '', $app = null) { if (!$app) { $app = App::$primary; } switch ($type) { case 'system': $path = INJI_SYSTEM_DIR . '/config/config.php'; self::$_configs['system'] = $data; Inji::$inst->event('Config-change-system', $data); break; case 'app': $path = $app->path . "/config/config.php"; self::$_configs['app'][$app->name] = $data; Inji::$inst->event('Config-change-app-' . $app->name, $data); break; case 'module': $path = $app->path . "/config/modules/{$module}.php"; self::$_configs['module'][$app->name][$module] = $data; Inji::$inst->event('Config-change-module-' . $app->name . '-' . $module, $data); break; case 'share': if ($module) { $path = INJI_PROGRAM_DIR . "/config/modules/{$module}.php"; self::$_configs['shareModules'][$module] = $data; Inji::$inst->event('Config-change-shareModules-' . $module, $data); } else { $path = INJI_PROGRAM_DIR . "/config/config.php"; self::$_configs['share'] = $data; Inji::$inst->event('Config-change-share', $data); } break; default: $path = $type; self::$_configs['custom'][$path] = $data; break; } $text = "<?php\nreturn " . CodeGenerator::genArray($data); Tools::createDir(substr($path, 0, strripos($path, '/'))); file_put_contents($path, $text); }
<?php echo "<?php\n"; ?> <?php echo "namespace {$module};\n"; ?> class <?php echo $codeName; ?> extends \Model { //modelParams<?php if (!empty($name)) { echo "\n static " . '$objectName = \'' . $name . "';"; } if (!empty($labels)) { echo "\n static " . '$labels = [' . CodeGenerator::genArray($labels, 1) . "\n ];"; } if (!empty($cols)) { echo "\n static " . '$cols = [' . CodeGenerator::genArray($cols, 1) . "\n ];"; } ?> //!modelParams //modelBody //!modelBody }