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
|
|
|
* @param parsedown $parseDown The instance to use to parse. |
33
|
|
|
*/ |
34
|
6 |
|
public function __construct($templatePath = '', $parseDown = null) |
|
|
|
|
35
|
|
|
{ |
36
|
6 |
|
$this->templatePath = $templatePath; |
37
|
6 |
|
$this->parsedown = new \Parsedown(); |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Render a template. |
42
|
|
|
* |
43
|
|
|
* throws RuntimeException if $templatePath . $template does not exist |
44
|
|
|
* |
45
|
|
|
* @param ResponseInterface $response |
46
|
|
|
* @param string $template |
47
|
|
|
* |
48
|
|
|
* @return ResponseInterface |
49
|
|
|
* |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
* @throws \RuntimeException |
52
|
|
|
*/ |
53
|
2 |
|
public function render(ResponseInterface $response, $template) |
54
|
|
|
{ |
55
|
2 |
|
$output = $this->fetch($template); |
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
|
|
|
* Get the parsedown. |
84
|
|
|
* |
85
|
|
|
* @return \Parsedown |
86
|
|
|
*/ |
87
|
2 |
|
public function getParsedown() |
88
|
|
|
{ |
89
|
2 |
|
return $this->parsedown; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the parsedown. |
94
|
|
|
* |
95
|
|
|
* @param \Parsedown |
96
|
|
|
*/ |
97
|
1 |
|
public function setParsedown($parsedown) |
98
|
|
|
{ |
99
|
1 |
|
$this->parsedown = $parsedown; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Renders a template and returns the result as a string. |
104
|
|
|
* |
105
|
|
|
* throws RuntimeException if $templatePath . $template does not exist |
106
|
|
|
* |
107
|
|
|
* @param $template |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
* |
111
|
|
|
* @throws \InvalidArgumentException |
112
|
|
|
* @throws \RuntimeException |
113
|
|
|
*/ |
114
|
2 |
|
public function fetch($template) |
115
|
|
|
{ |
116
|
2 |
|
if (!is_file($this->templatePath.$template)) { |
117
|
1 |
|
throw new \RuntimeException("View cannot render `$template` because the template does not exist"); |
118
|
|
|
} |
119
|
1 |
|
$output = $this->parsedown->text(file_get_contents($this->templatePath.$template)); |
120
|
|
|
|
121
|
1 |
|
return $output; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.