Url: store current request in a public static class variable to allow access from everywhere

This commit is contained in:
Benjamin Renard 2023-02-25 05:03:19 +01:00
parent 610cdb0f7c
commit 3edf176dc2

View file

@ -6,6 +6,12 @@ use Exception;
class Url {
/**
* Current request
* @var UrlRequest|null
*/
public static $request = null;
/**
* Configured URL patterns :
*
@ -426,31 +432,31 @@ class Url {
public static function handle_request($default_url=null) {
$sentry_span = new SentrySpan('http.handle_request', 'Handle the HTTP request');
$request = self :: get_request($default_url);
self :: $request = self :: get_request($default_url);
if (!is_callable($request -> handler)) {
if (!is_callable(self :: $request -> handler)) {
Log :: error(
"URL handler function %s does not exists !",
format_callable($request -> handler));
format_callable(self :: $request -> handler));
Log :: fatal(I18n::_("This request cannot be processed."));
}
if ($request -> api_mode)
if (self :: $request -> api_mode)
self :: $_api_mode = true;
if (Tpl :: initialized())
Tpl :: assign('request', $request);
Tpl :: assign('request', self :: $request );
// Check authentication (if need)
if($request -> authenticated && !Auth::login(true))
if(self :: $request -> authenticated && !Auth::login(true))
Log :: fatal(I18n::_("Authentication required but fail to authenticate you."));
try {
call_user_func($request -> handler, $request);
call_user_func(self :: $request -> handler, self :: $request );
}
catch (Exception $e) {
Log :: exception(
$e, "An exception occured running URL handler function %s()",
format_callable($request -> handler));
$e, "An exception occured running URL handler %s",
format_callable(self :: $request -> handler));
Log :: fatal(I18n::_("This request could not be processed correctly."));
}
$sentry_span->finish();