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