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