1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie\Render; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\Accessor\AccessorTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* An engine collection. |
18
|
|
|
* |
19
|
|
|
* @property-read array $extensions The extensions supported by the engines. |
20
|
|
|
*/ |
21
|
|
|
class EngineCollection implements \ArrayAccess, \IteratorAggregate |
22
|
|
|
{ |
23
|
|
|
use AccessorTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $engines; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
protected function get_extensions() |
34
|
|
|
{ |
35
|
|
|
return array_keys($this->engines); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Engine[] |
40
|
|
|
*/ |
41
|
|
|
private $instances; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $engines |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $engines = []) |
47
|
|
|
{ |
48
|
|
|
foreach ($engines as $extension => $engine) |
49
|
|
|
{ |
50
|
|
|
$this[$extension] = $engine; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function offsetExists($extension) |
58
|
|
|
{ |
59
|
|
|
return isset($this->engines[$extension]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public function offsetGet($extension) |
66
|
|
|
{ |
67
|
|
|
if (!$this->offsetExists($extension)) |
68
|
|
|
{ |
69
|
|
|
throw new EngineNotDefined([ $extension, $this ]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (empty($this->instances[$extension])) |
73
|
|
|
{ |
74
|
|
|
$instance = $this->engines[$extension]; |
75
|
|
|
|
76
|
|
|
if (is_string($instance)) |
77
|
|
|
{ |
78
|
|
|
$instance = new $instance; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->instances[$extension] = $instance; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->instances[$extension]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function offsetSet($extension, $engine) |
91
|
|
|
{ |
92
|
|
|
$this->engines[$extension] = $engine; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
|
|
public function offsetUnset($extension) |
99
|
|
|
{ |
100
|
|
|
unset($this->engines[$extension]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
|
|
public function getIterator() |
107
|
|
|
{ |
108
|
|
|
return new \ArrayIterator($this->engines); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Resolves the engine to use from the specified pathname. |
113
|
|
|
* |
114
|
|
|
* @param $pathname |
115
|
|
|
* |
116
|
|
|
* @return Engine|bool An engine or `false` if none matches the extension. |
117
|
|
|
*/ |
118
|
|
|
public function resolve_engine($pathname) |
119
|
|
|
{ |
120
|
|
|
$extension = pathinfo($pathname, PATHINFO_EXTENSION); |
121
|
|
|
|
122
|
|
|
if (!$extension) |
123
|
|
|
{ |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$extension = "." . $extension; |
128
|
|
|
|
129
|
|
|
if (!isset($this[$extension])) |
130
|
|
|
{ |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this[$extension]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Renders a template with the specified variables. |
139
|
|
|
* |
140
|
|
|
* @param string $template_pathname The pathname of the template. |
141
|
|
|
* @param mixed $thisArg The subject of the rendering. |
142
|
|
|
* @param array $variables |
143
|
|
|
* @param array $options |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
* |
147
|
|
|
* @throws EngineNotAvailable when there is no engine available to render the template. |
148
|
|
|
*/ |
149
|
|
|
public function render($template_pathname, $thisArg, $variables, array $options = []) |
150
|
|
|
{ |
151
|
|
|
$engine = $this->resolve_engine($template_pathname); |
152
|
|
|
|
153
|
|
|
if (!$engine) |
154
|
|
|
{ |
155
|
|
|
throw new EngineNotAvailable("There is no engine available to render template $template_pathname."); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $engine->render($template_pathname, $thisArg, $variables, $options); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|