Blame view

src/InputOutput/IHMImpl/Tools/IHMParamTemplateClass.php 9.59 KB
bf27ba04   Benjamin Renard   Add templated par...
1
2
3
4
5
6
7
8
9
10
<?php

/**
 * @class IHMParamTemplateClass
 * @brief Class used to manage templated parameters
 * @details
 */
class IHMParamTemplateClass
{
	protected $paramTemplateList = null;
ffc5cb81   Elena.Budnik   temporary commit
11
	protected $paramTemplateListFilePath;
bf27ba04   Benjamin Renard   Add templated par...
12
	
ffc5cb81   Elena.Budnik   temporary commit
13
14
15
16
17
18
19
20
	protected static $paramTemplateNode                    = 'paramTemplate';
	protected static $paramTemplateIdAtt                   = 'paramId';
	protected static $paramTemplateFileNameAtt             = 'fileName';
	protected static $paramTemplateArgumentsNode           = 'arguments';
	protected static $paramTemplateArgumentNode            = 'argument';
	protected static $paramTemplateArgumentKeyAtt          = 'key';
	protected static $paramTemplateArgumentNameAtt         = 'name';
	protected static $paramTemplateArgumentTypeAtt         = 'type';
a7011f4d   Benjamin Renard   Generated list fo...
21
22
23
	protected static $paramTemplateArgumentMinKeyAtt       = 'minkey';
	protected static $paramTemplateArgumentMaxKeyAtt       = 'maxkey';
	protected static $paramTemplateArgumentNameTplAtt      = 'nametpl';
ffc5cb81   Elena.Budnik   temporary commit
24
25
26
27
	protected static $paramTemplateArgumentDefaultAtt      = 'default';
	protected static $paramTemplateArgumentListItemNode    = 'item';
	protected static $paramTemplateArgumentListItemKeyAtt  = 'key';
	protected static $paramTemplateArgumentListItemNameAtt = 'name';
bf27ba04   Benjamin Renard   Add templated par...
28
29
30
31
32
33
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
ffc5cb81   Elena.Budnik   temporary commit
34
		$this->paramTemplateListFilePath = IHMConfigClass::getParamTemplateListFilePath();
bf27ba04   Benjamin Renard   Add templated par...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	}
	
	/*
	 * @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);
	}
	
	/*
e4545ed5   Benjamin Renard   Implement templat...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
	 * 
	 */
	public function parseTemplatedParam($param_expression) {
		$exploded_param_expression = explode('_',$param_expression);
		
		$tmp_paramid = "";
		$real_param_id = "";
		
		foreach ($exploded_param_expression as $expression_part) {
			//add one by one each part of the expression to check if it's a templated parameter
			if ($tmp_paramid != "")
				$tmp_paramid .= "_";
			$tmp_paramid .= $expression_part;
			if ($this->isTemplatedParam($tmp_paramid)) {
				$real_param_id = $tmp_paramid;
				break;
			}
		}
		
		if (empty($real_param_id))
			return FALSE;
		
		$template_args_str = str_replace($real_param_id, "", $param_expression);
		
		if ($template_args_str == "")
			//A templated param need at least one argument
			return FALSE;
		
		$template_args_info = $this->paramTemplateList[$real_param_id];
		
		$args_format = "";
		foreach ($template_args_info["arguments"] as $arg) {
			$args_format .= "_";
			switch($arg["type"]) {
				case 'float' :
				case 'double' :
					$args_format .= "%f";
					break;
				case 'int' :
c76ef7b2   Benjamin Renard   Add boolean argum...
93
				case 'bool' :
e4545ed5   Benjamin Renard   Implement templat...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
					$args_format .= "%d";
					break;
				default:
					$args_format .= "%s";
			}
		}
		
		$exploded_args = sscanf($template_args_str , $args_format);
		
		if ($exploded_args === -1)
			return FALSE;
		
		$template_args = array();
		$i = 0;
		foreach ($template_args_info["arguments"] as $arg_key => $arg) {
			$template_args[$arg_key] = $exploded_args[$i];
			++$i;
		}
		
		return array(
			"paramid"       => $real_param_id,
			"fullparamid" => $param_expression,
			"template_args" => $template_args
		);
	}
	
	/*
bf27ba04   Benjamin Renard   Add templated par...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
	 * @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
	 */
