diff --git a/src/InputOutput/IHMImpl/Config/IHMConfigClass.php b/src/InputOutput/IHMImpl/Config/IHMConfigClass.php index 6198758..c6ea72e 100644 --- a/src/InputOutput/IHMImpl/Config/IHMConfigClass.php +++ b/src/InputOutput/IHMImpl/Config/IHMConfigClass.php @@ -14,7 +14,13 @@ class IHMConfigClass private static $constantsFile = "constants.xml"; private static $functionsFile = "functions.xml"; + + private static $paramTemplateDir = "ParamTemplate/"; + private static $paramTemplateFile = "ParamTemplateList.xml"; + + private static $paramTemplateGenerateDir = "generateTemplateParams"; + private static $dataDir = "data/"; private static $compilationDir = "compilation/"; @@ -76,6 +82,21 @@ class IHMConfigClass return IHM_SRC_DIR.self::$genericDataDir.self::$functionsDir.self::$functionsFile; } + public static function getParamTemplateFilePath($fileName) + { + return IHM_SRC_DIR.self::$genericDataDir.self::$paramTemplateDir.$fileName.".xml"; + } + + public static function getTemplateParamGeneratePath() + { + return self::getUserPath().self::$paramTemplateGenerateDir; + } + + public static function getParamTemplateListFilePath() + { + return IHM_SRC_DIR.self::$genericDataDir.self::$paramTemplateDir.self::$paramTemplateFile; + } + public static function getUserParamManagerFilePath() { return self::getUserWSPath().self::$userParamMgrFile; diff --git a/src/InputOutput/IHMImpl/ParamInfo/IHMInputOutputParamInfoClass.php b/src/InputOutput/IHMImpl/ParamInfo/IHMInputOutputParamInfoClass.php index 4573797..45bcd3f 100644 --- a/src/InputOutput/IHMImpl/ParamInfo/IHMInputOutputParamInfoClass.php +++ b/src/InputOutput/IHMImpl/ParamInfo/IHMInputOutputParamInfoClass.php @@ -8,12 +8,14 @@ class IHMInputOutputParamInfoClass implements InputOutputInterface { private $paramInfoData = null; + private $paramTemplateMgr = null; /* * @brief Constructor */ function __construct() { + $this->paramTemplateMgr = new IHMParamTemplateClass(); } /* @@ -42,6 +44,10 @@ class IHMInputOutputParamInfoClass implements InputOutputInterface if (!isset($input->paramId) || empty($input->paramId)) throw new Exception("Param info request need a param id as argument"); + $templatedParams = $this->paramTemplateMgr->getParamTemplates(); + + if (array_key_exists($input->paramId, $templatedParams)) + $this->paramInfoData->setTemplateInfo($templatedParams[$input->paramId]); $paramInfoFilePath = IHMConfigClass::getLocalParamInfoPath().'info_'.$input->paramId.".xml"; @@ -86,6 +92,9 @@ class IHMInputOutputParamInfoClass implements InputOutputInterface "success" => true, "data" => $data->getResult() ); + if ($data->getTemplateInfo() != NULL) { + $result["template"] = $data->getTemplateInfo(); + } } } diff --git a/src/InputOutput/IHMImpl/Params/DownloadImpl/IHMInputOutputParamsDownloadClass.php b/src/InputOutput/IHMImpl/Params/DownloadImpl/IHMInputOutputParamsDownloadClass.php index 310a204..9cbf585 100644 --- a/src/InputOutput/IHMImpl/Params/DownloadImpl/IHMInputOutputParamsDownloadClass.php +++ b/src/InputOutput/IHMImpl/Params/DownloadImpl/IHMInputOutputParamsDownloadClass.php @@ -74,7 +74,7 @@ class IHMInputOutputParamsDownloadClass extends IHMInputOutputParamsAbstractClas //add params to output foreach ($input->list as $param) { - $paramInfo = $this->paramManager->addExistingParam($param->name,$this->paramsData); + $paramInfo = $this->paramManager->addExistingParam($param->name, $this->paramsData); switch ($param->type) { case 0: //scalar - nothing to do diff --git a/src/InputOutput/IHMImpl/Params/GeneratorImpl/IHMInputOutputParamsGeneratorClass.php b/src/InputOutput/IHMImpl/Params/GeneratorImpl/IHMInputOutputParamsGeneratorClass.php index 489634f..6784d0f 100644 --- a/src/InputOutput/IHMImpl/Params/GeneratorImpl/IHMInputOutputParamsGeneratorClass.php +++ b/src/InputOutput/IHMImpl/Params/GeneratorImpl/IHMInputOutputParamsGeneratorClass.php @@ -16,7 +16,7 @@ class IHMInputOutputParamsGeneratorClass extends IHMInputOutputParamsAbstractCla $this->paramsData->setParamId($input->paramId); - $paramInfo = $this->paramManager->addExistingParam($input->paramId,$this->paramsData); + $paramInfo = $this->paramManager->addExistingParam($input->paramId, $this->paramsData); return $this->paramsData; } diff --git a/src/InputOutput/IHMImpl/Params/IHMInputOutputParamsAbstractClass.php b/src/InputOutput/IHMImpl/Params/IHMInputOutputParamsAbstractClass.php index 1d027b3..e90e129 100644 --- a/src/InputOutput/IHMImpl/Params/IHMInputOutputParamsAbstractClass.php +++ b/src/InputOutput/IHMImpl/Params/IHMInputOutputParamsAbstractClass.php @@ -10,6 +10,7 @@ abstract class IHMInputOutputParamsAbstractClass implements InputOutputInterface protected $paramManager = null; protected $expressionManager = null; protected $jobsManager = null; + protected $paramsData = null; protected $requestID = ""; protected $requestDirPrefix = ""; diff --git a/src/InputOutput/IHMImpl/Params/PlotImpl/IHMInputOutputParamsPlotClass.php b/src/InputOutput/IHMImpl/Params/PlotImpl/IHMInputOutputParamsPlotClass.php index 42b3225..a0671d6 100644 --- a/src/InputOutput/IHMImpl/Params/PlotImpl/IHMInputOutputParamsPlotClass.php +++ b/src/InputOutput/IHMImpl/Params/PlotImpl/IHMInputOutputParamsPlotClass.php @@ -619,7 +619,7 @@ class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass if ($paramData->{'param-drawing-object'}->{'serie-xaxis-param'} == '') continue; - $paramXInfo = $this->paramManager->addExistingParam($paramData->{'param-drawing-object'}->{'serie-xaxis-param'},$this->paramsData); + $paramXInfo = $this->paramManager->addExistingParam($paramData->{'param-drawing-object'}->{'serie-xaxis-param'}, $this->paramsData); if ($paramXInfo['id'] == '') throw new Exception('Cannot retrieve X parameter.'); $requestParamsNode->addParam($paramXInfo['id']); @@ -646,7 +646,7 @@ class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass foreach ($paramsData as $paramData) { //Param - $paramInfo = $this->paramManager->addExistingParam($paramData->{'param-id'},$this->paramsData); + $paramInfo = $this->paramManager->addExistingParam($paramData->{'param-id'}, $this->paramsData, $paramData->{'param-template-args'}); $paramInfo['indexes'] = array(); $dim1 = $paramData->{'param-dim-1'}; @@ -731,7 +731,7 @@ class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass continue; } - $paramColoredInfo = $this->paramManager->addExistingParam($paramData->{'param-drawing-object'}->{'serie-colored-param'},$this->paramsData); + $paramColoredInfo = $this->paramManager->addExistingParam($paramData->{'param-drawing-object'}->{'serie-colored-param'}, $this->paramsData); if ($paramColoredInfo['id'] == '') throw new Exception('Cannot retrieve colored parameter.'); $requestParamsNode->addParam($paramColoredInfo['id']); @@ -923,7 +923,7 @@ class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass case 'min-max': $errorBarTypeNode = $errorBarNode->getBarByType(RequestOutputPlotYSerieErrorBarTypeEnum::MINMAX); - $minParamInfo = $this->paramManager->addExistingParam($serieData->{'serie-errorbar-minparam'},$this->paramsData); + $minParamInfo = $this->paramManager->addExistingParam($serieData->{'serie-errorbar-minparam'}, $this->paramsData); if ($minParamInfo['id'] == '') throw new Exception('Cannot retrieve min. error parameter.'); $requestParamsNode->addParam($minParamInfo['id']); @@ -934,7 +934,7 @@ class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass else throw new Exception('Min parameter for error bar must be a component.'); - $maxParamInfo = $this->paramManager->addExistingParam($serieData->{'serie-errorbar-maxparam'},$this->paramsData); + $maxParamInfo = $this->paramManager->addExistingParam($serieData->{'serie-errorbar-maxparam'}, $this->paramsData); if ($maxParamInfo['id'] == '') throw new Exception('Cannot retrieve max. error parameter.'); $requestParamsNode->addParam($maxParamInfo['id']); @@ -948,7 +948,7 @@ class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass case 'delta': $errorBarTypeNode = $errorBarNode->getBarByType(RequestOutputPlotYSerieErrorBarTypeEnum::DELTA); - $deltaParamInfo = $this->paramManager->addExistingParam($serieData->{'serie-errorbar-deltaparam'},$this->paramsData); + $deltaParamInfo = $this->paramManager->addExistingParam($serieData->{'serie-errorbar-deltaparam'}, $this->paramsData); if ($deltaParamInfo['id'] == '') throw new Exception('Cannot retrieve delta error parameter.'); $requestParamsNode->addParam($deltaParamInfo['id']); diff --git a/src/InputOutput/IHMImpl/Params/StatisticsImpl/IHMInputOutputParamsStatisticsClass.php b/src/InputOutput/IHMImpl/Params/StatisticsImpl/IHMInputOutputParamsStatisticsClass.php index d135fa9..5b8c4cd 100644 --- a/src/InputOutput/IHMImpl/Params/StatisticsImpl/IHMInputOutputParamsStatisticsClass.php +++ b/src/InputOutput/IHMImpl/Params/StatisticsImpl/IHMInputOutputParamsStatisticsClass.php @@ -37,7 +37,7 @@ class IHMInputOutputParamsStatisticsClass extends IHMInputOutputParamsAbstractCl foreach ($paramFunctionAssociation as $param => $functions) { - $paramInfo = $this->paramManager->addExistingParam($param,$this->paramsData); + $paramInfo = $this->paramManager->addExistingParam($param, $this->paramsData); $paramsNode->addParam($paramInfo['id']); $outputParamNode = $catalogNode->addParam($paramInfo['id'],$paramInfo['indexes']); foreach ($functions as $function) $outputParamNode->addFunction($function); diff --git a/src/InputOutput/IHMImpl/Tools/IHMParamManagerClass.php b/src/InputOutput/IHMImpl/Tools/IHMParamManagerClass.php index bbde587..c5d9268 100644 --- a/src/InputOutput/IHMImpl/Tools/IHMParamManagerClass.php +++ b/src/InputOutput/IHMImpl/Tools/IHMParamManagerClass.php @@ -9,25 +9,27 @@ class IHMParamManagerClass { protected $userParameterLoader = null; protected $expressionParser = null; + protected $templateParamsManager = null; /* * @brief Constructor */ function __construct() { + $this->templateParamsManager = new IHMParamTemplateClass(); } /* * @brief Add an existing parameter */ - public function addExistingParam($param,$paramsData) + public function addExistingParam($param, $paramsData, $templateArgs = NULL) { if ($this->isDerivedParam($param)) return $this->addDerivedParam($param,$paramsData); else if ($this->isUploadedParam($param)) return $this->addUploadedParam($param,$paramsData); else - return $this->addLocalParam($param,$paramsData); + return $this->addLocalParam($param,$paramsData, $templateArgs); return ""; } @@ -63,7 +65,7 @@ class IHMParamManagerClass /* * @brief Add a local parameter */ - private function addLocalParam($param,$paramsData) + private function addLocalParam($param, $paramsData, $templateArgs) { //local parameter $indexes = array(); @@ -78,30 +80,48 @@ class IHMParamManagerClass } else $paramId = $param; - - $paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml"; - if (!file_exists($paramPath)) - throw new Exception('Cannot find parameter local file '.$paramId); + + //check templated parameter + $real_param_id = $paramId; + if ($this->templateParamsManager->isTemplatedParam($paramId)) { + $paramPath = $this->templateParamsManager->generateTemplatedParamFile($paramId, $templateArgs); + $real_param_id = $this->templateParamsManager->getTemplatedParamId($paramId, $templateArgs); + if (empty($paramPath) || !file_exists($paramPath)) + throw new Exception('Cannot generate parameter template file '.$paramId); + } + else { + $paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml"; + if (!file_exists($paramPath)) + throw new Exception('Cannot find parameter local file '.$paramId); + } - $paramsData->addParamToCopy($paramId,$paramPath); + $paramsData->addParamToCopy($real_param_id,$paramPath); - $this->addLinkedLocalParams($paramId,$paramsData); + $this->addLinkedLocalParams($paramId, $paramsData, $templateArgs); - return array("id" => $paramId, "indexes" => $indexes, "calib_infos" => $calib_infos); + return array("id" => $real_param_id, "indexes" => $indexes, "calib_infos" => $calib_infos); } /* * @brief Add linked parameter */ - private function addLinkedLocalParams($paramId,$paramsData) + private function addLinkedLocalParams($paramId,$paramsData,$templateArgs = NULL) { $doc = new DOMDocument("1.0", "UTF-8"); $doc->preserveWhiteSpace = false; $doc->formatOutput = true; - $paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml"; + $real_param_id = $real_param_id; + if ($this->templateParamsManager->isTemplatedParam($paramId)) { + $paramPath = $this->templateParamsManager->generateTemplatedParamFile($paramId, $templateArgs); + $real_param_id = $this->templateParamsManager->getTemplatedParamId($paramId, $templateArgs); + } + else { + $paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml"; + } + - if (!$doc->load($paramPath)) + if (empty($paramPath) || !$doc->load($paramPath)) throw new Exception('Cannot find parameter local file '.$paramId); //<get> @@ -120,9 +140,15 @@ class IHMParamManagerClass $linkedParamId = $amdaParamNode->getAttribute('name'); if ($linkedParamId == '') continue; - $linkedParamPath = IHMConfigClass::getLocalParamDBPath().$linkedParamId.".xml"; - $paramsData->addParamToCopy($linkedParamId,$linkedParamPath); - $this->addLinkedLocalParams($linkedParamId,$paramsData); + $real_linked_param_id = $linkedParamId; + if ($this->templateParamsManager->isTemplatedParam($linkedParamId)) { + $linkedParamPath = $this->templateParamsManager->generateTemplatedParamFile($linkedParamId, $templateArgs); + $real_linked_param_id = $this->templateParamsManager->getTemplatedParamId($linkedParamId, $templateArgs); + } + else + $linkedParamPath = IHMConfigClass::getLocalParamDBPath().$linkedParamId.".xml"; + $paramsData->addParamToCopy($real_linked_param_id,$linkedParamPath); + $this->addLinkedLocalParams($linkedParamId, $paramsData); } } diff --git a/src/InputOutput/IHMImpl/Tools/IHMParamTemplateClass.php b/src/InputOutput/IHMImpl/Tools/IHMParamTemplateClass.php new file mode 100644 index 0000000..7ce262d --- /dev/null +++ b/src/InputOutput/IHMImpl/Tools/IHMParamTemplateClass.php @@ -0,0 +1,225 @@ +<?php + +/** + * @class IHMParamTemplateClass + * @brief Class used to manage templated parameters + * @details + */ +class IHMParamTemplateClass +{ + protected $paramTemplateList = null; + + private static $paramTemplateNode = 'paramTemplate'; + private static $paramTemplateIdAtt = 'paramId'; + private static $paramTemplateFileNameAtt = 'fileName'; + private static $paramTemplateArgumentsNode = 'arguments'; + private static $paramTemplateArgumentNode = 'argument'; + private static $paramTemplateArgumentKeyAtt = 'key'; + private static $paramTemplateArgumentNameAtt = 'name'; + private static $paramTemplateArgumentTypeAtt = 'type'; + private static $paramTemplateArgumentDefaultAtt = 'default'; + private static $paramTemplateArgumentListItemNode = 'item'; + private static $paramTemplateArgumentListItemKeyAtt = 'key'; + private static $paramTemplateArgumentListItemNameAtt = 'name'; + + /* + * @brief Constructor + */ + function __construct() + { + } + + /* + * @brief Get list of templated parameters + */ + public function getParamTemplates() { + $this->loadUserParamManagerFile(); + return $this->paramTemplateList; + } + + /* + * @brief Check if param_id is a templated parameter + */ + public function isTemplatedParam($param_id) { + $list = $this->getParamTemplates(); + return array_key_exists($param_id, $list); + } + + /* + * @brief Get templated parameter id + */ + public function getTemplatedParamId($param_id, $template_args) { + $templatePath = $this->getTemplatePath($param_id); + + if (empty($templatePath)) + return ""; + + if (!isset($template_args)) + $template_args = array(); + + //Enrich template args with default values + $this->addDefaultValues($param_id, $template_args); + + return basename($this->replaceArgs($templatePath, $template_args), ".xml"); + } + + /* + * @brief Generate parameter file from template + */ + public function generateTemplatedParamFile($param_id, $template_args) { + $templatePath = $this->getTemplatePath($param_id); + if (empty($templatePath)) + return ""; + + if (!isset($template_args)) + $template_args = array(); + + //Enrich template args with default values + $this->addDefaultValues($param_id, $template_args); + + $dst_param_id = $this->getTemplatedParamId($param_id, $template_args); + + //Build destination file path + $dstDir = IHMConfigClass::getTemplateParamGeneratePath(); + if (!is_dir($dstDir)) + mkdir($dstDir); + $dstFilePath = $dstDir."/".$dst_param_id.".xml"; + + //Check if it's necessary to generate the parameter file + if (file_exists($dstFilePath)) { + $dateModifDst = filemtime($dstFilePath); + $dateModifTemplate = filemtime($templatePath); + + if ($dateModifTemplate != $dateModifDst) + //no modification - reuse old generated file + return $dstFilePath; + } + + //Generate file + $templateHandle = fopen($templatePath, "r"); + $dstHandle = fopen($dstFilePath, "w"); + if (!$templateHandle || !$dstHandle) + return ""; + while (($line = fgets($templateHandle)) !== false) { + fwrite($dstHandle, $this->replaceArgs($line, $template_args)); + } + fclose($templateHandle); + fclose($dstHandle); + + $dateModifTemplate = filemtime($templatePath); + touch($dstFilePath, $dateModifTemplate); + + return $dstFilePath; + } + + /* + * @brief Get template file path + */ + private function getTemplatePath($param_id) { + if (!$this->isTemplatedParam($param_id)) + return ""; + return IHMConfigClass::getParamTemplateFilePath($this->paramTemplateList[$param_id]['fileName']); + } + + /* + * @brief Replace args in string + */ + private function replaceArgs($string, $template_args) { + $result = $string; + foreach ($template_args as $template_arg_key => $template_arg_value) { + $result = str_replace("##".$template_arg_key."##", $template_arg_value, $result); + } + return $result; + } + + /* + * @brief Enrich Template args with default values + */ + private function addDefaultValues($param_id, &$template_args) { + $list = $this->getParamTemplates(); + + if (!array_key_exists($param_id, $list)) + return; + + $arguments = $list[$param_id]->arguments; + foreach ($arguments as $arg_key => $arg_def) { + if (!array_key_exists($arg_key, $template_args)) + $template_args[$arg_key] = $arg_def->default; + } + } + + /* + * @brief Load list of templated parameters + */ + private function loadUserParamManagerFile() + { + if (isset($this->paramTemplateList)) + return; + + $this->paramTemplateList = array(); + + //load xml file + $dom = new DomDocument("1.0"); + if (!$dom->load(IHMConfigClass::getParamTemplateListFilePath())) + return; + + $paramTemplateNodes = $dom->getElementsByTagName(self::$paramTemplateNode); + + foreach ($paramTemplateNodes as $paramTemplateNode) { + $paramId = $paramTemplateNode->getAttribute(self::$paramTemplateIdAtt); + $paramFileName = $paramTemplateNode->getAttribute(self::$paramTemplateFileNameAtt); + + if (empty($paramId) || empty($paramFileName)) + continue; + + $arguments = array(); + + $argumentsNode = $paramTemplateNode->getElementsByTagName(self::$paramTemplateArgumentsNode); + + if (count($argumentsNode) > 0) { + $argumentsNode = $argumentsNode->item(0); + $argumentNodes = $argumentsNode->getElementsByTagName(self::$paramTemplateArgumentNode); + + foreach($argumentNodes as $argumentNode) { + $argumentKey = $argumentNode->getAttribute(self::$paramTemplateArgumentKeyAtt); + $argumentName = $argumentNode->getAttribute(self::$paramTemplateArgumentNameAtt); + $argumentType = $argumentNode->getAttribute(self::$paramTemplateArgumentTypeAtt); + $argumentDefault = $argumentNode->getAttribute(self::$paramTemplateArgumentDefaultAtt); + + if (empty($argumentKey) || empty($argumentType)) + continue; + + $arguments[$argumentKey] = array( + "name" => (empty($argumentName) ? $argumentKey : $argumentName), + "type" => $argumentType, + "default" => $argumentDefault + ); + + switch ($argumentType) { + case "list" : + $arguments[$argumentKey]['items'] = array(); + + $itemNodes = $argumentNode->getElementsByTagName(self::$paramTemplateArgumentListItemNode); + foreach ($itemNodes as $itemNode) { + $itemKey = $itemNode->getAttribute(self::$paramTemplateArgumentListItemKeyAtt); + $itemName = $itemNode->getAttribute(self::$paramTemplateArgumentListItemNameAtt); + if ($itemKey == "") + continue; + $arguments[$argumentKey]['items'][$itemKey] = (empty($itemName) ? $itemKey : $itemName); + } + + break; + default: + //Nothing to do + } + } + } + + $this->paramTemplateList[$paramId] = array( + "fileName" => $paramFileName, + "arguments" => $arguments + ); + + } + } +} \ No newline at end of file diff --git a/src/Request/ParamInfoRequestDataClass.php b/src/Request/ParamInfoRequestDataClass.php index 8758ae2..9e851ea 100644 --- a/src/Request/ParamInfoRequestDataClass.php +++ b/src/Request/ParamInfoRequestDataClass.php @@ -21,6 +21,7 @@ class ParamInfoRequestDataClass extends RequestDataClass { private $filePath = ""; private $paramId = ""; + private $templateInfo = NULL; private $type = ParamInfoTypeEnumClass::UNKNOWN; private $result = NULL; @@ -63,6 +64,16 @@ class ParamInfoRequestDataClass extends RequestDataClass { $this->paramId = $paramId; } + + public function getTemplateInfo() + { + return $this->templateInfo; + } + + public function setTemplateInfo($templateInfo) + { + $this->templateInfo = $templateInfo; + } } ?> \ No newline at end of file -- libgit2 0.21.2