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
|
|
|
if (is_null($templateFile)) { |
85
|
|
|
throw new BoilerException('No template found with name ' . $templateFileName); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$pathName = $templateFile->getPathname(); |
89
|
|
|
$template = $this->parseTemplateFile($pathName); |
90
|
|
|
|
91
|
|
|
if (!is_array($template)) { |
92
|
|
|
throw new BoilerException('Template could not be parsed as a yaml file'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $template; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the template. |
100
|
|
|
* |
101
|
|
|
* @param string $name |
102
|
|
|
* |
103
|
|
|
* @return null|SplFileInfo |
104
|
|
|
*/ |
105
|
|
|
protected function getTemplate(string $name): ?SplFileInfo |
106
|
|
|
{ |
107
|
|
|
foreach ($this->paths as $directory) { |
108
|
|
|
$fileInDirectory = $this->findYamlFileInDirectory($name, $directory . '/' . $name); |
|
|
|
|
109
|
|
|
|
110
|
|
|
if (!is_null($fileInDirectory)) { |
111
|
|
|
return $fileInDirectory; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$file = $this->findYamlFileInDirectory($name, $directory); |
|
|
|
|
115
|
|
|
|
116
|
|
|
if (!is_null($file)) { |
117
|
|
|
return $file; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Parse and load yml file from path. |
126
|
|
|
* |
127
|
|
|
* @param string $path |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
* |
131
|
|
|
* @throws BoilerException |
132
|
|
|
*/ |
133
|
|
|
protected function parseTemplateFile(string $path) |
134
|
|
|
{ |
135
|
|
|
try { |
136
|
|
|
return Yaml::parseFile($path); |
137
|
|
|
} catch (ParseException $exception) { |
138
|
|
|
throw new BoilerException( |
139
|
|
|
'Template file located at `' . $path . '` could not be parsed', |
140
|
|
|
$exception->getCode(), |
141
|
|
|
$exception |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Find a yaml file with correct name in a specified directory. |
148
|
|
|
* |
149
|
|
|
* @param string $name |
150
|
|
|
* @param string $directory |
151
|
|
|
* |
152
|
|
|
* @return null|SplFileInfo |
153
|
|
|
*/ |
154
|
|
|
protected function findYamlFileInDirectory(string $name, string $directory) |
155
|
|
|
{ |
156
|
|
|
if (!is_dir($directory)) { |
157
|
|
|
return null; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$name .= '.yml'; |
161
|
|
|
$finder = new Finder(); |
162
|
|
|
$finder->files()->name($name)->depth('== 0')->in($directory); |
163
|
|
|
|
164
|
|
|
foreach ($finder as $file) { |
165
|
|
|
if ($file->getFilename() === $name) { |
166
|
|
|
return $file; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Load included templates. |
175
|
|
|
* |
176
|
|
|
* @param array $template |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
* @throws BoilerException |
180
|
|
|
*/ |
181
|
|
|
protected function getIncludedTemplates(array $template): array |
182
|
|
|
{ |
183
|
|
|
if (!array_key_exists('include', $template)) { |
184
|
|
|
return []; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!is_array($template['include'])) { |
188
|
|
|
throw new BoilerException('Include function must be an array.'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return array_map(function ($fileName) { |
192
|
|
|
$templateFile = $this->getTemplate($fileName); |
193
|
|
|
if ($templateFile === null) { |
194
|
|
|
throw new BoilerException('Included file `' . $fileName . '` does not exists'); |
195
|
|
|
} |
196
|
|
|
$template = $this->parseTemplateFile($templateFile->getPathname()); |
197
|
|
|
|
198
|
|
|
if (! $template) { |
199
|
|
|
throw new BoilerException('Included file `' . $fileName . '` cannot be parsed'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $template; |
203
|
|
|
}, $template['include']); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Merge the extra template (if there are) with the main template. |
208
|
|
|
* |
209
|
|
|
* @param array $template |
210
|
|
|
* @param array $extraTemplates |
211
|
|
|
* |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
|
|
protected function mergeExtraTemplates(array $template, array $extraTemplates): array |
215
|
|
|
{ |
216
|
|
|
foreach ($extraTemplates as $extraTemplate) { |
217
|
|
|
$template = array_merge($template, $extraTemplate); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $template; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Validate the loaded template-file. |
225
|
|
|
* |
226
|
|
|
* @param array $template |
227
|
|
|
* |
228
|
|
|
* @throws BoilerException |
229
|
|
|
*/ |
230
|
|
|
protected function validateTemplate(array $template): void |
231
|
|
|
{ |
232
|
|
|
if (!array_key_exists('name', $template) || empty($template['name'])) { |
233
|
|
|
throw new BoilerException('There\'s no name defined in the template'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (!array_key_exists('steps', $template) || !is_array($template['steps']) || empty($template['steps'])) { |
237
|
|
|
throw new BoilerException('There are no steps defined in the template'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$this->validateSteps($template); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Internal method to validate the steps. |
245
|
|
|
* |
246
|
|
|
* @param array $template |
247
|
|
|
* |
248
|
|
|
* @throws BoilerException |
249
|
|
|
*/ |
250
|
|
|
protected function validateSteps(array $template): void |
251
|
|
|
{ |
252
|
|
|
$steps = $template['steps']; |
253
|
|
|
foreach ($steps as $step) { |
254
|
|
|
if (!array_key_exists($step, $template)) { |
255
|
|
|
throw new BoilerException('The step `' . $step . '` does not exists.'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
if (!array_key_exists('name', $template[$step])) { |
259
|
|
|
throw new BoilerException('No `name` set for the step `' . $step . '`'); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
if (!array_key_exists('script', $template[$step])) { |
263
|
|
|
throw new BoilerException('No `script` set for the step `' . $step . '`'); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
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.