Added $grav->exit() method to properly terminate the request with a response

This commit is contained in:
Matias Griese
2019-06-03 12:10:43 +03:00
parent dc5390b3dc
commit ca89156c4f
2 changed files with 46 additions and 22 deletions

View File

@@ -6,6 +6,7 @@
* Added support for [Tideways XHProf](https://github.com/tideways/php-xhprof-extension) PHP Extension for profiling method calls
* Updated Symfony Components to 4.3
* Added support for Twig 2.11 (compatible with Twig 1.40+)
* Added `$grav->exit()` method to properly terminate the request with a response
# v1.6.10
## mm/dd/2019

View File

@@ -236,26 +236,46 @@ class Grav extends Container
}
/**
* Set the system locale based on the language and configuration
* Terminates Grav request with a response.
*
* Please use this method instead of calling `die();` or `exit();`. Note that you need to create a response object.
*
* @param ResponseInterface $response
*/
public function setLocale()
public function exit(ResponseInterface $response): void
{
// Initialize Locale if set and configured.
if ($this['language']->enabled() && $this['config']->get('system.languages.override_locale')) {
$language = $this['language']->getLanguage();
setlocale(LC_ALL, \strlen($language) < 3 ? ($language . '_' . strtoupper($language)) : $language);
} elseif ($this['config']->get('system.default_locale')) {
setlocale(LC_ALL, $this['config']->get('system.default_locale'));
// Make sure nothing extra gets written to the response.
while (ob_get_level()) {
ob_end_clean();
}
// Close the session.
if (isset($this['session'])) {
$this['session']->close();
}
/** @var ServerRequestInterface $request */
$request = $this['request'];
/** @var Debugger $debugger */
$debugger = $this['debugger'];
$response = $debugger->logRequest($request, $response);
// Send the response and terminate.
$this->header($response);
echo $response->getBody();
exit();
}
/**
* Redirect browser to another location.
* Terminates Grav request and redirects browser to another location.
*
* Please use this method instead of calling `header("Location: {$url}", true, 302); exit();`.
*
* @param string $route Internal route.
* @param int $code Redirection code (30x)
*/
public function redirect($route, $code = null)
public function redirect($route, $code = null): void
{
/** @var Uri $uri */
$uri = $this['uri'];
@@ -272,10 +292,6 @@ class Grav extends Container
$code = $this['config']->get('system.pages.redirect_default_code', 302);
}
if (isset($this['session'])) {
$this['session']->close();
}
if ($uri::isExternal($route)) {
$url = $route;
} else {
@@ -288,16 +304,9 @@ class Grav extends Container
}
}
/** @var ServerRequestInterface $request */
$request = $this['request'];
$response = new Response($code, ['Location' => $url]);
/** @var Debugger $debugger */
$debugger = $this['debugger'];
$response = $debugger->logRequest($request, $response);
$this->header($response);
exit();
$this->exit($response);
}
/**
@@ -340,6 +349,20 @@ class Grav extends Container
}
}
/**
* Set the system locale based on the language and configuration
*/
public function setLocale()
{
// Initialize Locale if set and configured.
if ($this['language']->enabled() && $this['config']->get('system.languages.override_locale')) {
$language = $this['language']->getLanguage();
setlocale(LC_ALL, \strlen($language) < 3 ? ($language . '_' . strtoupper($language)) : $language);
} elseif ($this['config']->get('system.default_locale')) {
setlocale(LC_ALL, $this['config']->get('system.default_locale'));
}
}
/**
* Fires an event with optional parameters.
*