1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helmut\Forms; |
4
|
|
|
|
5
|
|
|
class Renderer { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Array of engine definitions using |
9
|
|
|
* file extension as key. |
10
|
|
|
* |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $engines = [ |
14
|
|
|
'.mustache.php' => 'Helmut\Forms\Engines\Mustache', |
15
|
|
|
'.twig.php' => 'Helmut\Forms\Engines\Twig', |
16
|
|
|
'.blade.php' => 'Helmut\Forms\Engines\Blade', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Cache of running engines. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $running = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Cache of template files. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $templates = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add a new engine implementation. |
35
|
|
|
* |
36
|
|
|
* @param string $extension |
37
|
|
|
* @param string $class |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function addEngine($extension, $class) |
41
|
|
|
{ |
42
|
|
|
array_unshift($this->engines, [$extension => $class]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get an engine implementation. |
47
|
|
|
* |
48
|
|
|
* @param string $key |
49
|
|
|
* @return \Helmut\Forms\Engine |
50
|
|
|
*/ |
51
|
47 |
|
public function start($key) |
52
|
|
|
{ |
53
|
47 |
|
if ( ! isset($this->running[$key])) { |
54
|
47 |
|
$this->running[$key] = new $this->engines[$key]; |
55
|
|
|
} |
56
|
|
|
|
57
|
47 |
|
return $this->running[$key]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get engine file extensions. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
47 |
|
public function extensions() |
66
|
|
|
{ |
67
|
47 |
|
return array_keys($this->engines); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a template cache key. |
72
|
|
|
* |
73
|
|
|
* @param string $template |
74
|
|
|
* @param array $paths |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
47 |
|
public function key($template, $paths) |
78
|
|
|
{ |
79
|
47 |
|
return $template.'-'.md5(serialize($paths)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Render a template. |
84
|
|
|
* |
85
|
|
|
* @param string $template |
86
|
|
|
* @param array $properties |
87
|
|
|
* @param array $paths |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
47 |
|
public function render($template, $properties, $paths) |
91
|
|
|
{ |
92
|
47 |
|
if ($this->has($template, $paths)) { |
93
|
|
|
|
94
|
47 |
|
$template = $this->findTemplate($template, $paths); |
95
|
|
|
|
96
|
47 |
|
return $this->start($template['engine'])->render($template['path'], $properties); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Check if a template exists. |
102
|
|
|
* |
103
|
|
|
* @param string $template |
104
|
|
|
* @param array $paths |
105
|
|
|
* @return boolean |
106
|
|
|
*/ |
107
|
|
|
public function has($template, $paths) |
108
|
47 |
|
{ |
109
|
|
|
return ! is_null($this->findTemplate($template, $paths)); |
110
|
47 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Find template file with engine. |
114
|
|
|
* |
115
|
|
|
* @param string $template |
116
|
|
|
* @param array $paths |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function findTemplate($template, $paths) |
120
|
47 |
|
{ |
121
|
|
|
$key = $this->key($template, $paths); |
122
|
47 |
|
|
123
|
|
|
if ( ! isset($this->templates[$key])) { |
124
|
47 |
|
|
125
|
|
|
$this->templates[$key] = null; |
126
|
47 |
|
|
127
|
|
|
$extensions = $this->extensions(); |
128
|
47 |
|
foreach ($paths as $path) { |
129
|
47 |
|
foreach ($extensions as $extension) { |
130
|
47 |
|
if (file_exists($path.$template.$extension)) { |
131
|
47 |
|
return $this->templates[$key] = [ |
132
|
47 |
|
'engine' => $extension, |
133
|
47 |
|
'path' => $path.$template.$extension, |
134
|
47 |
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->templates[$key]; |
141
|
47 |
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|