1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mvc package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mvc; |
11
|
|
|
|
12
|
|
|
use Aura\Router\Route; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
use Slick\Http\Server\AbstractMiddleware; |
16
|
|
|
use Slick\Http\Server\MiddlewareInterface; |
17
|
|
|
use Slick\Http\Stream; |
18
|
|
|
use Slick\Mvc\Renderer\HtmlExtension; |
19
|
|
|
use Slick\Template\Template; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Request Renderer |
23
|
|
|
* |
24
|
|
|
* @package Slick\Mvc |
25
|
|
|
* @author Filipe Silva <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Renderer extends AbstractMiddleware implements MiddlewareInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ServerRequestInterface |
32
|
|
|
*/ |
33
|
|
|
protected $request; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Handles a Request and updated the response |
37
|
|
|
* |
38
|
|
|
* @param ServerRequestInterface $request |
39
|
|
|
* @param ResponseInterface $response |
40
|
|
|
* |
41
|
|
|
* @return ResponseInterface |
42
|
|
|
*/ |
43
|
|
|
public function handle( |
44
|
|
|
ServerRequestInterface $request, ResponseInterface $response |
45
|
|
|
) |
46
|
|
|
{ |
47
|
|
|
$this->request = $request; |
48
|
|
|
$render = $this->request->getAttribute('render', true); |
49
|
|
|
if ($render) { |
50
|
|
|
$response = $this->render($response); |
51
|
|
|
} |
52
|
|
|
return $this->executeNext($request, $response); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Adds a template path |
57
|
|
|
* |
58
|
|
|
* @param $path |
59
|
|
|
*/ |
60
|
|
|
public function addTemplatePath($path) |
61
|
|
|
{ |
62
|
|
|
Template::addPath($path); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Processes and renders the response |
67
|
|
|
* |
68
|
|
|
* @param ResponseInterface $response |
69
|
|
|
* @return ResponseInterface|static |
70
|
|
|
*/ |
71
|
|
|
protected function render(ResponseInterface $response) |
72
|
|
|
{ |
73
|
|
|
$stream = new Stream('php://memory', 'rw+'); |
74
|
|
|
$content = $this->getEngine() |
75
|
|
|
->parse($this->getTemplate()) |
76
|
|
|
->process($this->getData()) |
77
|
|
|
; |
78
|
|
|
$stream->write($content); |
79
|
|
|
|
80
|
|
|
$response = $response->withBody($stream); |
81
|
|
|
return $response; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates the engine for this rendering |
86
|
|
|
* |
87
|
|
|
* @return \Slick\Template\TemplateEngineInterface |
88
|
|
|
*/ |
89
|
|
|
protected function getEngine() |
90
|
|
|
{ |
91
|
|
|
Template::addPath(dirname(__DIR__).'/templates'); |
92
|
|
|
$template = new Template(); |
93
|
|
|
$template->addExtension(HtmlExtension::class); |
94
|
|
|
return $template->initialize(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets the template file for current request |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function getTemplate() |
103
|
|
|
{ |
104
|
|
|
$template = $this->request |
105
|
|
|
->getAttribute('template', $this->getDefaultTemplate()); |
106
|
|
|
return $template.'.twig'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Gets data to be processed |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function getData() |
115
|
|
|
{ |
116
|
|
|
$index = ControllerInterface::REQUEST_ATTR_VIEW_DATA; |
117
|
|
|
return $this->request->getAttribute($index, []); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the default template from current request route |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
protected function getDefaultTemplate() |
126
|
|
|
{ |
127
|
|
|
/** @var Route $route */ |
128
|
|
|
$route = $this->request->getAttribute('route'); |
129
|
|
|
$values = $route->attributes; |
130
|
|
|
return "{$values['controller']}/{$values['action']}"; |
131
|
|
|
} |
132
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.