Skip to content

Commit c2874bd

Browse files
authored
Merge pull request #56 from akrabat/php7
Update for PHP 7.1+
2 parents 546767b + 7c25fe6 commit c2874bd

File tree

8 files changed

+164
-198
lines changed

8 files changed

+164
-198
lines changed

.travis.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ dist: trusty
55
matrix:
66
include:
77
- php: 7.1
8-
env: ANALYSIS='true'
98
- php: 7.2
109
- php: 7.3
10+
env: ANALYSIS='true'
1111
- php: nightly
1212
allow_failures:
1313
- php: nightly
1414

15-
before_script: composer install
15+
before_script:
16+
- if [[ "$ANALYSIS" == 'true' ]]; then composer require php-coveralls/php-coveralls:^2.1.0 ; fi
17+
- composer install -n
1618

17-
script: vendor/bin/phpunit --coverage-text --configuration phpunit.xml.dist
19+
script:
20+
- if [[ "$ANALYSIS" != 'true' ]]; then vendor/bin/phpunit ; fi
21+
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpunit --coverage-clover clover.xml ; fi
22+
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpcs ; fi

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## PHP Renderer
44

5-
This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 3.
5+
This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 4.
66

77

88
### Cross-site scripting (XSS) risks
@@ -17,28 +17,29 @@ Install with [Composer](http://getcomposer.org):
1717
composer require slim/php-view
1818

1919

20-
## Usage with Slim 3
20+
## Usage with Slim 4
2121

2222
```php
2323
use Slim\Views\PhpRenderer;
2424

2525
include "vendor/autoload.php";
2626

27-
$app = new Slim\App();
28-
$container = $app->getContainer();
29-
$container['renderer'] = new PhpRenderer("./templates");
27+
$app = Slim\AppFactor::creates();
3028

3129
$app->get('/hello/{name}', function ($request, $response, $args) {
32-
return $this->renderer->render($response, "hello.php", $args);
30+
$renderer = new PhpRenderer('path/to/templates');
31+
return $renderer->render($response, "hello.php", $args);
3332
});
3433

3534
$app->run();
3635
```
3736

37+
Note that you could place the PhpRenderer instantiation within your DI Container.
38+
3839
## Usage with any PSR-7 Project
3940
```php
4041
//Construct the View
41-
$phpView = new PhpRenderer("./path/to/templates");
42+
$phpView = new PhpRenderer("path/to/templates");
4243

4344
//Render a Template
4445
$response = $phpView->render(new Response(), "hello.php", $yourData);
@@ -52,7 +53,7 @@ You can now add variables to your renderer that will be available to all templat
5253
$templateVariables = [
5354
"title" => "Title"
5455
];
55-
$phpView = new PhpRenderer("./path/to/templates", $templateVariables);
56+
$phpView = new PhpRenderer("path/to/templates", $templateVariables);
5657

5758
// or setter
5859
$phpView->setAttributes($templateVariables);
@@ -66,7 +67,7 @@ Data passed in via `->render()` takes precedence over attributes.
6667
$templateVariables = [
6768
"title" => "Title"
6869
];
69-
$phpView = new PhpRenderer("./path/to/templates", $templateVariables);
70+
$phpView = new PhpRenderer("path/to/templates", $templateVariables);
7071

7172
//...
7273

@@ -83,19 +84,19 @@ Inside your templates you may use `$this` to refer to the PhpRenderer object to
8384
You can now render view in another views called layouts, this allows you to compose modular view templates
8485
and help keep your views DRY.
8586

86-
Create your layout `./path/to/templates/layout.php`.
87+
Create your layout `path/to/templates/layout.php`.
8788
```phtml
8889
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>
8990
```
9091

91-
Create your view template `./path/to/templates/hello.php`.
92+
Create your view template `path/to/templates/hello.php`.
9293
```phtml
9394
Hello <?=$name?>!
9495
```
9596

9697
Rendering in your code.
9798
```php
98-
$phpView = new PhpRenderer("./path/to/templates", ["title" => "My App"]);
99+
$phpView = new PhpRenderer("path/to/templates", ["title" => "My App"]);
99100
$phpView->setLayout("layout.php");
100101

101102
//...

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"require-dev": {
2323
"phpunit/phpunit": "^7.0",
24-
"slim/psr7" : "^0.6"
24+
"slim/psr7" : "^0.6",
25+
"squizlabs/php_codesniffer": "^3.5"
2526
}
2627
}

phpcs.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Slim coding standard">
3+
<description>Slim coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<!-- use colors in output -->
8+
<arg name="colors"/>
9+
10+
<!-- inherit rules from: -->
11+
<rule ref="PSR12"/>
12+
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
13+
14+
<!-- Paths to check -->
15+
<file>src</file>
16+
<file>tests</file>
17+
</ruleset>

src/PhpRenderer.php

Lines changed: 41 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* Slim Framework (http://slimframework.com)
47
*
@@ -8,8 +11,10 @@
811
*/
912
namespace Slim\Views;
1013

11-
use \InvalidArgumentException;
14+
use InvalidArgumentException;
1215
use 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

Comments
 (0)