1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
6
|
|
|
* @package MW |
7
|
|
|
* @subpackage View |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\MW\View; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Allow more than one view engine at once |
16
|
|
|
* |
17
|
|
|
* @package MW |
18
|
|
|
* @subpackage View |
19
|
|
|
*/ |
20
|
|
|
class Compose implements \Aimeos\MW\View\Iface |
21
|
|
|
{ |
22
|
|
|
private $engines; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Initializes the logger object. |
27
|
|
|
* |
28
|
|
|
* @param array $engines Associative list of file name extensions as keys and view objects as values |
29
|
|
|
*/ |
30
|
|
|
public function __construct( array $engines ) |
31
|
|
|
{ |
32
|
|
|
\Aimeos\MW\Common\Base::checkClassList( '\Aimeos\MW\View\Iface', $engines ); |
33
|
|
|
|
34
|
|
|
$this->engines = $engines; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Calls the view helper with the given name and arguments and returns it's output. |
40
|
|
|
* |
41
|
|
|
* @param string $name Name of the view helper |
42
|
|
|
* @param array $args Arguments passed to the view helper |
43
|
|
|
* @return mixed Output depending on the view helper |
44
|
|
|
*/ |
45
|
|
|
public function __call( $name, array $args ) |
46
|
|
|
{ |
47
|
|
|
foreach( $this->engines as $engine ) |
48
|
|
|
{ |
49
|
|
|
if( ( $result = call_user_func_array( array( $engine, $name ), $args ) ) !== null ) { |
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the value associated to the given key. |
58
|
|
|
* |
59
|
|
|
* @param string $key Name of the value that should be returned |
60
|
|
|
* @return mixed Value associated to the given key |
61
|
|
|
* @throws \Aimeos\MW\View\Exception If the requested key isn't available |
62
|
|
|
*/ |
63
|
|
|
public function __get( $key ) |
64
|
|
|
{ |
65
|
|
|
foreach( $this->engines as $engine ) |
66
|
|
|
{ |
67
|
|
|
if( isset( $engine->$key ) ) { |
68
|
|
|
return $engine->$key; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
throw new \Aimeos\MW\View\Exception( sprintf( 'No value for key "%1$s" found', $key ) ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Tests if a key with the given name exists. |
78
|
|
|
* |
79
|
|
|
* @param string $key Name of the value that should be tested |
80
|
|
|
* @return boolean True if the key exists, false if not |
81
|
|
|
*/ |
82
|
|
|
public function __isset( $key ) |
83
|
|
|
{ |
84
|
|
|
foreach( $this->engines as $engine ) |
85
|
|
|
{ |
86
|
|
|
if( isset( $engine->$key ) ) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Removes a key from the stored values. |
97
|
|
|
* |
98
|
|
|
* @param string $key Name of the value that should be removed |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function __unset( $key ) |
102
|
|
|
{ |
103
|
|
|
foreach( $this->engines as $engine ) { |
104
|
|
|
unset( $engine->$key ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets a new value for the given key. |
111
|
|
|
* |
112
|
|
|
* @param string $key Name of the value that should be set |
113
|
|
|
* @param mixed $value Value associated to the given key |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function __set( $key, $value ) |
117
|
|
|
{ |
118
|
|
|
foreach( $this->engines as $engine ) { |
119
|
|
|
$engine->$key = $value; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Adds a view helper instance to the view. |
126
|
|
|
* |
127
|
|
|
* @param string $name Name of the view helper as called in the template |
128
|
|
|
* @param \Aimeos\MW\View\Helper\Iface $helper View helper instance |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function addHelper( $name, \Aimeos\MW\View\Helper\Iface $helper ) |
132
|
|
|
{ |
133
|
|
|
foreach( $this->engines as $engine ) { |
134
|
|
|
$engine->addHelper( $name, $helper ); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Assigns a whole set of values at once to the view. |
141
|
|
|
* This method overwrites already existing key/value pairs set by the magic method. |
142
|
|
|
* |
143
|
|
|
* @param array $values Associative list of key/value pairs |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function assign( array $values ) |
147
|
|
|
{ |
148
|
|
|
foreach( $this->engines as $engine ) { |
149
|
|
|
$engine->assign( $values ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns the value associated to the given key or the default value if the key is not available. |
156
|
|
|
* |
157
|
|
|
* @param string $key Name of the value that should be returned |
158
|
|
|
* @param mixed $default Default value returned if ths key is not available |
159
|
|
|
* @return mixed Value associated to the given key or the default value |
160
|
|
|
*/ |
161
|
|
|
public function get( $key, $default = null ) |
162
|
|
|
{ |
163
|
|
|
foreach( $this->engines as $engine ) |
164
|
|
|
{ |
165
|
|
|
if( ( $value = $engine->get( $key ) ) !== null ) { |
166
|
|
|
return $value; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $default; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Renders the output based on the given template file name and the key/value pairs. |
176
|
|
|
* |
177
|
|
|
* @param string $filename File name of the view template |
178
|
|
|
* @return string Output generated by the template |
179
|
|
|
* @throws \Aimeos\MW\View\Exception If the template isn't found |
180
|
|
|
*/ |
181
|
|
|
public function render( $filename ) |
182
|
|
|
{ |
183
|
|
|
return $this->getEngine( $filename )->render( $filename ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the view engine suitable for the given file name |
189
|
|
|
* |
190
|
|
|
* @param string $filename Name of the template file including the file extension |
191
|
|
|
* @return \Aimeos\MW\View\Iface View engine |
192
|
|
|
* @throws \Aimeos\MW\View\Exception If no registered view engine for this file extension is found |
193
|
|
|
*/ |
194
|
|
|
protected function getEngine( $filename ) |
195
|
|
|
{ |
196
|
|
|
foreach( (array) $filename as $file ) |
197
|
|
|
{ |
198
|
|
|
foreach( $this->engines as $fileext => $engine ) |
199
|
|
|
{ |
200
|
|
|
if( substr_compare( $file, $fileext, -strlen( $fileext ) ) === 0 ) { |
201
|
|
|
return $engine; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
throw new \Aimeos\MW\View\Exception( sprintf( 'No view engine for "%1$s" found', $filename ) ); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|