1
|
|
|
<?php |
2
|
|
|
namespace PHPMV\core; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* A template parser for js files. |
6
|
|
|
* |
7
|
|
|
* PHPMV\core$TemplateParser |
8
|
|
|
* This class is part of php-ui-common |
9
|
|
|
* |
10
|
|
|
* @author jc |
11
|
|
|
* @version 1.0.0 |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class TemplateParser { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The template content. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $templateContent; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The default template file extension. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private const FILE_EXT = '.tpl.js'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private const OPEN_TAG = '/*'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private const CLOSE_TAG = '*/'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The active template directory |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private static $templateDirectory; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Load a template file. |
51
|
|
|
* |
52
|
|
|
* @param string $filename |
53
|
|
|
*/ |
54
|
3 |
|
public function loadTemplatefile(string $filename): void { |
55
|
3 |
|
$completeFilename = self::$templateDirectory . $filename . self::FILE_EXT; |
56
|
3 |
|
$this->templateContent = \file_get_contents($completeFilename); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Parse a loaded template file with values (in an associative array). |
61
|
|
|
* Variables in template are defined with the OPEN_TAG, a key of the array and the CLOSE_TAG. |
62
|
|
|
* |
63
|
|
|
* @param array $values |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
2 |
|
public function parse(array $values): string { |
67
|
2 |
|
$result = $this->templateContent; |
68
|
2 |
|
foreach ($values as $k => $v) { |
69
|
2 |
|
$result = \str_replace(self::OPEN_TAG . $k . self::CLOSE_TAG, $v, $result); |
70
|
|
|
} |
71
|
2 |
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Load and parse a template file with values (in an associative array). |
76
|
|
|
* |
77
|
|
|
* @param string $filename |
78
|
|
|
* @param array $values |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public function loadAndParse(string $filename, array $values): string { |
82
|
1 |
|
$this->loadTemplatefile($filename); |
83
|
1 |
|
return $this->parse($values); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return the active template directory. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
1 |
|
public static function getTemplateDirectory(): string { |
92
|
1 |
|
return self::$templateDirectory; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Define the template base directory. |
97
|
|
|
* |
98
|
|
|
* @param string $templateDirectory |
99
|
|
|
*/ |
100
|
4 |
|
public static function setTemplateDirectory(string $templateDirectory): void { |
101
|
4 |
|
self::$templateDirectory = $templateDirectory; |
102
|
4 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
1 |
|
public function getTemplateContent() { |
109
|
1 |
|
return $this->templateContent; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|