1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\DefaultPhpRepository\Helper; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
5
|
|
|
use Yoanm\DefaultPhpRepository\Exception\TargetFileExistsException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class TemplateFileProcessor |
9
|
|
|
*/ |
10
|
|
|
class TemplateHelper |
11
|
|
|
{ |
12
|
|
|
/** @var array */ |
13
|
|
|
private $variableList = []; |
14
|
|
|
/** @var string[] */ |
15
|
|
|
private $variableNameList = []; |
16
|
|
|
/** @var string */ |
17
|
|
|
private $filenameResolverRegexp; |
18
|
|
|
/** @var Filesystem */ |
19
|
|
|
private $fs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $variableList |
23
|
|
|
* @param array $extraTemplatePath |
24
|
|
|
*/ |
25
|
|
|
public function __construct(array $variableList, array $extraTemplatePath = []) |
26
|
|
|
{ |
27
|
|
|
$this->variableList = $variableList; |
28
|
|
|
foreach($this->variableList as $variableId => $variableValue) { |
29
|
|
|
$variableId = sprintf('%%%s%%', $variableId); |
30
|
|
|
$this->variableNameList[$variableId] = $variableId; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
foreach ($extraTemplatePath as $extraKey => $extraValue) { |
34
|
|
|
$variableId = sprintf('%%%s%%', $extraKey); |
35
|
|
|
$this->variableNameList[$variableId] = $variableId; |
36
|
|
|
$this->variableList[$variableId] = $this->loadTemplate($extraValue); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// compile this regexp at startup (no need to to it each time) |
40
|
|
|
$this->filenameResolverRegexp = sprintf( |
41
|
|
|
'#%s?(?:[^%s]+%s)*([^%s]+)\.tmpl$#', |
42
|
|
|
PathHelper::separator(), |
43
|
|
|
PathHelper::separator(), |
44
|
|
|
PathHelper::separator(), |
45
|
|
|
PathHelper::separator() |
46
|
|
|
); |
47
|
|
|
// compile this regexp at startup (no need to to it each time) |
48
|
|
|
$this->filePathResolverRegexp = sprintf( |
|
|
|
|
49
|
|
|
'#%s?(?:[^%s]+%s)*[^%s]+\.tmpl$#', |
50
|
|
|
PathHelper::separator(), |
51
|
|
|
PathHelper::separator(), |
52
|
|
|
PathHelper::separator(), |
53
|
|
|
PathHelper::separator() |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->fs = new Filesystem(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $templateFilePath |
62
|
|
|
* @param string $outputFilePath |
63
|
|
|
* @param bool $overwrite overwrite even if present |
64
|
|
|
* |
65
|
|
|
* @throws TargetFileExistsException |
66
|
|
|
*/ |
67
|
|
|
public function dumpTemplate($templateFilePath, $outputFilePath, $overwrite = false) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->fs->dumpFile($outputFilePath, $this->loadTemplate($templateFilePath)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $templateFilePath |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function resolveOutputFilePath($templateFilePath, $outputDir) |
78
|
|
|
{ |
79
|
|
|
return sprintf( |
80
|
|
|
'%s%s', |
81
|
|
|
PathHelper::appendPathSeparator($outputDir), |
82
|
|
|
$this->resolveOutputFilename($templateFilePath) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $templateFilePath |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function resolveOutputFilename($templateFilePath) |
92
|
|
|
{ |
93
|
|
|
return preg_replace($this->filenameResolverRegexp, '\1', $templateFilePath); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $templateFilePath |
98
|
|
|
* |
99
|
|
|
* @return string file content |
100
|
|
|
*/ |
101
|
|
|
public function loadTemplate($templateFilePath) |
102
|
|
|
{ |
103
|
|
|
return str_replace($this->variableNameList, $this->variableList, file_get_contents($templateFilePath)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: