Completed
Push — master ( 4e1f37...568ba2 )
by Filipe
02:24 queued 15s
created

Renderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 2
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\Template\Template;
19
20
/**
21
 * Request Renderer
22
 *
23
 * @package Slick\Mvc
24
 * @author  Filipe Silva <[email protected]>
25
 */
26
class Renderer extends AbstractMiddleware implements MiddlewareInterface
27
{
28
29
    /**
30
     * @var ServerRequestInterface
31
     */
32
    protected $request;
33
34
    /**
35
     * Handles a Request and updated the response
36
     *
37
     * @param ServerRequestInterface $request
38
     * @param ResponseInterface $response
39
     *
40
     * @return ResponseInterface
41
     */
42
    public function handle(
43
        ServerRequestInterface $request, ResponseInterface $response
44
    )
45
    {
46
        $this->request = $request;
47
        $render = $this->request->getAttribute('render', true);
48
        if ($render) {
49
            $response = $this->render($response);
50
        }
51
        return $this->executeNext($request, $response);
52
    }
53
54
    /**
55
     * Adds a template path
56
     *
57
     * @param $path
58
     */
59
    public function addTemplatePath($path)
60
    {
61
        Template::addPath($path);
62
    }
63
64
    /**
65
     * Processes and renders the response
66
     *
67
     * @param ResponseInterface $response
68
     * @return ResponseInterface|static
69
     */
70
    protected function render(ResponseInterface $response)
71
    {
72
        $stream = new Stream('php://memory', 'rw+');
73
        $this->getEngine()->parse($this->getTemplate());
74
        $content = $this->getEngine()->process($this->getData());
75
        $stream->write($content);
76
77
        $response = $response->withBody($stream);
78
        return $response;
79
    }
80
81
    /**
82
     * Creates the engine for this rendering
83
     *
84
     * @return \Slick\Template\TemplateEngineInterface
85
     */
86
    protected function getEngine()
87
    {
88
        $template = new Template();
89
        return $template->initialize();
90
    }
91
92
    /**
93
     * Gets the template file for current request
94
     * 
95
     * @return string
96
     */
97
    protected function getTemplate()
98
    {
99
        $template = $this->request
100
            ->getAttribute('template', $this->getDefaultTemplate());
101
        return $template.'.twig';
102
    }
103
104
    /**
105
     * Gets data to be processed
106
     *
107
     * @return array
108
     */
109
    protected function getData()
110
    {
111
        $index = ControllerInterface::REQUEST_ATTR_VIEW_DATA;
112
        return $this->request->getAttribute($index, []);
113
    }
114
115
    /**
116
     * Returns the default template from current request route
117
     * 
118
     * @return string
119
     */
120
    protected function getDefaultTemplate()
121
    {
122
        /** @var Route $route */
123
        $route = $this->request->getAttribute('route');
124
        $values = $route->attributes;
125
        return "{$values['controller']}/{$values['action']}";
126
    }
127
}