Stop using a singleton for LocaleManager (#298)

This commit is contained in:
Pierre Rudloff 2020-10-17 22:36:03 +02:00
parent f5045b3ae7
commit 15636aa435
7 changed files with 9 additions and 46 deletions

View file

@ -10,7 +10,6 @@ use Alltube\Exception\ConfigException;
use Alltube\Library\Downloader; use Alltube\Library\Downloader;
use Jawira\CaseConverter\CaseConverterException; use Jawira\CaseConverter\CaseConverterException;
use Jean85\PrettyVersions; use Jean85\PrettyVersions;
use PackageVersions\Versions;
use Symfony\Component\ErrorHandler\Debug; use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Jawira\CaseConverter\Convert; use Jawira\CaseConverter\Convert;
@ -159,7 +158,7 @@ class Config
$this->applyOptions($options); $this->applyOptions($options);
$this->getEnv(); $this->getEnv();
$this->validateOptions(); $this->validateOptions();
$localeManager = LocaleManager::getInstance(); $localeManager = new LocaleManager();
if (empty($this->genericFormats)) { if (empty($this->genericFormats)) {
// We don't put this in the class definition so it can be detected by xgettext. // We don't put this in the class definition so it can be detected by xgettext.

View file

@ -50,17 +50,10 @@ class LocaleManager
*/ */
private $translator; private $translator;
/**
* Singleton instance.
*
* @var LocaleManager|null
*/
private static $instance;
/** /**
* LocaleManager constructor. * LocaleManager constructor.
*/ */
private function __construct() public function __construct()
{ {
$session = SessionManager::getSession(); $session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class); $this->sessionSegment = $session->getSegment(self::class);
@ -171,29 +164,4 @@ class LocaleManager
return ''; return '';
} }
/**
* Get LocaleManager singleton instance.
*
* @return LocaleManager
* @todo Stop using a singleton.
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Destroy singleton instance.
*
* @return void
*/
public static function destroyInstance()
{
self::$instance = null;
}
} }

View file

@ -21,6 +21,6 @@ class LocaleManagerFactory
throw new DependencyException('You need to install the intl extension for PHP.'); throw new DependencyException('You need to install the intl extension for PHP.');
} }
return LocaleManager::getInstance(); return new LocaleManager();
} }
} }

View file

@ -11,8 +11,7 @@ use Alltube\Controller\BaseController;
use Alltube\Controller\DownloadController; use Alltube\Controller\DownloadController;
use Alltube\Controller\FrontController; use Alltube\Controller\FrontController;
use Alltube\Exception\ConfigException; use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException; use Alltube\LocaleManager;
use Alltube\LocaleManagerFactory;
use Alltube\ViewFactory; use Alltube\ViewFactory;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
use Slim\Container; use Slim\Container;
@ -56,7 +55,6 @@ abstract class ControllerTest extends BaseTest
/** /**
* Prepare tests. * Prepare tests.
* @throws ConfigException|SmartyException * @throws ConfigException|SmartyException
* @throws DependencyException
*/ */
protected function setUp(): void protected function setUp(): void
{ {
@ -66,7 +64,7 @@ abstract class ControllerTest extends BaseTest
$this->request = Request::createFromEnvironment(Environment::mock()); $this->request = Request::createFromEnvironment(Environment::mock());
$this->response = new Response(); $this->response = new Response();
$this->container['config'] = Config::fromFile($this->getConfigFile()); $this->container['config'] = Config::fromFile($this->getConfigFile());
$this->container['locale'] = LocaleManagerFactory::create(); $this->container['locale'] = new LocaleManager();
$this->container['view'] = ViewFactory::create($this->container, $this->request); $this->container['view'] = ViewFactory::create($this->container, $this->request);
$this->container['logger'] = new NullLogger(); $this->container['logger'] = new NullLogger();

View file

@ -27,7 +27,7 @@ class LocaleManagerTest extends BaseTest
protected function setUp(): void protected function setUp(): void
{ {
$_SESSION[LocaleManager::class]['locale'] = 'foo_BAR'; $_SESSION[LocaleManager::class]['locale'] = 'foo_BAR';
$this->localeManager = LocaleManager::getInstance(); $this->localeManager = new LocaleManager();
} }
/** /**
@ -38,7 +38,6 @@ class LocaleManagerTest extends BaseTest
protected function tearDown(): void protected function tearDown(): void
{ {
$this->localeManager->unsetLocale(); $this->localeManager->unsetLocale();
LocaleManager::destroyInstance();
} }
/** /**

View file

@ -38,7 +38,7 @@ class LocaleMiddlewareTest extends BaseTest
protected function setUp(): void protected function setUp(): void
{ {
$this->container = new Container(); $this->container = new Container();
$this->container['locale'] = LocaleManager::getInstance(); $this->container['locale'] = new LocaleManager();
$this->middleware = new LocaleMiddleware($this->container); $this->middleware = new LocaleMiddleware($this->container);
} }
@ -50,7 +50,6 @@ class LocaleMiddlewareTest extends BaseTest
protected function tearDown(): void protected function tearDown(): void
{ {
$this->container['locale']->unsetLocale(); $this->container['locale']->unsetLocale();
LocaleManager::destroyInstance();
} }
/** /**

View file

@ -28,7 +28,7 @@ class ViewFactoryTest extends BaseTest
public function testCreate() public function testCreate()
{ {
$container = new Container(); $container = new Container();
$container['locale'] = LocaleManager::getInstance(); $container['locale'] = new LocaleManager();
$view = ViewFactory::create($container); $view = ViewFactory::create($container);
$this->assertInstanceOf(Smarty::class, $view); $this->assertInstanceOf(Smarty::class, $view);
} }
@ -42,7 +42,7 @@ class ViewFactoryTest extends BaseTest
public function testCreateWithXForwardedProto() public function testCreateWithXForwardedProto()
{ {
$container = new Container(); $container = new Container();
$container['locale'] = LocaleManager::getInstance(); $container['locale'] = new LocaleManager();
$request = Request::createFromEnvironment(Environment::mock()); $request = Request::createFromEnvironment(Environment::mock());
$view = ViewFactory::create($container, $request->withHeader('X-Forwarded-Proto', 'https')); $view = ViewFactory::create($container, $request->withHeader('X-Forwarded-Proto', 'https'));
$this->assertInstanceOf(Smarty::class, $view); $this->assertInstanceOf(Smarty::class, $view);