11<?php
2+
3+ declare (strict_types=1 );
4+
25/**
36 * Slim Framework (http://slimframework.com)
47 *
811 */
912namespace Slim \Views ;
1013
11- use \ InvalidArgumentException ;
14+ use InvalidArgumentException ;
1215use Psr \Http \Message \ResponseInterface ;
16+ use RuntimeException ;
17+ use Throwable ;
1318
1419/**
1520 * Class PhpRenderer
@@ -34,14 +39,7 @@ class PhpRenderer
3439 */
3540 protected $ layout ;
3641
37- /**
38- * SlimRenderer constructor.
39- *
40- * @param string $templatePath
41- * @param array $attributes
42- * @param string $layout
43- */
44- public function __construct ($ templatePath = "" , $ attributes = [], $ layout = "" )
42+ public function __construct (string $ templatePath = '' , array $ attributes = [], string $ layout = '' )
4543 {
4644 $ this ->templatePath = rtrim ($ templatePath , '/ \\' ) . '/ ' ;
4745 $ this ->attributes = $ attributes ;
@@ -51,20 +49,13 @@ public function __construct($templatePath = "", $attributes = [], $layout = "")
5149 /**
5250 * Render a template
5351 *
54- * $data cannot contain template as a key
55- *
56- * throws RuntimeException if $templatePath . $template does not exist
57- *
58- * @param ResponseInterface $response
59- * @param string $template
60- * @param array $data
61- *
62- * @return ResponseInterface
52+ * @note $data cannot contain template as a key
6353 *
64- * @throws \InvalidArgumentException
65- * @throws \RuntimeException
54+ * @throws InvalidArgumentException
55+ * @throws RuntimeException if $templatePath . $template does not exist
56+ * @throws Throwable
6657 */
67- public function render (ResponseInterface $ response , $ template , array $ data = [])
58+ public function render (ResponseInterface $ response , string $ template , array $ data = []): ResponseInterface
6859 {
6960 $ output = $ this ->fetch ($ template , $ data , true );
7061
@@ -75,46 +66,38 @@ public function render(ResponseInterface $response, $template, array $data = [])
7566
7667 /**
7768 * Get layout template
78- *
79- * @return string
8069 */
81- public function getLayout ()
70+ public function getLayout (): string
8271 {
8372 return $ this ->layout ;
8473 }
8574
8675 /**
8776 * Set layout template
88- *
89- * @param string $layout
9077 */
91- public function setLayout ($ layout )
78+ public function setLayout (string $ layout ): void
9279 {
93- if ($ layout === "" || $ layout === null ) {
80+ if ($ layout === '' || $ layout === null ) {
9481 $ this ->layout = null ;
9582 } else {
9683 $ layoutPath = $ this ->templatePath . $ layout ;
9784 if (!is_file ($ layoutPath )) {
98- throw new \ RuntimeException (" Layout template ` $ layout` does not exist" );
85+ throw new RuntimeException (' Layout template " ' . $ layout . ' " does not exist' );
9986 }
10087 $ this ->layout = $ layout ;
10188 }
10289 }
10390
10491 /**
10592 * Get the attributes for the renderer
106- *
107- * @return array
10893 */
109- public function getAttributes ()
94+ public function getAttributes (): array
11095 {
11196 return $ this ->attributes ;
11297 }
11398
11499 /**
115100 * Set the attributes for the renderer
116- *
117- * @param array $attributes
118101 */
119102 public function setAttributes (array $ attributes )
120103 {
@@ -123,21 +106,19 @@ public function setAttributes(array $attributes)
123106
124107 /**
125108 * Add an attribute
126- *
127- * @param $key
128- * @param $value
129109 */
130- public function addAttribute ($ key , $ value ) {
110+ public function addAttribute (string $ key , $ value ): void
111+ {
131112 $ this ->attributes [$ key ] = $ value ;
132113 }
133114
134115 /**
135116 * Retrieve an attribute
136117 *
137- * @param $key
138118 * @return mixed
139119 */
140- public function getAttribute ($ key ) {
120+ public function getAttribute (string $ key )
121+ {
141122 if (!isset ($ this ->attributes [$ key ])) {
142123 return false ;
143124 }
@@ -147,42 +128,31 @@ public function getAttribute($key) {
147128
148129 /**
149130 * Get the template path
150- *
151- * @return string
152131 */
153- public function getTemplatePath ()
132+ public function getTemplatePath (): string
154133 {
155134 return $ this ->templatePath ;
156135 }
157136
158137 /**
159138 * Set the template path
160- *
161- * @param string $templatePath
162139 */
163- public function setTemplatePath ($ templatePath )
140+ public function setTemplatePath (string $ templatePath ): void
164141 {
165142 $ this ->templatePath = rtrim ($ templatePath , '/ \\' ) . '/ ' ;
166143 }
167144
168145 /**
169146 * Renders a template and returns the result as a string
170147 *
171- * cannot contain template as a key
172- *
173- * throws RuntimeException if $templatePath . $template does not exist
174- *
175- * @param $template
176- * @param array $data
177- * @param bool $useLayout
178- *
179- * @return mixed
148+ * @note $data cannot contain template as a key
180149 *
181- * @throws \InvalidArgumentException
182- * @throws \RuntimeException
150+ * @throws InvalidArgumentException
151+ * @throws RuntimeException
152+ * @throws Throwable
183153 */
184- public function fetch ($ template , array $ data = [], $ useLayout = false ) {
185-
154+ public function fetch (string $ template , array $ data = [], bool $ useLayout = false ): string
155+ {
186156 $ output = $ this ->fetchTemplate ($ template , $ data );
187157
188158 if ($ this ->layout !== null && $ useLayout ) {
@@ -196,25 +166,20 @@ public function fetch($template, array $data = [], $useLayout = false) {
196166 /**
197167 * Renders a template and returns the result as a string
198168 *
199- * cannot contain template as a key
200- *
201- * throws RuntimeException if $templatePath . $template does not exist
169+ * @note $data cannot contain template as a key
202170 *
203- * @param $template
204- * @param array $data
205- *
206- * @return mixed
207- *
208- * @throws \InvalidArgumentException
209- * @throws \RuntimeException
171+ * @throws InvalidArgumentException
172+ * @throws RuntimeException
173+ * @throws Throwable
210174 */
211- public function fetchTemplate ($ template , array $ data = []) {
175+ public function fetchTemplate (string $ template , array $ data = []): string
176+ {
212177 if (isset ($ data ['template ' ])) {
213- throw new \ InvalidArgumentException (" Duplicate template key found " );
178+ throw new InvalidArgumentException (' Duplicate template key found ' );
214179 }
215180
216181 if (!is_file ($ this ->templatePath . $ template )) {
217- throw new \ RuntimeException (" View cannot render ` $ template` because the template does not exist" );
182+ throw new RuntimeException (' View cannot render " ' . $ template . ' " because the template does not exist' );
218183 }
219184
220185 $ data = array_merge ($ this ->attributes , $ data );
@@ -223,11 +188,7 @@ public function fetchTemplate($template, array $data = []) {
223188 ob_start ();
224189 $ this ->protectedIncludeScope ($ this ->templatePath . $ template , $ data );
225190 $ output = ob_get_clean ();
226-
227- } catch (\Throwable $ e ) { // PHP 7+
228- ob_end_clean ();
229- throw $ e ;
230- } catch (\Exception $ e ) { // PHP < 7
191+ } catch (Throwable $ e ) {
231192 ob_end_clean ();
232193 throw $ e ;
233194 }
@@ -236,10 +197,10 @@ public function fetchTemplate($template, array $data = []) {
236197 }
237198
238199 /**
239- * @param string $template
240- * @param array $data
200+ * Include template within a separate scope for extracted $data
241201 */
242- protected function protectedIncludeScope ($ template , array $ data ) {
202+ protected function protectedIncludeScope (string $ template , array $ data ): void
203+ {
243204 extract ($ data );
244205 include func_get_arg (0 );
245206 }
0 commit comments