refactor: New Session class

So that session is shared between classes and does not get overwritten
This commit is contained in:
Pierre Rudloff 2019-04-22 17:00:51 +02:00
parent 25f33bba56
commit 1387d836dc
5 changed files with 48 additions and 18 deletions

View file

@ -6,7 +6,6 @@
namespace Alltube; namespace Alltube;
use Aura\Session\Segment; use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
/** /**
@ -37,13 +36,10 @@ class LocaleManager
/** /**
* LocaleManager constructor. * LocaleManager constructor.
*
* @param array $cookies Cookie array
*/ */
public function __construct(array $cookies = []) public function __construct()
{ {
$session_factory = new SessionFactory(); $session = SessionManager::getSession();
$session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment(self::class); $this->sessionSegment = $session->getSegment(self::class);
$cookieLocale = $this->sessionSegment->get('locale'); $cookieLocale = $this->sessionSegment->get('locale');
if (isset($cookieLocale)) { if (isset($cookieLocale)) {

View file

@ -0,0 +1,37 @@
<?php
/**
* SessionManager class.
*/
namespace Alltube;
use Aura\Session\Session;
use Aura\Session\SessionFactory;
/**
* Manage sessions.
*/
class SessionManager
{
/**
* Current session.
*
* @var Session
*/
private static $session;
/**
* Get the current session.
*
* @return Session
*/
public static function getSession()
{
if (!isset(self::$session)) {
$session_factory = new SessionFactory();
self::$session = $session_factory->newInstance($_COOKIE);
}
return self::$session;
}
}

View file

@ -6,9 +6,9 @@
namespace Alltube\Controller; namespace Alltube\Controller;
use Alltube\Config; use Alltube\Config;
use Alltube\SessionManager;
use Alltube\Video; use Alltube\Video;
use Aura\Session\Segment; use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
@ -57,14 +57,12 @@ abstract class BaseController
* BaseController constructor. * BaseController constructor.
* *
* @param ContainerInterface $container Slim dependency container * @param ContainerInterface $container Slim dependency container
* @param array $cookies Cookie array
*/ */
public function __construct(ContainerInterface $container, array $cookies = []) public function __construct(ContainerInterface $container)
{ {
$this->config = Config::getInstance(); $this->config = Config::getInstance();
$this->container = $container; $this->container = $container;
$session_factory = new SessionFactory(); $session = SessionManager::getSession();
$session = $session_factory->newInstance($cookies);
$this->sessionSegment = $session->getSegment(self::class); $this->sessionSegment = $session->getSegment(self::class);
if ($this->config->stream) { if ($this->config->stream) {

View file

@ -42,11 +42,10 @@ class FrontController extends BaseController
* BaseController constructor. * BaseController constructor.
* *
* @param ContainerInterface $container Slim dependency container * @param ContainerInterface $container Slim dependency container
* @param array $cookies Cookie array
*/ */
public function __construct(ContainerInterface $container, array $cookies = []) public function __construct(ContainerInterface $container)
{ {
parent::__construct($container, $cookies); parent::__construct($container);
$this->localeManager = $this->container->get('locale'); $this->localeManager = $this->container->get('locale');
$this->view = $this->container->get('view'); $this->view = $this->container->get('view');

View file

@ -31,12 +31,12 @@ $container['view'] = ViewFactory::create($container);
if (!class_exists('Locale')) { if (!class_exists('Locale')) {
die('You need to install the intl extension for PHP.'); die('You need to install the intl extension for PHP.');
} }
$container['locale'] = new LocaleManager($_COOKIE); $container['locale'] = new LocaleManager();
$app->add(new LocaleMiddleware($container)); $app->add(new LocaleMiddleware($container));
$frontController = new FrontController($container, $_COOKIE); $frontController = new FrontController($container);
$jsonController = new JsonController($container, $_COOKIE); $jsonController = new JsonController($container);
$downloadController = new DownloadController($container, $_COOKIE); $downloadController = new DownloadController($container);
$container['errorHandler'] = [$jsonController, 'error']; $container['errorHandler'] = [$jsonController, 'error'];