1 | <?php |
||
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( |
|
58 | |||
59 | /** |
||
60 | * Gets updated HTTP response |
||
61 | * |
||
62 | * @return ResponseInterface |
||
63 | */ |
||
64 | 10 | public function getResponse() |
|
68 | |||
69 | /** |
||
70 | * Gets updated HTTP request |
||
71 | * |
||
72 | * @return ServerRequestInterface |
||
73 | */ |
||
74 | 14 | public function getRequest() |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
201 | } |