/** * 创建数据模型文件,生成beans配置文件 * @param simple_html_dom $xml */ public static function create($xml) { $modelDir = APP_PATH . "configs/models/"; if (!is_writable(dirname($modelDir))) { tprintError("Error: directory '{$modelDir}' is not writeadble, please add permissions."); return; } //create directory FileUtils::makeFileDirs($modelDir); $root = $xml->find("root", 1); $configs = array("module" => $root->getAttribute("module"), "author" => $root->getAttribute("author"), "email" => $root->getAttribute("email")); $tables = $root->find("table"); $tempContent = file_get_contents(dirname(__DIR__) . "/template/model.tpl"); foreach ($tables as $value) { $modelFile = $modelDir . ucfirst(GModel::underline2hump($value->name)) . ".model.php"; if (file_exists($modelFile)) { //若文件已经存在则跳过 tprintWarning("Warnning : model file '{$modelFile}' has existed,skiped."); continue; } $pk = $value->find("pk", 0); $content = str_replace("{table_name}", $value->name, $tempContent); if ($pk) { $content = str_replace("{pk}", $pk->name, $content); } $content = str_replace("{model_name}", ucfirst(GModel::underline2hump($value->name)) . "Model", $content); $content = str_replace("{app_name}", APP_NAME, $content); $content = str_replace("{author}", $configs["author"], $content); $content = str_replace("{email}", $configs["email"], $content); if (file_put_contents($modelFile, $content) !== false) { tprintOk("create model file '{$modelFile}' successfully."); } else { tprintError("Error: create model file '{$modelFile}' faild."); } } }
/** * 生成Dao文件 * @param simple_html_dom $xml */ public static function create($xml) { $moduleDir = APP_PATH . "modules/"; if (!is_writable(dirname($moduleDir))) { tprintError("directory '{$moduleDir}' is not writeable, please add permissions."); return; } $root = $xml->find("root", 1); $configs = array("module" => $root->getAttribute("module"), "author" => $root->getAttribute("author"), "email" => $root->getAttribute("email")); //创建目录 $module = $moduleDir . $configs["module"] . "/"; FileUtils::makeFileDirs($module . "dao/interfaces"); $serviceConfigs = $root->find("service-config service"); //get service configs foreach ($serviceConfigs as $value) { $className = $value->dao; //class name of dao $models = $value->model; //get the association models $interfaceName = "I" . $className; //生成接口文件 $daoInterface = $module . "dao/interfaces/{$interfaceName}.class.php"; if (file_exists($daoInterface)) { //若文件已经存在则跳过 tprintWarning("Warnning : DAO interface file '{$daoInterface}' is existed, skiped."); continue; } $sb = new StringBuffer(); $sb->appendLine('<?php'); $sb->appendLine("namespace {$configs["module"]}\\dao\\interfaces;"); $sb->appendLine('use common\\dao\\interfaces\\ICommonDao;'); $sb->appendLine('/**'); $sb->appendLine(" * {$configs["module"]}(DAO)接口"); $sb->appendLine(" * @package {$configs["module"]}\\dao\\interfaces"); $sb->appendLine(" * @author {$configs["author"]}<{$configs["email"]}>"); $sb->appendLine(' */'); $sb->appendLine("interface {$interfaceName} extends ICommonDao {}"); if (file_put_contents($daoInterface, $sb->toString()) !== false) { tprintOk("create DAO interface file '{$daoInterface}' successfully!"); } else { tprintError("Error : create DAO interface file '{$daoInterface}' faild."); } //生成实现dao $daoImpl = $module . "dao/{$className}.class.php"; if (file_exists($daoImpl)) { //若文件已经存在则跳过 tprintWarning("Warnning : DAO file '{$daoImpl}' is existed, skiped."); continue; } $sb = new StringBuffer(); $sb->appendLine('<?php'); $sb->appendLine("namespace {$configs["module"]}\\dao;"); $sb->appendLine(""); $sb->appendLine("use {$configs["module"]}\\dao\\interfaces\\{$interfaceName};"); $sb->appendLine("use common\\dao\\CommonDao;"); $sb->appendLine("use herosphp\\core\\Loader;"); $sb->appendLine(""); $sb->appendLine("Loader::import('{$configs["module"]}.dao.interfaces.{$interfaceName}');"); $sb->appendLine(""); $sb->appendLine('/**'); $sb->appendLine(" * {$configs["module"]}(DAO)接口实现"); $sb->appendLine(" * @package {$configs["module"]}\\dao"); $sb->appendLine(" * @author {$configs["author"]}<{$configs["email"]}>"); $sb->appendLine(' */'); $sb->appendLine("class {$className} extends CommonDao implements {$interfaceName} {"); if (strpos($models, ",") !== false) { $models = explode(",", $models); foreach ($models as $m) { $sb->appendLine(""); $dao = GModel::underline2hump($m) . "Dao"; $sb->appendTab("/**", 1); $sb->appendTab(" * @var \\herosphp\\model\\C_Model", 1); $sb->appendTab(" */", 1); $sb->appendTab("private \${$dao} = null;", 1); } $sb->appendLine(""); $sb->appendTab("/**", 1); $parameters = array(); foreach ($models as $m) { $m = "\$" . GModel::underline2hump($m) . "Model"; $sb->appendTab(" * @param {$m}", 1); $parameters[] = $m; } $sb->appendTab(" */", 1); $sb->appendTab("public function __construct(" . implode(", ", $parameters) . ") {", 1); //init the modelDao $sb->appendTab("\$this->setModelDao(Loader::model(" . array_shift($parameters) . "));", 2); for ($i = 1; $i < count($models); $i++) { if (!$parameters[$i - 1]) { continue; } $m = GModel::underline2hump($models[$i]) . "Dao"; $sb->appendTab("\$this->{$m} = Loader::model({$parameters[$i - 1]});", 2); } $sb->appendTab("}", 1); } $sb->appendLine("}"); if (file_put_contents($daoImpl, $sb->toString()) !== false) { tprintOk("create DAO file '{$daoImpl}' successfully."); } else { tprintError("Error : create DAO file '{$daoImpl}' faild."); } } }
/** * 生成service文件 * @param simple_html_dom $xml */ public static function create($xml) { $moduleDir = APP_PATH . "modules/"; if (!is_writable(dirname($moduleDir))) { tprintError("directory '{$moduleDir}' is not writeable, please add permissions."); return; } $root = $xml->find("root", 1); $configs = array("module" => $root->getAttribute("module"), "author" => $root->getAttribute("author"), "email" => $root->getAttribute("email")); //创建目录 $module = $moduleDir . $configs["module"] . "/"; FileUtils::makeFileDirs($module . "service/interfaces"); $serviceConfigs = $root->find("service-config service"); foreach ($serviceConfigs as $value) { $className = $value->name; //class name of service $interfaceName = "I" . $className; //生成接口文件 $serviceInterface = $module . "service/interfaces/{$interfaceName}.class.php"; if (file_exists($serviceInterface)) { //若文件已经存在则跳过 tprintWarning("Warnning : The service interface file '{$serviceInterface}' is existed, skiped."); continue; } $sb = new StringBuffer(); $sb->appendLine('<?php'); $sb->appendLine("namespace {$configs["module"]}\\service\\interfaces;"); $sb->appendLine('use common\\service\\interfaces\\ICommonService;'); $sb->appendLine('/**'); $sb->appendLine(" * {$configs["module"]}服务接口"); $sb->appendLine(" * @package {$configs["module"]}\\service\\interfaces"); $sb->appendLine(" * @author {$configs["author"]}<{$configs["email"]}>"); $sb->appendLine(' */'); $sb->appendLine("interface {$interfaceName} extends ICommonService{}"); if (file_put_contents($serviceInterface, $sb->toString()) !== false) { tprintOk("create service interface file '{$serviceInterface}' successfully."); } else { tprintError("Error : create service interface file '{$serviceInterface}' faild."); } //生成实现dao $serviceImpl = $module . "service/{$className}.class.php"; if (file_exists($serviceImpl)) { //若文件已经存在则跳过 tprintWarning("Warnning : The service file '{$serviceImpl}' is existed, skiped."); continue; } $sb = new StringBuffer(); $sb->appendLine('<?php'); $sb->appendLine("namespace {$configs["module"]}\\service;"); $sb->appendLine(""); $sb->appendLine("use {$configs["module"]}\\service\\interfaces\\{$interfaceName};"); $sb->appendLine("use common\\service\\CommonService;"); $sb->appendLine("use herosphp\\core\\Loader;"); $sb->appendLine(""); $sb->appendLine("Loader::import('{$configs["module"]}.service.interfaces.{$interfaceName}');"); $sb->appendLine(""); $sb->appendLine('/**'); $sb->appendLine(" * {$configs["module"]}(Service)接口实现"); $sb->appendLine(" * @package {$configs["module"]}\\service"); $sb->appendLine(" * @author {$configs["author"]}<{$configs["email"]}>"); $sb->appendLine(' */'); $sb->appendLine("class {$className} extends CommonService implements {$interfaceName} {}"); if (file_put_contents($serviceImpl, $sb->toString()) !== false) { tprintOk("create Service file '{$serviceImpl}' succcessfully."); } else { tprintError("Error : create Service file '{$serviceImpl}' faild."); } } //生成beans配置文件 $beansDir = APP_PATH . "configs/beans/"; $beansFile = $beansDir . "beans.{$root->module}.config.php"; FileUtils::makeFileDirs($beansDir); //create beans directory $sb = new StringBuffer(); $sb->appendLine('<?php'); $sb->appendLine('use herosphp\\bean\\Beans;'); $sb->appendLine('/**'); $sb->appendLine(" * {$configs["module"]}模块Beans装配配置"); $sb->appendLine(" * @author {$configs["author"]}<{$configs["email"]}>"); $sb->appendLine(' */'); $sb->appendLine('$beans = array('); foreach ($serviceConfigs as $value) { $serviceClassName = $value->name; $daoClassName = $value->dao; $models = $value->model; //get the association models if (strpos($models, ",") !== false) { $models = explode(",", $models); foreach ($models as $key => $value) { //转驼峰 $assoc[$key] = ucfirst(GModel::underline2hump($value)); } } $sb->appendTab("//{$className} configs", 1); if (is_array($models)) { $sb->appendTab("'{$configs["module"]}." . GModel::underline2hump($models[0]) . ".service' => array(", 1); } else { $sb->appendTab("'{$configs["module"]}." . GModel::underline2hump($models) . ".service' => array(", 1); } $sb->appendTab("'@type' => Beans::BEAN_OBJECT,", 2); $sb->appendTab("'@class' => '{$configs["module"]}\\service\\{$serviceClassName}',", 2); $sb->appendTab("'@attributes' => array(", 2); $sb->appendTab("'@bean/modelDao'=>array(", 3); $sb->appendTab("'@type'=>Beans::BEAN_OBJECT,", 4); $sb->appendTab("'@class'=>'{$configs["module"]}\\dao\\{$daoClassName}',", 4); if (is_array($models)) { $sb->appendTab("'@params' => array('" . implode("','", $assoc) . "')", 4); } else { $sb->appendTab("'@params' => array('" . ucfirst(GModel::underline2hump($models)) . "')", 4); } $sb->appendTab(")", 3); $sb->appendTab("),", 2); $sb->appendTab("),", 1); } $sb->appendLine(');'); $sb->appendLine('return $beans;'); if (file_put_contents($beansFile, $sb->toString()) !== false) { tprintOk("create Beans config file '{$beansFile}' successfully."); } else { tprintError("create Beans config file '{$beansFile}' faild."); } }
public function run() { $gmodel = new GModel($_SERVER["argv"][2], $_SERVER["argv"][3]); $gmodel->execute(); //$gmodel }