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\PhpEnvironment\Request; |
16
|
|
|
use Slick\Http\PhpEnvironment\Response; |
17
|
|
|
use Slick\Mvc\Controller\UrlUtils; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Controller |
21
|
|
|
* |
22
|
|
|
* @package Slick\Mvc |
23
|
|
|
* @author Filipe Silva <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class Controller implements ControllerInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ServerRequestInterface|Request |
30
|
|
|
*/ |
31
|
|
|
protected $request; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ResponseInterface|Response |
35
|
|
|
*/ |
36
|
|
|
protected $response; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* For URL parsing |
40
|
|
|
*/ |
41
|
|
|
use UrlUtils; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Registers the current HTTP request and response |
45
|
|
|
* |
46
|
|
|
* @param ServerRequestInterface $request |
47
|
|
|
* @param ResponseInterface $response |
48
|
|
|
* |
49
|
|
|
* @return Controller|$this|self|ControllerInterface |
50
|
|
|
*/ |
51
|
20 |
|
public function register( |
52
|
|
|
ServerRequestInterface $request, ResponseInterface $response |
53
|
|
|
) { |
54
|
20 |
|
$this->request = $request; |
55
|
20 |
|
$this->response = $response; |
56
|
20 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Gets updated HTTP response |
61
|
|
|
* |
62
|
|
|
* @return ResponseInterface |
63
|
|
|
*/ |
64
|
10 |
|
public function getResponse() |
65
|
|
|
{ |
66
|
10 |
|
return $this->response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Gets updated HTTP request |
71
|
|
|
* |
72
|
|
|
* @return ServerRequestInterface |
73
|
|
|
*/ |
74
|
14 |
|
public function getRequest() |
75
|
|
|
{ |
76
|
14 |
|
return $this->request; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Sets a value to be used by render |
81
|
|
|
* |
82
|
|
|
* The key argument can be an associative array with values to be set |
83
|
|
|
* or a string naming the passed value. If an array is given then the |
84
|
|
|
* value will be ignored. |
85
|
|
|
* |
86
|
|
|
* Those values must be set in the request attributes so they can be used |
87
|
|
|
* latter by any other middle ware in the stack. |
88
|
|
|
* |
89
|
|
|
* @param string|array $key |
90
|
|
|
* @param mixed $value |
91
|
|
|
* |
92
|
|
|
* @return Controller|$this|self|ControllerInterface |
93
|
|
|
*/ |
94
|
8 |
|
public function set($key, $value = null) |
95
|
|
|
{ |
96
|
8 |
|
if (is_string($key)) { |
97
|
6 |
|
return $this->registerVar($key, $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
foreach ($key as $name => $value) { |
101
|
2 |
|
$this->registerVar($name, $value); |
102
|
1 |
|
} |
103
|
2 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Enables or disables rendering |
108
|
|
|
* |
109
|
|
|
* @param bool $disable |
110
|
|
|
* @return ControllerInterface|self|$this |
111
|
|
|
*/ |
112
|
2 |
|
public function disableRendering($disable = true) |
113
|
|
|
{ |
114
|
2 |
|
$this->request = $this->request->withAttribute('render', !$disable); |
115
|
2 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Changes the current rendering template |
120
|
|
|
* |
121
|
|
|
* @param string $template |
122
|
|
|
* @return ControllerInterface|self|$this |
123
|
|
|
*/ |
124
|
2 |
|
public function setView($template) |
125
|
|
|
{ |
126
|
2 |
|
$this->request = $this->request->withAttribute('template', $template); |
127
|
2 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Redirects the flow to another route/path |
132
|
|
|
* |
133
|
|
|
* @param string $path the route or path to redirect to |
134
|
|
|
* |
135
|
|
|
* @return Controller|self|$this |
136
|
|
|
*/ |
137
|
4 |
|
public function redirect($path) |
138
|
|
|
{ |
139
|
4 |
|
$args = func_get_args(); |
140
|
4 |
|
$url = call_user_func_array([$this, 'getUrl'], $args); |
141
|
4 |
|
$this->response = $this->createRedirectResponse($url); |
142
|
4 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the routed request attributes |
147
|
|
|
* |
148
|
|
|
* @param null|string $name |
149
|
|
|
* @param mixed $default |
150
|
|
|
* |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
public function getRouteAttributes($name = null, $default = null) |
154
|
|
|
{ |
155
|
|
|
/** @var Route $route */ |
156
|
|
|
$route = $this->request->getAttribute('route', false); |
157
|
|
|
$attributes = $route |
158
|
|
|
? $route->attributes |
159
|
|
|
: []; |
160
|
|
|
|
161
|
|
|
if (null == $name) { |
|
|
|
|
162
|
|
|
return $attributes; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return array_key_exists($name, $attributes) |
166
|
|
|
? $attributes[$name] |
167
|
|
|
: $default; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Register a variable value |
172
|
|
|
* |
173
|
|
|
* @param string $key |
174
|
|
|
* @param mixed $value |
175
|
|
|
* |
176
|
|
|
* @return Controller|$this|self |
177
|
|
|
*/ |
178
|
8 |
|
protected function registerVar($key, $value) |
179
|
|
|
{ |
180
|
8 |
|
$attrName = ControllerInterface::REQUEST_ATTR_VIEW_DATA; |
181
|
8 |
|
$attributes = $this->request->getAttributes(); |
182
|
8 |
|
$attributes[$attrName][$key] = $value; |
183
|
8 |
|
$this->request = $this->request |
184
|
8 |
|
->withAttribute($attrName, $attributes[$attrName]); |
185
|
8 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Creates a redirect response for provided path |
190
|
|
|
* |
191
|
|
|
* @param string $path |
192
|
|
|
* |
193
|
|
|
* @return Response |
194
|
|
|
*/ |
195
|
4 |
|
protected function createRedirectResponse($path) |
196
|
|
|
{ |
197
|
4 |
|
$response = $this->response->withStatus(302) |
198
|
4 |
|
->withHeader('Location', $path); |
199
|
4 |
|
return $response; |
200
|
|
|
} |
201
|
|
|
} |