-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHttpController.php
executable file
·193 lines (175 loc) · 5.75 KB
/
HttpController.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* Copyright 2021 Jeremy Presutti <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types=1);
namespace Feast;
use Exception;
use Feast\Enums\ResponseCode;
use Feast\Interfaces\ControllerInterface;
use Feast\Interfaces\ResponseInterface;
use Feast\Interfaces\RouterInterface;
use Feast\ServiceContainer\NotFoundException;
use Feast\ServiceContainer\ServiceContainer;
use stdClass;
/**
* Class to manage controllers.
*/
abstract class HttpController implements ControllerInterface
{
public stdClass $jsonEnabledActions;
public ResponseInterface $response;
/**
* Initial creation of Controller
*
* @param ServiceContainer $di
* @param View $view
* @param ResponseInterface|null $response
* @throws NotFoundException
*/
public function __construct(public ServiceContainer $di, public View $view, ?ResponseInterface $response = null)
{
$this->jsonEnabledActions = new stdClass();
$this->response = $response ?? $di->get(ResponseInterface::class);
}
/**
* Initialize Controller - return false if not runnable for any reason.
*
* @return bool
*/
public function init(): bool
{
return true;
}
/**
* Forward to another action/controller/route.
*
* The preDispatch for plugins will NOT rerun. PostDispatch runs on the final action only.
*
* @param string|null $action
* @param string|null $controller
* @param array<string> $arguments
* @param array<string> $queryString
* @param string|null $module
* @param string $route
* @throws NotFoundException
* @throws Exception
*/
public function forward(
string $action = null,
string $controller = null,
array $arguments = [],
array $queryString = [],
?string $module = null,
string $route = ''
): void {
/** @var RouterInterface $router */
$router = $this->di->get(RouterInterface::class);
$path = $router->getPath($action, $controller, $arguments, $queryString, $module, $route);
$router->buildRouteForRequestUrl($path);
$router->forward();
}
/**
* @param object|array $responseObject
* @param int|null $jsonResponsePropertyTypes (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
* @return void
*/
public function sendJsonResponse(object|array $responseObject, ?int $jsonResponsePropertyTypes = null): void
{
$this->response->setJsonWithResponseObject($responseObject, $jsonResponsePropertyTypes);
}
/**
* Check if an action should always be JSON.
*
* Defaults to false.
*
* @param string $actionName
* @return bool
*/
public function alwaysJson(string $actionName): bool
{
return false;
}
/**
* Redirect to another action/controller/route.
*
* Sends a redirect after post dispatch plugins are ran.
*
* @param string|null $action
* @param string|null $controller
* @param array<string> $arguments
* @param array<string> $queryString
* @param string|null $module
* @param string $route
* @param ResponseCode $code (30x)
* @throws NotFoundException
* @throws Exception
*/
public function redirect(
?string $action = null,
?string $controller = null,
array $arguments = [],
array $queryString = [],
?string $module = null,
string $route = '',
ResponseCode $code = ResponseCode::HTTP_CODE_302
): void {
$router = $this->di->get(RouterInterface::class);
$response = $this->di->get(ResponseInterface::class);
if ($route === '') {
$route = $router->getRouteName();
}
$path = '/' . $router->getPath($action, $controller, $arguments, $queryString, $module, $route);
$response->redirect($path, $code);
}
/**
* Redirect to an external link after all post dispatch plugins finish.
*
* @param string $url The URL to redirect to
* @param ResponseCode $code Redirect code to use (default 302)
* @throws NotFoundException
*/
public function externalRedirect(string $url, ResponseCode $code = ResponseCode::HTTP_CODE_302): void
{
$response = $this->di->get(ResponseInterface::class);
$response->redirect($url, $code);
}
/**
* Marks an action as being allowed to return a JSON object instead of output content.
*
* @param string $action
*/
public function allowJsonForAction(string $action): void
{
$this->jsonEnabledActions->$action = true;
}
/**
* Return if an action is allowed to return a JSON object.
*
* If false, format/json will have NO effect.
*
* @return bool
* @throws NotFoundException
*/
public function jsonAllowed(): bool
{
$router = $this->di->get(RouterInterface::class);
$action = $router->getActionName();
$actionReformatted = $router->getActionNameCamelCase();
return $this->alwaysJson(
$action
) || isset($this->jsonEnabledActions->$action) || isset($this->jsonEnabledActions->$actionReformatted);
}
}