1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\honam; |
4
|
|
|
|
5
|
|
|
use ntentan\honam\exceptions\TemplateResolutionException; |
6
|
|
|
|
7
|
|
|
class TemplateFileResolver |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The array which holds the template path heirachy. |
11
|
|
|
* |
12
|
|
|
* @var array<string> |
13
|
|
|
*/ |
14
|
|
|
private $pathHierarchy = array(); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Append a directory to the end of the template path heirachy. |
18
|
|
|
* |
19
|
|
|
* @param string $path |
20
|
|
|
*/ |
21
|
|
|
public function appendToPathHierarchy($path) |
22
|
|
|
{ |
23
|
|
|
$this->pathHierarchy[] = $path; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prepend a directory to the beginning of the template path heirachy. |
28
|
|
|
* |
29
|
|
|
* @param string $path |
30
|
|
|
*/ |
31
|
|
|
public function prependToPathHierarchy($path) |
32
|
|
|
{ |
33
|
|
|
array_unshift($this->pathHierarchy, $path); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function testTemplateFile($testTemplate, $paths, $extension) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$templateFile = ''; |
39
|
|
|
foreach ($paths as $path) { |
40
|
|
|
$newTemplateFile = "$path/$testTemplate.$extension"; |
41
|
|
|
if (file_exists($newTemplateFile)) { |
42
|
|
|
$templateFile = $newTemplateFile; |
43
|
|
|
break; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
return $templateFile; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function testNoEngineTemplateFile($testTemplate, $paths) |
50
|
|
|
{ |
51
|
|
|
$templateFile = ''; |
52
|
|
|
foreach ($paths as $path) { |
53
|
|
|
$newTemplateFile = "$testTemplate.*"; |
54
|
|
|
$files = array_filter( |
55
|
|
|
scandir($path), |
56
|
|
|
function($file) use($newTemplateFile) { |
57
|
|
|
return fnmatch($newTemplateFile, $file); |
58
|
|
|
}); |
59
|
|
|
if (count($files) == 1) { |
60
|
|
|
$templateFile = $path . "/" . reset($files); |
61
|
|
|
break; |
62
|
|
|
} else if (count($files) > 1) { |
63
|
|
|
$templates = implode(", ", $files); |
64
|
|
|
throw new TemplateResolutionException("Multiple templates were resolved for the request '$testTemplate'. Please ensure that only one supported template type of the name '$testTemplate' exists in the path '$path'. Files found: $templates"); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return $templateFile; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function searchTemplateDirectory($template, $ignoreEngine = false) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$templateFile = ''; |
73
|
|
|
|
74
|
|
|
// Split the filename on the dots. The first part before the first dot |
75
|
|
|
// would be used to implement the file breakdown. The other parts are |
76
|
|
|
// fused together again and appended during the evaluation of the |
77
|
|
|
// breakdown. |
78
|
|
|
|
79
|
|
|
if ($ignoreEngine) { |
80
|
|
|
$breakDown = explode('_', $template); |
81
|
|
|
} else { |
82
|
|
|
$splitOnDots = explode('.', $template); |
83
|
|
|
$breakDown = explode('_', array_shift($splitOnDots)); |
84
|
|
|
$extension = implode(".", $splitOnDots); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
for ($i = 0; $i < count($breakDown); $i++) { |
|
|
|
|
88
|
|
|
$testTemplate = implode("_", array_slice($breakDown, $i, count($breakDown) - $i)); |
89
|
|
|
|
90
|
|
|
if ($ignoreEngine) { |
91
|
|
|
$templateFile = $this->testNoEngineTemplateFile($testTemplate, $this->pathHierarchy); |
92
|
|
|
} else { |
93
|
|
|
$templateFile = $this->testTemplateFile($testTemplate, $this->pathHierarchy, $extension); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($templateFile != '') { |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $templateFile; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Resolve a template file by running through all the directories in the |
106
|
|
|
* template heirachy till a file that matches the template is found. |
107
|
|
|
* |
108
|
|
|
* @param string $template |
109
|
|
|
* @return string |
110
|
|
|
* @throws \ntentan\honam\exceptions\FileNotFoundException |
111
|
|
|
*/ |
112
|
|
|
public function resolveTemplateFile($template) |
113
|
|
|
{ |
114
|
|
|
if ($template == '') { |
115
|
|
|
throw new TemplateResolutionException("Empty template file requested"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$templateFile = $this->searchTemplateDirectory($template, pathinfo($template, PATHINFO_EXTENSION) === ''); |
119
|
|
|
|
120
|
|
|
if ($templateFile == null) { |
121
|
|
|
$pathString = "[" . implode('; ', $this->pathHierarchy) . "]"; |
122
|
|
|
throw new TemplateResolutionException( |
123
|
|
|
"Could not find a suitable template file for the current request '{$template}'. Current template path $pathString" |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $templateFile; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.