1 | <?php |
||
13 | class ViewService implements Renderer |
||
14 | { |
||
15 | /** |
||
16 | * @var ViewFinder |
||
17 | */ |
||
18 | public $finder; |
||
19 | |||
20 | /** |
||
21 | * @var string the default type of view |
||
22 | * |
||
23 | * @see render() |
||
24 | */ |
||
25 | public $default_type = 'view'; |
||
26 | |||
27 | /** |
||
28 | * @var string[] a stack of variable references being captured to |
||
29 | * |
||
30 | * @see begin() |
||
31 | * @see end() |
||
32 | */ |
||
33 | protected $capture_stack = array(); |
||
34 | |||
35 | /** |
||
36 | * @var int a unique index to track use of the capture stack |
||
37 | * |
||
38 | * @see begin() |
||
39 | * @see end() |
||
40 | */ |
||
41 | private $capture_index = 0; |
||
42 | |||
43 | /** |
||
44 | * @var string[][] map where view-model class name => map where view type => view path |
||
45 | */ |
||
46 | private $path_cache = array(); |
||
47 | |||
48 | /** |
||
49 | * @var Closure[] map where view path => view Closure |
||
50 | */ |
||
51 | private $closure_cache = array(); |
||
52 | |||
53 | /** |
||
54 | * @param ViewFinder $finder |
||
55 | */ |
||
56 | 1 | public function __construct(ViewFinder $finder) |
|
60 | |||
61 | /** |
||
62 | * Locate and render a PHP template for the given view, directly to output - as |
||
63 | * opposed to {@see capture()} which will use output buffering to capture the |
||
64 | * content and return it as a string. |
||
65 | * |
||
66 | * The view will be made available to the template as <code>$view</code> and the |
||
67 | * calling context (<code>$this</code>) will be this ViewService. |
||
68 | * |
||
69 | * @param object $view the view-model to render |
||
70 | * @param string|null $type the type of view to render (optional) |
||
71 | * |
||
72 | * @return void |
||
73 | * |
||
74 | * @throws RuntimeException |
||
75 | * |
||
76 | * @see capture() |
||
77 | */ |
||
78 | 1 | public function render($view, $type = null) |
|
118 | |||
119 | /** |
||
120 | * Render and capture the output from a PHP template for the given view - as |
||
121 | * opposed to {@see render()} which will render directly to output. |
||
122 | * |
||
123 | * Use capture only when necessary, such as when capturing content from a |
||
124 | * rendered template to populate the body of an e-mail. |
||
125 | * |
||
126 | * @param object $view the view-model to render |
||
127 | * @param string|null $type the type of view to render (optional) |
||
128 | * |
||
129 | * @return string rendered content |
||
130 | * |
||
131 | * @see render() |
||
132 | */ |
||
133 | 1 | public function capture($view, $type = null) |
|
134 | { |
||
135 | 1 | ob_start(); |
|
136 | |||
137 | try { |
||
138 | 1 | $this->render($view, $type); |
|
139 | 1 | } catch (Exception $exception) { |
|
140 | // re-throwing below (PHP 5.3+) |
||
141 | } catch (Throwable $exception) { |
||
|
|||
142 | // re-throwing below (PHP 7.0+) |
||
143 | } |
||
144 | |||
145 | 1 | $output = ob_get_clean(); |
|
146 | |||
147 | 1 | if (isset($exception)) { |
|
148 | 1 | throw $exception; |
|
149 | } |
||
150 | |||
151 | 1 | return $output; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param string &$var target variable reference for captured content |
||
156 | * |
||
157 | * @return void |
||
158 | * |
||
159 | * @see end() |
||
160 | */ |
||
161 | 1 | public function begin(&$var) |
|
176 | |||
177 | /** |
||
178 | * @param string &$var target variable reference for captured content |
||
179 | * |
||
180 | * @return void |
||
181 | * |
||
182 | * @throws RuntimeException |
||
183 | * |
||
184 | * @see begin() |
||
185 | */ |
||
186 | 1 | public function end(&$var) |
|
204 | |||
205 | /** |
||
206 | * Internally render a template file (or delegate to a cached closure) |
||
207 | * |
||
208 | * @param string $_path_ absolute path to PHP template |
||
209 | * @param object $view the view-model to render |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | 1 | protected function renderFile($_path_, $view) |
|
227 | |||
228 | /** |
||
229 | * Internally render a template closure |
||
230 | * |
||
231 | * @param Closure $closure template closure |
||
232 | * @param object $view the view-model to render |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | 1 | protected function renderClosure($closure, $view) |
|
240 | |||
241 | /** |
||
242 | * Called internally, if a view could not be resolved |
||
243 | * |
||
244 | * @param object $view the view-model attempted to render |
||
245 | * @param string|null $type the type of view attempted to render (optional) |
||
246 | * |
||
247 | * @return void |
||
248 | * |
||
249 | * @see render() |
||
250 | */ |
||
251 | 1 | protected function onMissingView($view, $type) |
|
263 | } |
||
264 |