1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slim Framework (http://slimframework.com). |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/DavidePastore/Markdown-View |
6
|
|
|
*/ |
7
|
|
|
namespace DavidePastore\Slim\Views; |
8
|
|
|
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class MarkdownRenderer. |
13
|
|
|
*/ |
14
|
|
|
class MarkdownRenderer |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $templatePath; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The Parsedown instance. |
23
|
|
|
* |
24
|
|
|
* @var \Parsedown |
25
|
|
|
*/ |
26
|
|
|
protected $parsedown; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* SlimRenderer constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $templatePath |
32
|
|
|
*/ |
33
|
4 |
|
public function __construct($templatePath = '') |
34
|
|
|
{ |
35
|
4 |
|
$this->templatePath = $templatePath; |
36
|
4 |
|
$this->parsedown = new \Parsedown(); |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Render a template. |
41
|
|
|
* |
42
|
|
|
* throws RuntimeException if $templatePath . $template does not exist |
43
|
|
|
* |
44
|
|
|
* @param ResponseInterface $response |
45
|
|
|
* @param string $template |
46
|
|
|
* @param array $data |
47
|
|
|
* |
48
|
|
|
* @return ResponseInterface |
49
|
|
|
* |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
* @throws \RuntimeException |
52
|
|
|
*/ |
53
|
2 |
|
public function render(ResponseInterface $response, $template, array $data = []) |
54
|
|
|
{ |
55
|
2 |
|
$output = $this->fetch($template, $data); |
56
|
|
|
|
57
|
1 |
|
$response->getBody()->write($output); |
58
|
|
|
|
59
|
1 |
|
return $response; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the template path. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
2 |
|
public function getTemplatePath() |
68
|
|
|
{ |
69
|
2 |
|
return $this->templatePath; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set the template path. |
74
|
|
|
* |
75
|
|
|
* @param string $templatePath |
76
|
|
|
*/ |
77
|
1 |
|
public function setTemplatePath($templatePath) |
78
|
|
|
{ |
79
|
1 |
|
$this->templatePath = $templatePath; |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Renders a template and returns the result as a string. |
84
|
|
|
* |
85
|
|
|
* throws RuntimeException if $templatePath . $template does not exist |
86
|
|
|
* |
87
|
|
|
* @param $template |
88
|
|
|
* @param array $data |
89
|
|
|
* |
90
|
|
|
* @return mixed |
91
|
|
|
* |
92
|
|
|
* @throws \InvalidArgumentException |
93
|
|
|
* @throws \RuntimeException |
94
|
|
|
*/ |
95
|
2 |
|
public function fetch($template, array $data = []) |
|
|
|
|
96
|
|
|
{ |
97
|
2 |
|
if (!is_file($this->templatePath.$template)) { |
98
|
1 |
|
throw new \RuntimeException("View cannot render `$template` because the template does not exist"); |
99
|
|
|
} |
100
|
1 |
|
$output = $this->parsedown->text(file_get_contents($this->templatePath.$template)); |
101
|
|
|
|
102
|
1 |
|
return $output; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.