public function __construct($module, $opt) { if (!$module || !$opt) { tprintError("请指定模块和操作!"); die; } //获取并解析xml文档 $xmlFilePath = APP_PATH . "xml/{$module}.xml"; self::$XML = new simple_html_dom(file_get_contents($xmlFilePath)); $this->module = $module; $this->opt = $opt; }
/** * 执行客户端的php任务 * @param string $taskName 任务名称 */ public static function runClient($taskName = null) { if ($taskName == null || $taskName == '') { tprintError('请传入需要执行的任务名称!'); die; } //加载框架核心类 self::_loadBaseLib(); //设置默认时区 date_default_timezone_set(TIME_ZONE); //设置时间用不超时 set_time_limit(0); //设置错误等级 error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_WARNING); Loader::import($taskName, IMPORT_CLIENT, '.php'); }
/** * 替换 CR换行符号 * @param $filename */ private static function replaceCR($filename) { $ext = FileUtils::getFileExt($filename); if (!in_array($ext, self::$extensions)) { return; } $content = file_get_contents($filename); $content = str_replace("\r", "\n", $content); if (file_put_contents($filename, $content)) { tprintOk("成功替换文件{$filename}"); self::$fileNum++; } else { tprintError("替换文件失败{$filename}"); //记录错误日志 file_put_contents(self::$logFile, "替换文件失败{$filename}", FILE_APPEND); } }
/** * @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 . "action"); FileUtils::makeFileDirs($module . "template/default"); $tables = $root->find("table"); $tempContent = file_get_contents(dirname(__DIR__) . "/template/controller.tpl"); foreach ($tables as $value) { $tableName = $value->name; //表名称 $actionName = $value->getAttribute("action-name"); if (!$actionName) { continue; } $className = ucfirst($actionName) . "Action"; $actionFile = $module . "action/{$className}.class.php"; if (file_exists($actionFile)) { //若文件已经存在则跳过 tprintWarning("Warnning : DAO interface file '{$actionFile}' is existed, skiped."); continue; } $content = str_replace("{module}", $configs["module"], $tempContent); $content = str_replace("{author}", $configs["author"], $content); $content = str_replace("{email}", $configs["email"], $content); $content = str_replace("{table_name}", $tableName, $content); $content = str_replace("{class_name}", $className, $content); if (file_put_contents($actionFile, $content) !== false) { tprintOk("create Controller file '{$actionFile}' successfully!"); } else { tprintError("Error : create Controller file '{$actionFile}' faild."); } } }
/** * 创建数据模型文件,生成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."); } } }
/** * 1. 重命名裁剪的缩略图,覆盖缩略图的原图 * 2. 删除其他缩略图缓存 * @param $filename */ private static function replaceImage($filename) { //1. 如果是裁剪图片,则将其覆盖原图 if (($pos = strpos($filename, '__crop__')) !== false) { $srcImage = substr($filename, 0, $pos - 1); //重命名 $message = "覆盖文件 {$filename} => {$srcImage}"; if (rename($filename, $srcImage)) { tprintOk($message . "成功!"); self::$repNum++; } else { self::addLog($message . "失败!"); tprintError($message); } } else { $pattern = '/\\.\\d+x\\d+\\./'; if (preg_match($pattern, $filename)) { $message = "删除文件 {$filename}"; if (unlink($filename)) { tprintOk($message . "成功!"); self::$delNum++; } else { tprintError($message . "失败!"); } } } }
/** * 创建表结构 * @param $configs */ private static function createTables($configs) { $tables = self::$XML->find("table"); foreach ($tables as $value) { $tableName = $configs["table-prefix"] . $value->name; self::query("DROP TABLE IF EXISTS `{$tableName}`"); $sql = "CREATE TABLE `{$tableName}`("; $pk = $value->find("pk", 0); if ($pk) { $sql .= "`{$pk->name}` {$pk->type} NOT NULL "; if ($pk->ai) { $sql .= "AUTO_INCREMENT "; } $sql .= "COMMENT '主键',"; } //添加字段 $fields = $value->find("fields", 0); if ($fields) { foreach ($fields->children() as $fd) { if ($fd->default || $fd->default === "0") { //has default value if (in_array($fd->default, self::$DEFAULT_VALUE_KEYWORD)) { $sql .= "`{$fd->name}` {$fd->type} NOT NULL DEFAULT {$fd->default} COMMENT '{$fd->comment}',"; } else { $sql .= "`{$fd->name}` {$fd->type} NOT NULL DEFAULT '{$fd->default}' COMMENT '{$fd->comment}',"; } } else { //has not default value $sql .= "`{$fd->name}` {$fd->type} NOT NULL COMMENT '{$fd->comment}',"; } //创建索引 if ($fd->getAttribute("add-index") == "true") { $indexType = $fd->getAttribute("index-type"); if ($indexType == "normal") { $sql .= "KEY `{$fd->name}` (`{$fd->name}`), "; } elseif ($indexType == "unique") { $sql .= "UNIQUE KEY `{$fd->name}` (`{$fd->name}`),"; } } } } if ($pk) { $sql .= "PRIMARY KEY (`{$pk->name}`)"; } $sql .= ") ENGINE={$value->engine} DEFAULT CHARSET={$configs['charset']} COMMENT='{$value->comment}' AUTO_INCREMENT=1 ;"; if (self::query($sql) !== false) { tprintOk("create table '{$tableName}' successfully."); } else { tprintError("create table '{$tableName}' faild."); tprintError(self::$DB_CONN->error); } } }
/** * 更新临时表的文章tags */ private function updateTags() { $items = self::$_TEMP_DB->getItems("select id,tags from fiidee_article"); foreach ($items as $value) { if (trim($value['tags']) == '') { continue; } $tags = self::$_TEMP_DB->getItems("select name from fiidee_article_tags WHERE id IN ({$value['tags']})"); if ($tags) { $__tags = array(); foreach ($tags as $__value) { $__tags[] = $__value['name']; } $__tags = implode(' ', $__tags); $__data = array('tags' => $__tags); $result = self::$_TEMP_DB->update('fiidee_article', $__data, "id =" . $value['id']); if ($result) { tprintOk("更新标签成功, ID : {$value['id']}, 标签 : {$__tags}"); } else { $message = "更新标签失败, ID : {$value['id']}"; tprintError($message); $this->addErrorLog($message); } } } }
/** * 生成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."); } }