Skip to content

Commit ade53ff

Browse files
committed
added createRedirectResponse into utils class
1 parent f836ced commit ade53ff

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

app/App.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use App\Base\Models\Rewrite;
1717
use Degami\Basics\Exceptions\BasicException;
1818
use DI\ContainerBuilder;
19-
use Symfony\Component\HttpFoundation\RedirectResponse;
2019
use Symfony\Component\HttpFoundation\Response;
2120
use FastRoute\Dispatcher;
2221
use Psr\Container\ContainerInterface;
@@ -196,11 +195,7 @@ public function __construct() {
196195
App::$instance = $this;
197196
} catch (Throwable $e) {
198197
$response = new Response(
199-
$this->genericErrorPage(
200-
'Critical Error',
201-
$e->getMessage(),
202-
$e
203-
),
198+
$this->genericErrorPage('Critical Error', $e->getMessage()),
204199
500
205200
);
206201
$response->send();
@@ -254,7 +249,7 @@ public function bootstrap()
254249
}
255250

256251
// redirect to new url
257-
$response = RedirectResponse::create(
252+
$response = $this->getUtils()->createRedirectResponse(
258253
$redirects[$redirect_key]['url_to'],
259254
$redirects[$redirect_key]['redirect_code']
260255
);
@@ -353,11 +348,7 @@ public function bootstrap()
353348
$response = $this->containerCall([$this->getUtils(), 'exceptionPage'], ['exception' => $e, 'route_info' => $this->getAppRouteInfo()]);
354349
} else {
355350
$response = new Response(
356-
$this->genericErrorPage(
357-
'Critical Error',
358-
$e->getMessage(),
359-
$e
360-
),
351+
$this->genericErrorPage('Critical Error', $e->getMessage()),
361352
500
362353
);
363354
}

app/base/abstracts/Controllers/BasePage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public function canBeFPC(): bool
295295
*/
296296
protected function doRedirect($url, array $additional_headers = []): RedirectResponse
297297
{
298-
return new RedirectResponse(
298+
return $this->getUtils()->createRedirectResponse(
299299
$url,
300300
302,
301301
array_merge(

app/base/tools/Utils/Globals.php

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use App\Base\Models\User;
4343
use App\App;
4444
use Symfony\Component\HttpFoundation\JsonResponse;
45+
use Symfony\Component\HttpFoundation\RedirectResponse;
4546

4647
/**
4748
* Global utils functions Helper Class
@@ -166,35 +167,35 @@ public function errorPage(int $error_code, Request $request, ?RouteInfo $route_i
166167
$template = $this->getTemplates()->make($template_name ?: 'errors::' . $error_code);
167168
$template->data($template_data);
168169

169-
return (new Response(
170+
return $this->createHtmlResponse(
170171
$template->render(),
171172
$error_code
172-
));
173+
);
173174
case 503:
174175
$template = $this->getTemplates()->make($template_name ?: 'errors::offline');
175176
$template_data['body_class'] = 'maintenance';
176177
$template->data($template_data);
177178

178-
return (new Response(
179+
return $this->createHtmlResponse(
179180
$template->render(),
180181
$error_code
181-
));
182+
);
182183
}
183184

184185
if ($error_code == 500 && isset($template_data['e'])) {
185186
$template = $this->getTemplates()->make($template_name ?: 'errors::exception');
186187
$template->data($template_data);
187188

188-
return (new Response(
189+
return $this->createHtmlResponse(
189190
$template->render(),
190191
500
191-
));
192+
);
192193
}
193194

194-
return (new Response(
195+
return $this->createHtmlResponse(
195196
$this->getTemplates()->make('errors::500')->render(),
196197
500
197-
));
198+
);
198199
}
199200

200201
/**
@@ -265,13 +266,13 @@ public function blockedIpPage(Request $request, ?RouteInfo $route_info = null):
265266
*
266267
* @param Exception $exception
267268
* @param Request $request
268-
* @return Response
269+
* @return JsonResponse
269270
* @throws BasicException
270271
* @throws DependencyException
271272
* @throws NotFoundException
272273
* @throws PhpfastcacheSimpleCacheException
273274
*/
274-
public function exceptionJson(Exception $exception, Request $request): Response
275+
public function exceptionJson(Exception $exception, Request $request): JsonResponse
275276
{
276277
$this->logRequestIfNeeded(500, $request);
277278

@@ -298,11 +299,10 @@ public function exceptionJson(Exception $exception, Request $request): Response
298299
default => 500,
299300
};
300301

301-
return (new Response(
302-
json_encode($content),
303-
$exceptionCode,
304-
['Content-Type' => 'application/json']
305-
));
302+
return $this->createJsonResponse(
303+
$content,
304+
$exceptionCode
305+
);
306306
}
307307

308308
/**
@@ -333,11 +333,10 @@ public function exceptionXML(Exception $exception, Request $request): Response
333333
];
334334
}
335335

336-
return (new Response(
336+
return $this->createXmlResponse(
337337
ArrayToXml::convert($content),
338-
500,
339-
['Content-Type' => 'text/xml']
340-
));
338+
500
339+
);
341340
}
342341

343342
/**
@@ -818,4 +817,36 @@ public function createHtmlResponse(string $content, int $status = 200): Response
818817
['Content-Type' => 'text/html']
819818
);
820819
}
820+
821+
/**
822+
* Creates an XML response.
823+
*
824+
* @param string $content
825+
* @param int $status
826+
* @return Response
827+
*/
828+
public function createXmlResponse(string $content, int $status = 200): Response
829+
{
830+
return new Response(
831+
$content,
832+
$status,
833+
['Content-Type' => 'text/xml']
834+
);
835+
}
836+
837+
/**
838+
* returns a redirect object
839+
*
840+
* @param $url
841+
* @param array $additional_headers
842+
* @return RedirectResponse
843+
*/
844+
public function createRedirectResponse(string $url, int $code, array $headers = []): RedirectResponse
845+
{
846+
return new RedirectResponse(
847+
$url,
848+
$code,
849+
$headers
850+
);
851+
}
821852
}

0 commit comments

Comments
 (0)