Convert SessionManager to a factory class
This commit is contained in:
parent
5b0ee7651b
commit
de8c5e5dc7
10 changed files with 64 additions and 52 deletions
|
@ -10,7 +10,7 @@ use Alltube\Config;
|
|||
use Alltube\Library\Downloader;
|
||||
use Alltube\Library\Video;
|
||||
use Alltube\LocaleManager;
|
||||
use Alltube\SessionManager;
|
||||
use Alltube\SessionFactory;
|
||||
use Aura\Session\Segment;
|
||||
use Consolidation\Log\Logger;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
@ -85,7 +85,7 @@ abstract class BaseController
|
|||
{
|
||||
$this->config = $container->get('config');
|
||||
$this->container = $container;
|
||||
$session = SessionManager::getSession();
|
||||
$session = $container->get('session');
|
||||
$this->sessionSegment = $session->getSegment(self::class);
|
||||
$this->localeManager = $this->container->get('locale');
|
||||
$this->downloader = $this->config->getDownloader();
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Alltube\Factory;
|
|||
|
||||
use Alltube\Exception\DependencyException;
|
||||
use Alltube\LocaleManager;
|
||||
use Slim\Container;
|
||||
|
||||
/**
|
||||
* Class LocaleManagerFactory
|
||||
|
@ -13,15 +14,16 @@ class LocaleManagerFactory
|
|||
{
|
||||
|
||||
/**
|
||||
* @param Container $container
|
||||
* @return LocaleManager|null
|
||||
* @throws DependencyException
|
||||
*/
|
||||
public static function create()
|
||||
public static function create(Container $container)
|
||||
{
|
||||
if (!class_exists('Locale')) {
|
||||
throw new DependencyException('You need to install the intl extension for PHP.');
|
||||
}
|
||||
|
||||
return new LocaleManager();
|
||||
return new LocaleManager($container->get('session'));
|
||||
}
|
||||
}
|
||||
|
|
27
classes/Factory/SessionFactory.php
Normal file
27
classes/Factory/SessionFactory.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* SessionFactory class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Factory;
|
||||
|
||||
use Aura\Session\Session;
|
||||
|
||||
/**
|
||||
* Manage sessions.
|
||||
*/
|
||||
class SessionFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* Get the current session.
|
||||
*
|
||||
* @return Session
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
$session_factory = new \Aura\Session\SessionFactory();
|
||||
return $session_factory->newInstance($_COOKIE);
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@
|
|||
namespace Alltube;
|
||||
|
||||
use Aura\Session\Segment;
|
||||
use Aura\Session\Session;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Symfony\Component\Translation\Loader\PoFileLoader;
|
||||
|
@ -52,10 +53,10 @@ class LocaleManager
|
|||
|
||||
/**
|
||||
* LocaleManager constructor.
|
||||
* @param Session $session
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(Session $session)
|
||||
{
|
||||
$session = SessionManager::getSession();
|
||||
$this->sessionSegment = $session->getSegment(self::class);
|
||||
$cookieLocale = $this->sessionSegment->get('locale');
|
||||
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ use Alltube\ErrorHandler;
|
|||
use Alltube\Factory\ConfigFactory;
|
||||
use Alltube\Factory\LocaleManagerFactory;
|
||||
use Alltube\Factory\LoggerFactory;
|
||||
use Alltube\Factory\SessionFactory;
|
||||
use Alltube\Factory\ViewFactory;
|
||||
use Alltube\Middleware\CspMiddleware;
|
||||
use Alltube\Middleware\LinkHeaderMiddleware;
|
||||
|
@ -32,8 +33,11 @@ try {
|
|||
// Config.
|
||||
$container['config'] = ConfigFactory::create($container);
|
||||
|
||||
// Session.
|
||||
$container['session'] = SessionFactory::create();
|
||||
|
||||
// Locales.
|
||||
$container['locale'] = LocaleManagerFactory::create();
|
||||
$container['locale'] = LocaleManagerFactory::create($container);
|
||||
|
||||
// Smarty.
|
||||
$container['view'] = ViewFactory::create($container);
|
||||
|
|
|
@ -11,6 +11,9 @@ use Alltube\Controller\BaseController;
|
|||
use Alltube\Controller\DownloadController;
|
||||
use Alltube\Controller\FrontController;
|
||||
use Alltube\Exception\ConfigException;
|
||||
use Alltube\Exception\DependencyException;
|
||||
use Alltube\Factory\LocaleManagerFactory;
|
||||
use Alltube\Factory\SessionFactory;
|
||||
use Alltube\Factory\ViewFactory;
|
||||
use Alltube\LocaleManager;
|
||||
use Psr\Log\NullLogger;
|
||||
|
@ -55,6 +58,7 @@ abstract class ControllerTest extends BaseTest
|
|||
/**
|
||||
* Prepare tests.
|
||||
* @throws ConfigException|SmartyException
|
||||
* @throws DependencyException
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
|
@ -64,7 +68,8 @@ abstract class ControllerTest extends BaseTest
|
|||
$this->request = Request::createFromEnvironment(Environment::mock());
|
||||
$this->response = new Response();
|
||||
$this->container['config'] = Config::fromFile($this->getConfigFile());
|
||||
$this->container['locale'] = new LocaleManager();
|
||||
$this->container['session'] = SessionFactory::create();
|
||||
$this->container['locale'] = LocaleManagerFactory::create($this->container);
|
||||
$this->container['view'] = ViewFactory::create($this->container, $this->request);
|
||||
$this->container['logger'] = new NullLogger();
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
namespace Alltube\Test;
|
||||
|
||||
use Alltube\Factory\SessionFactory;
|
||||
use Alltube\Locale;
|
||||
use Alltube\LocaleManager;
|
||||
|
||||
|
@ -27,7 +28,7 @@ class LocaleManagerTest extends BaseTest
|
|||
protected function setUp(): void
|
||||
{
|
||||
$_SESSION[LocaleManager::class]['locale'] = 'foo_BAR';
|
||||
$this->localeManager = new LocaleManager();
|
||||
$this->localeManager = new LocaleManager(SessionFactory::create());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,7 +6,9 @@
|
|||
|
||||
namespace Alltube\Test;
|
||||
|
||||
use Alltube\LocaleManager;
|
||||
use Alltube\Exception\DependencyException;
|
||||
use Alltube\Factory\LocaleManagerFactory;
|
||||
use Alltube\Factory\SessionFactory;
|
||||
use Alltube\Middleware\LocaleMiddleware;
|
||||
use Slim\Container;
|
||||
use Slim\Http\Environment;
|
||||
|
@ -34,11 +36,13 @@ class LocaleMiddlewareTest extends BaseTest
|
|||
|
||||
/**
|
||||
* Prepare tests.
|
||||
* @throws DependencyException
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->container = new Container();
|
||||
$this->container['locale'] = new LocaleManager();
|
||||
$this->container['session'] = SessionFactory::create();
|
||||
$this->container['locale'] = LocaleManagerFactory::create($this->container);
|
||||
$this->middleware = new LocaleMiddleware($this->container);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,10 @@
|
|||
|
||||
namespace Alltube\Test;
|
||||
|
||||
use Alltube\Exception\DependencyException;
|
||||
use Alltube\Factory\LocaleManagerFactory;
|
||||
use Alltube\Factory\SessionFactory;
|
||||
use Alltube\Factory\ViewFactory;
|
||||
use Alltube\LocaleManager;
|
||||
use Slim\Container;
|
||||
use Slim\Http\Environment;
|
||||
use Slim\Http\Request;
|
||||
|
@ -24,11 +26,13 @@ class ViewFactoryTest extends BaseTest
|
|||
*
|
||||
* @return void
|
||||
* @throws SmartyException
|
||||
* @throws DependencyException
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$container = new Container();
|
||||
$container['locale'] = new LocaleManager();
|
||||
$container['session'] = SessionFactory::create();
|
||||
$container['locale'] = LocaleManagerFactory::create($container);
|
||||
$view = ViewFactory::create($container);
|
||||
$this->assertInstanceOf(Smarty::class, $view);
|
||||
}
|
||||
|
@ -38,11 +42,13 @@ class ViewFactoryTest extends BaseTest
|
|||
*
|
||||
* @return void
|
||||
* @throws SmartyException
|
||||
* @throws DependencyException
|
||||
*/
|
||||
public function testCreateWithXForwardedProto()
|
||||
{
|
||||
$container = new Container();
|
||||
$container['locale'] = new LocaleManager();
|
||||
$container['session'] = SessionFactory::create();
|
||||
$container['locale'] = LocaleManagerFactory::create($container);
|
||||
$request = Request::createFromEnvironment(Environment::mock());
|
||||
$view = ViewFactory::create($container, $request->withHeader('X-Forwarded-Proto', 'https'));
|
||||
$this->assertInstanceOf(Smarty::class, $view);
|
||||
|
|
Loading…
Reference in a new issue