-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseController.php
133 lines (122 loc) · 3.73 KB
/
BaseController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Duktig\Core\Controller;
use Psr\Http\Message\ServerRequestInterface;
use Interop\Http\Factory\ResponseFactoryInterface;
use Duktig\Core\View\RendererInterface;
use Duktig\Core\Config\ConfigInterface;
abstract class BaseController
{
protected $request;
protected $response;
protected $renderer;
protected $queryParams;
protected $config;
protected $templateSuffix;
public function __construct(
ResponseFactoryInterface $responseFactory,
RendererInterface $renderer,
ConfigInterface $config
)
{
$this->response = $responseFactory->createResponse();
$this->renderer = $renderer;
$this->config= $config;
$this->templateSuffix = $this->config->getParam('view')['templateSuffix'];
$this->addResponseHeader('Content-Type', 'text/html');
}
/**
* Sets the request.
*
* @param ServerRequestInterface $request
* @return void
*/
public function setRequest(ServerRequestInterface $request) : void
{
$this->request = $request;
}
/**
* Parse the query params.
*
* @return void
*/
protected function parseQueryParams() : void
{
$queryParams = [];
$queryString = $this->request->getUri()->getQuery();
parse_str($queryString, $queryParams);
$this->queryParams = $queryParams;
}
/**
* Gets a query param.
*
* @param string $param
* @return string|NULL
*/
protected function getQueryParam(string $param) : ?string
{
if ($this->queryParams === null) {
$this->parseQueryParams();
}
return $this->queryParams[$param] ?? null;
}
/**
* Gets all the query params.
*
* @return array
*/
protected function getQueryParams() : array
{
if ($this->queryParams === null) {
$this->parseQueryParams();
}
return $this->queryParams;
}
/**
* Sets the response status.
*
* @param int $code
* @param string $reasonPhrase [optional]
*/
protected function setResponseStatus(int $code, string $reasonPhrase = '') : void
{
$this->response = $this->response->withStatus($code, $reasonPhrase);
}
/**
* Adds a response header.
*
* @param string $name
* @param string $value [optional]
*/
protected function addResponseHeader(string $name, string $value = '') : void
{
$this->response = $this->response->withAddedHeader($name, $value);
}
/**
* Writes to the response body.
*
* @param string $body
*/
protected function writeResponseBody(string $body) : void
{
$this->response->getBody()->write($body);
}
/**
* @param array $data Template data
* @param string|null $template [optional] If not provided, the conventional
* template naming is used, ie. default template for
* ControllerExample::someAction inside the template directory will be
* 'Example/someAction{{suffix}}'
* @return string The rendered html
*/
protected function render(array $data, string $template = null) : string
{
if (is_null($template)) {
$controllerMethodName = debug_backtrace()[1]['function'];
$fullyQualifiedClassName = debug_backtrace()[1]['class'];
$fullyQualifiedClassNameArr = explode('\\', $fullyQualifiedClassName);
$controllerClassName = $fullyQualifiedClassNameArr[count($fullyQualifiedClassNameArr)-1];
$template = $controllerClassName . '/' . $controllerMethodName . $this->templateSuffix;
}
return $this->renderer->render($template, $data);
}
}