|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Glamorous\Boiler\Helpers; |
|
5
|
|
|
|
|
6
|
|
|
use Glamorous\Boiler\BoilerException; |
|
7
|
|
|
use SplFileInfo; |
|
8
|
|
|
use Symfony\Component\Finder\Finder; |
|
9
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
|
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
11
|
|
|
|
|
12
|
|
|
class Template |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Singleton pattern instance. |
|
16
|
|
|
* |
|
17
|
|
|
* @var self |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $instance = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Array with the paths. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $paths; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Template constructor. |
|
30
|
|
|
*/ |
|
31
|
|
|
private function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get singleton instance of the class. |
|
37
|
|
|
* |
|
38
|
|
|
* @return Template |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function getInstance() |
|
41
|
|
|
{ |
|
42
|
|
|
if (self::$instance === null) { |
|
43
|
|
|
self::$instance = new Template(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return self::$instance; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Method to get a valid template from a given template name. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $fileName |
|
53
|
|
|
* @param array $paths |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
* |
|
57
|
|
|
* @throws BoilerException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function searchTemplateByFilenameInGivenPaths(string $fileName, array $paths): array |
|
60
|
|
|
{ |
|
61
|
|
|
$this->paths = $paths; |
|
62
|
|
|
$template = $this->findAndParseTemplate($fileName); |
|
63
|
|
|
$extraTemplates = $this->getIncludedTemplates($template); |
|
64
|
|
|
$template = $this->mergeExtraTemplates($template, $extraTemplates); |
|
65
|
|
|
|
|
66
|
|
|
$this->validateTemplate($template); |
|
67
|
|
|
|
|
68
|
|
|
return $template; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Find and parse template file. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $templateFileName |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
* |
|
78
|
|
|
* @throws BoilerException |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function findAndParseTemplate(string $templateFileName): array |
|
81
|
|
|
{ |
|
82
|
|
|
$templateFile = $this->getTemplate($templateFileName); |
|
83
|
|
|
|
|
84
|
|
|
$pathName = $templateFile->getPathname(); |
|
85
|
|
|
$template = $this->parseTemplateFile($pathName); |
|
86
|
|
|
|
|
87
|
|
|
if (!is_array($template)) { |
|
88
|
|
|
throw new BoilerException('Template could not be parsed as a yaml file'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $template; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get the template. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $name |
|
98
|
|
|
* |
|
99
|
|
|
* @return SplFileInfo |
|
100
|
|
|
* |
|
101
|
|
|
* @throws BoilerException |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getTemplate(string $name): ?SplFileInfo |
|
104
|
|
|
{ |
|
105
|
|
|
foreach ($this->paths as $directory) { |
|
106
|
|
|
$fileInDirectory = $this->findYamlFileInDirectory($name, $directory . '/' . $name); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
if (!is_null($fileInDirectory)) { |
|
109
|
|
|
return $fileInDirectory; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$file = $this->findYamlFileInDirectory($name, $directory); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
if (!is_null($file)) { |
|
115
|
|
|
return $file; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
throw new BoilerException('Boiler-file with name `' . $name . '` does not exists.'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Parse and load yml file from path. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $path |
|
126
|
|
|
* |
|
127
|
|
|
* @return mixed |
|
128
|
|
|
* |
|
129
|
|
|
* @throws BoilerException |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function parseTemplateFile(string $path) |
|
132
|
|
|
{ |
|
133
|
|
|
try { |
|
134
|
|
|
return Yaml::parseFile($path); |
|
135
|
|
|
} catch (ParseException $exception) { |
|
136
|
|
|
throw new BoilerException( |
|
137
|
|
|
'Template file located at `' . $path . '` could not be parsed', |
|
138
|
|
|
$exception->getCode(), |
|
139
|
|
|
$exception |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Find a yaml file with correct name in a specified directory. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $name |
|
148
|
|
|
* @param string $directory |
|
149
|
|
|
* |
|
150
|
|
|
* @return null|SplFileInfo |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function findYamlFileInDirectory(string $name, string $directory) |
|
153
|
|
|
{ |
|
154
|
|
|
if (!is_dir($directory)) { |
|
155
|
|
|
return null; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$name .= '.yml'; |
|
159
|
|
|
$finder = new Finder(); |
|
160
|
|
|
$finder->files()->name($name)->depth('== 0')->in($directory); |
|
161
|
|
|
|
|
162
|
|
|
foreach ($finder as $file) { |
|
163
|
|
|
if ($file->getFilename() === $name) { |
|
164
|
|
|
return $file; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return null; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Load included templates. |
|
173
|
|
|
* |
|
174
|
|
|
* @param array $template |
|
175
|
|
|
* |
|
176
|
|
|
* @return array |
|
177
|
|
|
* @throws BoilerException |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function getIncludedTemplates(array $template): array |
|
180
|
|
|
{ |
|
181
|
|
|
if (!array_key_exists('include', $template)) { |
|
182
|
|
|
return []; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (!is_array($template['include'])) { |
|
186
|
|
|
throw new BoilerException('Include function must be an array.'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return array_map(function ($fileName) { |
|
190
|
|
|
$templateFile = $this->getTemplate($fileName); |
|
191
|
|
|
$template = $this->parseTemplateFile($templateFile->getPathname()); |
|
192
|
|
|
|
|
193
|
|
|
if (!$template) { |
|
194
|
|
|
throw new BoilerException('Included file `' . $fileName . '` cannot be parsed'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $template; |
|
198
|
|
|
}, $template['include']); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Merge the extra template (if there are) with the main template. |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $template |
|
205
|
|
|
* @param array $extraTemplates |
|
206
|
|
|
* |
|
207
|
|
|
* @return array |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function mergeExtraTemplates(array $template, array $extraTemplates): array |
|
210
|
|
|
{ |
|
211
|
|
|
foreach ($extraTemplates as $extraTemplate) { |
|
212
|
|
|
$template = array_merge($template, $extraTemplate); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return $template; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Validate the loaded template-file. |
|
220
|
|
|
* |
|
221
|
|
|
* @param array $template |
|
222
|
|
|
* |
|
223
|
|
|
* @throws BoilerException |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function validateTemplate(array $template): void |
|
226
|
|
|
{ |
|
227
|
|
|
if (!array_key_exists('name', $template) || empty($template['name'])) { |
|
228
|
|
|
throw new BoilerException('There\'s no name defined in the template'); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
if (!array_key_exists('steps', $template) || !is_array($template['steps']) || empty($template['steps'])) { |
|
232
|
|
|
throw new BoilerException('There are no steps defined in the template'); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$this->validateSteps($template); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Internal method to validate the steps. |
|
240
|
|
|
* |
|
241
|
|
|
* @param array $template |
|
242
|
|
|
* |
|
243
|
|
|
* @throws BoilerException |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function validateSteps(array $template): void |
|
246
|
|
|
{ |
|
247
|
|
|
$steps = $template['steps']; |
|
248
|
|
|
foreach ($steps as $step) { |
|
249
|
|
|
if (!array_key_exists($step, $template)) { |
|
250
|
|
|
throw new BoilerException('The step `' . $step . '` does not exists.'); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if (!array_key_exists('name', $template[$step])) { |
|
254
|
|
|
throw new BoilerException('No `name` set for the step `' . $step . '`'); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
if (!array_key_exists('script', $template[$step])) { |
|
258
|
|
|
throw new BoilerException('No `script` set for the step `' . $step . '`'); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.