/** * 执行应用程序 * @param $configs * @throws HeroException * @internal param 系统配置信息 $array */ public function execute($configs) { $this->setConfigs($configs); //请求初始化 $this->requestInit(); //invoker 方法调用 try { $this->actionInvoke(); } catch (HeroException $e) { if (APP_DEBUG) { //如果是调试模式就抛出异常 throw $e; } else { if (ADD_LOGS) { //否则记录日志 $logDir = APP_RUNTIME_PATH . "logs/" . APP_NAME . "/"; if (!file_exists($logDir)) { FileUtils::makeFileDirs($logDir); } file_put_contents($logDir . date("Y-m-d") . ".log", $e->toString(), FILE_APPEND); return; } else { die("Hacker Attempt!"); } } } //发送响应 $this->sendResponse(); }
/** * @see ICache::set(); * @param string $key * @param string $content * @param null $expire * @return int */ public function set($key, $content, $expire = 0) { $cacheFile = $this->getCacheFile($key); $dirname = dirname($cacheFile); if (!file_exists($dirname)) { FileUtils::makeFileDirs($dirname); } $data['expire'] = $expire; $data['data'] = $content; return file_put_contents($cacheFile, StringUtils::jsonEncode($data), LOCK_EX); }
/** * @see \herosphp\session\interfaces\ISession::start(). * @param null $config * @return mixed|void */ public static function start($config = null) { //初始化配置信息 self::$config = $config; self::$sessionSavePath = $config["session_save_path"]; if (!file_exists(self::$sessionSavePath)) { FileUtils::makeFileDirs(self::$sessionSavePath); } if (!is_writable(self::$sessionSavePath)) { E("session 目录" . self::$sessionSavePath . "不可写,请更改权限。"); } //初始化用户ip self::$userIp = $_SERVER['REMOTE_ADDR']; session_set_save_handler(array(__CLASS__, 'open'), array(__CLASS__, 'close'), array(__CLASS__, 'read'), array(__CLASS__, 'write'), array(__CLASS__, 'destroy'), array(__CLASS__, 'gc')); @session_start(); }
/** * @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."); } } }
/** * 编译模板 * @param string $tempFile 模板文件路径 * @param string $compileFile 编译文件路径 */ private function complieTemplate($tempFile, $compileFile) { //根据缓存情况编译模板 if (!file_exists($compileFile) || $this->cache == 1 && filemtime($compileFile) < filemtime($tempFile) || $this->cache == 0) { //获取模板文件 $content = @file_get_contents($tempFile); if ($content == FALSE) { if (APP_DEBUG) { E("加载模板文件 {" . $tempFile . "} 失败!请在相应的目录建立模板文件。"); } } //替换模板 $content = preg_replace(array_keys(self::$tempRules), self::$tempRules, $content); //生成编译目录 if (!file_exists(dirname($compileFile))) { FileUtils::makeFileDirs(dirname($compileFile)); } //生成php文件 if (!file_put_contents($compileFile, $content, LOCK_EX)) { if (APP_DEBUG) { E("生成编译文件 {$compileFile} 失败。"); } } } }
public function __construct($key) { $lockDir = APP_RUNTIME_PATH . 'lock/'; FileUtils::makeFileDirs($lockDir); $this->file_handler = fopen($lockDir . md5($key) . '.lock', 'w'); }
/** * 生成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."); } }