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 Jawira\CaseConverter\CaseConverterException;
use Jean85\PrettyVersions;
use PackageVersions\Versions;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\Yaml\Yaml;
use Jawira\CaseConverter\Convert;
@ -159,7 +158,7 @@ class Config
$this->applyOptions($options);
$this->getEnv();
$this->validateOptions();
$localeManager = LocaleManager::getInstance();
$localeManager = new LocaleManager();
if (empty($this->genericFormats)) {
// 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;
/**
* Singleton instance.
*
* @var LocaleManager|null
*/
private static $instance;
/**
* LocaleManager constructor.
*/
private function __construct()
public function __construct()
{
$session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class);
@ -171,29 +164,4 @@ class LocaleManager
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.');
}
return LocaleManager::getInstance();
return new LocaleManager();
}
}

View File

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

View File

@ -27,7 +27,7 @@ class LocaleManagerTest extends BaseTest
protected function setUp(): void
{
$_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
{
$this->localeManager->unsetLocale();
LocaleManager::destroyInstance();
}
/**

View File

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

View File

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