Added better session checks, warn in debugbar if session messages cannot be used

This commit is contained in:
Matias Griese
2018-02-21 12:53:40 +02:00
parent 742c6f9baa
commit 01886b6df9
2 changed files with 20 additions and 9 deletions

View File

@@ -174,23 +174,23 @@ class Language
// if languages set
if ($this->enabled()) {
// try setting from prefix of URL (/en/blah/blah)
// Try setting language from prefix of URL (/en/blah/blah).
if (preg_match($regex, $uri, $matches)) {
$this->lang_in_url = true;
$this->active = $matches[2];
$uri = preg_replace("/\\" . $matches[1] . "/", '', $uri, 1);
$uri = preg_replace("/\\" . $matches[1] . '/', '', $uri, 1);
// store in session if different
if ($this->config->get('system.session.enabled', false)
// Store in session if language is different.
if (isset($this->grav['session']) && $this->grav['session']->started()
&& $this->config->get('system.languages.session_store_active', true)
&& $this->grav['session']->active_language != $this->active
) {
$this->grav['session']->active_language = $this->active;
}
} else {
// try getting from session, else no active
if ($this->config->get('system.session.enabled', false) &&
$this->config->get('system.languages.session_store_active', true)) {
// Try getting language from the session, else no active.
if (isset($this->grav['session']) && $this->grav['session']->started()
&& $this->config->get('system.languages.session_store_active', true)) {
$this->active = $this->grav['session']->active_language ?: null;
}
// if still null, try from http_accept_language header
@@ -203,8 +203,8 @@ class Language
}
}
// repeat if not found, try base language only - fixes Safari sending the language code always
// with a locale (e.g. it-it or fr-fr)
// Repeat if not found, try base language only - fixes Safari sending the language code always
// with a locale (e.g. it-it or fr-fr).
foreach ($preferred as $lang) {
$lang = substr($lang, 0, 2);
if ($this->validate($lang)) {

View File

@@ -8,6 +8,8 @@
namespace Grav\Common\Service;
use Grav\Common\Debugger;
use Grav\Common\Session;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use RocketTheme\Toolbox\Session\Message;
@@ -18,6 +20,15 @@ class MessagesServiceProvider implements ServiceProviderInterface
{
// Define session message service.
$container['messages'] = function ($c) {
if (!isset($c['session']) || !$c['session']->started()) {
/** @var Debugger $debugger */
$debugger = $c['debugger'];
$debugger->addMessage('Session not started: session messages may disappear', 'warming');
return new Message;
}
/** @var Session $session */
$session = $c['session'];
if (!isset($session->messages)) {