4fe90834   Benjamin Renard   Fix bug with get ...
190
	public function getTemplatePath($param_id) {
bf27ba04   Benjamin Renard   Add templated par...
191
192
193
194
		if (!$this->isTemplatedParam($param_id))
			return "";
		return IHMConfigClass::getParamTemplateFilePath($this->paramTemplateList[$param_id]['fileName']);
	}
7911f4bc   Benjamin Renard   Another bug with ...
195
196
197
198
199
200
201
202
203
204
205
206
207

	/*
	 *  @brief Get template id by template file name
	 */
	public function getTemplateId($template_name) {
		$list = $this->getParamTemplates();
		foreach ($list as $param_id => $param_info) {
			if ($param_info['fileName'] == $template_name) {
				return $param_id;
			}
		}
		return FALSE;
	}
bf27ba04   Benjamin Renard   Add templated par...
208
209
	
	/*
4fe90834   Benjamin Renard   Fix bug with get ...
210
211
212
213
214
215
216
217
218
	 * 
	 */
	public function getTemplateFileName($param_id) {
		if (!$this->isTemplatedParam($param_id))
			return "";
		return $this->paramTemplateList[$param_id]['fileName'];
	}
	
	/*
bf27ba04   Benjamin Renard   Add templated par...
219
220
	 * @brief Replace args in string
	*/
7d35612a   Benjamin Renard   Fix ParamManager ...
221
	public function replaceArgs($string, $template_args) {
bf27ba04   Benjamin Renard   Add templated par...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
		$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
	*/
ffc5cb81   Elena.Budnik   temporary commit
248
	protected function loadUserParamManagerFile()
bf27ba04   Benjamin Renard   Add templated par...
249
250
251
252
253
	{
		if (isset($this->paramTemplateList))
			return;
		
		$this->paramTemplateList = array();
ffc5cb81   Elena.Budnik   temporary commit
254
	 
bf27ba04   Benjamin Renard   Add templated par...
255
256
		//load xml file
		$dom = new DomDocument("1.0");
ffc5cb81   Elena.Budnik   temporary commit
257
		if (!$dom->load($this->paramTemplateListFilePath))
bf27ba04   Benjamin Renard   Add templated par...
258
			return;
ffc5cb81   Elena.Budnik   temporary commit
259
			
bf27ba04   Benjamin Renard   Add templated par...
260
		$paramTemplateNodes = $dom->getElementsByTagName(self::$paramTemplateNode);
ffc5cb81   Elena.Budnik   temporary commit
261

bf27ba04   Benjamin Renard   Add templated par...
262
263
264
265
266
267
		foreach ($paramTemplateNodes as $paramTemplateNode) {
			$paramId       = $paramTemplateNode->getAttribute(self::$paramTemplateIdAtt);
			$paramFileName = $paramTemplateNode->getAttribute(self::$paramTemplateFileNameAtt);
			
			if (empty($paramId) || empty($paramFileName))
				continue;
ffc5cb81   Elena.Budnik   temporary commit
268
				
bf27ba04   Benjamin Renard   Add templated par...
269
270
271
272
273
274
275
276
277
278
279
280
281
			$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);
a7011f4d   Benjamin Renard   Generated list fo...
282
283
284
					$argumentMinKey = $argumentNode->getAttribute(self::$paramTemplateArgumentMinKeyAtt);
					$argumentMaxKey = $argumentNode->getAttribute(self::$paramTemplateArgumentMaxKeyAtt);
					$argumentNameTpl = $argumentNode->getAttribute(self::$paramTemplateArgumentNameTplAtt);
bf27ba04   Benjamin Renard   Add templated par...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
					
					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);
							}
a7011f4d   Benjamin Renard   Generated list fo...
307
308
309
310
311
312
313
314
							break;
						case "generated-list":
							$arguments[$argumentKey]['type'] = 'list';
							$arguments[$argumentKey]['items'] = array();
							for ($key = intval($argumentMinKey) ; $key <= intval($argumentMaxKey); ++$key) {
								$itemName = str_replace('##key##',$key,$argumentNameTpl);
								$arguments[$argumentKey]['items'][strval($key)] = $itemName;
							}
bf27ba04   Benjamin Renard   Add templated par...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
							break;
						default:
							//Nothing to do
					}
				}
			}
			
			$this->paramTemplateList[$paramId] = array(
				"fileName"  => $paramFileName,
				"arguments" => $arguments
			);
			
		}
	}
a7011f4d   Benjamin Renard   Generated list fo...
329
}