Blame view

src/InputOutput/IHMImpl/Tools/IHMParamTemplateClass.php 10.3 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
	 * @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) {
6f98ec26   Benjamin Renard   Write argument it...
176
			fwrite($dstHandle, $this->replaceArgs($line, $template_args, $this->getArguments($param_id)));
bf27ba04   Benjamin Renard   Add templated par...
177
178
179
180
181
182
183
184
185
186
187
188
189
		}
		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
	*/
6f98ec26   Benjamin Renard   Write argument it...
221
	public function replaceArgs($string, $template_args, $arguments = array()) {
bf27ba04   Benjamin Renard   Add templated par...
222
		$result = $string;
6f98ec26   Benjamin Renard   Write argument it...
223
224
225
		if (empty($template_args)) {
			return $result;
		}
bf27ba04   Benjamin Renard   Add templated par...
226
227
		foreach ($template_args as $template_arg_key => $template_arg_value) {
			$result = str_replace("##".$template_arg_key."##", $template_arg_value, $result);
6f98ec26   Benjamin Renard   Write argument it...
228
229
230
231
			if (array_key_exists($template_arg_key, $arguments) && ($arguments[$template_arg_key]['type'] == 'list')) {
				$item_name = array_key_exists($template_arg_value, $arguments[$template_arg_key]['items']) ? $arguments[$template_arg_key]['items'][$template_arg_value] : 'Unknown';
				$result = str_replace("@@".$template_arg_key."@@", $item_name, $result);
			}
bf27ba04   Benjamin Renard   Add templated par...
232
233
234
235
236
237
238
239
		}
		return $result;
	}
	
	/*
	 * @brief Enrich Template args with default values
	 */
	private function addDefaultValues($param_id, &$template_args) {
6f98ec26   Benjamin Renard   Write argument it...
240
241
		$arguments = $this->getArguments($param_id);
		if (empty($arguments))
bf27ba04   Benjamin Renard   Add templated par...
242
			return;
bf27ba04   Benjamin Renard   Add templated par...
243
244
245
246
247
		foreach ($arguments as $arg_key => $arg_def) {
			if (!array_key_exists($arg_key, $template_args))
				$template_args[$arg_key] = $arg_def->default;
		}
	}
6f98ec26   Benjamin Renard   Write argument it...
248
249
250
251
252
253
254
255
256
257
258

	/*
	 * @brief Get list of arguments for a given parameter
	 */
	protected function getArguments($param_id) {
		$list = $this->getParamTemplates();

		if (!array_key_exists($param_id, $list) || !isset($list[$param_id]['arguments']))
			return array();
		return $list[$param_id]['arguments'];
	}
bf27ba04   Benjamin Renard   Add templated par...
259
260
261
262
	
	/*
	 * @brief Load list of templated parameters
	*/
ffc5cb81   Elena.Budnik   temporary commit
263
	protected function loadUserParamManagerFile()
bf27ba04   Benjamin Renard   Add templated par...
264
265
266
267
268
	{
		if (isset($this->paramTemplateList))
			return;
		
		$this->paramTemplateList = array();
ffc5cb81   Elena.Budnik   temporary commit
269
	 
bf27ba04   Benjamin Renard   Add templated par...
270
271
		//load xml file
		$dom = new DomDocument("1.0");
ffc5cb81   Elena.Budnik   temporary commit
272
		if (!$dom->load($this->paramTemplateListFilePath))
bf27ba04   Benjamin Renard   Add templated par...
273
			return;
ffc5cb81   Elena.Budnik   temporary commit
274
			
bf27ba04   Benjamin Renard   Add templated par...
275
		$paramTemplateNodes = $dom->getElementsByTagName(self::$paramTemplateNode);
ffc5cb81   Elena.Budnik   temporary commit
276

bf27ba04   Benjamin Renard   Add templated par...
277
278
279
280
281
282
		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
283
				
bf27ba04   Benjamin Renard   Add templated par...
284
285
286
287
288
289
290
291
292
293
294
295
296
			$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...
297
298
299
					$argumentMinKey = $argumentNode->getAttribute(self::$paramTemplateArgumentMinKeyAtt);
					$argumentMaxKey = $argumentNode->getAttribute(self::$paramTemplateArgumentMaxKeyAtt);
					$argumentNameTpl = $argumentNode->getAttribute(self::$paramTemplateArgumentNameTplAtt);
bf27ba04   Benjamin Renard   Add templated par...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
					
					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...
322
323
324
325
326
327
328
329
							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...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
							break;
						default:
							//Nothing to do
					}
				}
			}
			
			$this->paramTemplateList[$paramId] = array(
				"fileName"  => $paramFileName,
				"arguments" => $arguments
			);
			
		}
	}
a7011f4d   Benjamin Renard   Generated list fo...
344
}