Use a factory to Config and LocaleManager (see #298)

This commit is contained in:
Pierre Rudloff 2020-07-15 23:05:41 +02:00
parent 96a75cbf14
commit 280618bb6b
9 changed files with 145 additions and 87 deletions

View file

@ -261,6 +261,7 @@ class Config
* Get Config singleton instance.
*
* @return Config
* @todo Stop using a singleton.
*/
public static function getInstance()
{

40
classes/ConfigFactory.php Normal file
View file

@ -0,0 +1,40 @@
<?php
namespace Alltube;
use Symfony\Component\ErrorHandler\Debug;
/**
* Class ConfigFactory
* @package Alltube
*/
class ConfigFactory
{
/**
* @return Config
* @throws Exception\ConfigException
*/
public static function create()
{
$configPath = __DIR__ . '/../config/config.yml';
if (is_file($configPath)) {
Config::setFile($configPath);
}
$config = Config::getInstance();
if ($config->uglyUrls) {
$container['router'] = new UglyRouter();
}
if ($config->debug) {
/*
We want to enable this as soon as possible,
in order to catch errors that are thrown
before the Slim error handler is ready.
*/
Debug::enable();
}
return $config;
}
}

View file

@ -83,7 +83,7 @@ abstract class BaseController
*/
public function __construct(ContainerInterface $container)
{
$this->config = Config::getInstance();
$this->config = $container->get('config');
$this->container = $container;
$session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class);

View file

@ -4,6 +4,10 @@ namespace Alltube\Exception;
use Exception;
/**
* Class ConfigException
* @package Alltube\Exception
*/
class ConfigException extends Exception
{

View file

@ -0,0 +1,14 @@
<?php
namespace Alltube\Exception;
use Exception;
/**
* Class DependencyException
* @package Alltube\Exception
*/
class DependencyException extends Exception
{
}

View file

@ -172,6 +172,7 @@ class LocaleManager
* Get LocaleManager singleton instance.
*
* @return LocaleManager
* @todo Stop using a singleton.
*/
public static function getInstance()
{

View file

@ -0,0 +1,26 @@
<?php
namespace Alltube;
use Alltube\Exception\DependencyException;
/**
* Class LocaleManagerFactory
* @package Alltube
*/
class LocaleManagerFactory
{
/**
* @return LocaleManager|null
* @throws DependencyException
*/
public static function create()
{
if (!class_exists('Locale')) {
throw new DependencyException('You need to install the intl extension for PHP.');
}
return LocaleManager::getInstance();
}
}

View file

@ -4,6 +4,7 @@ namespace Alltube;
use Consolidation\Log\Logger;
use Consolidation\Log\LogOutputStyler;
use Slim\Container;
use Symfony\Component\Console\Output\ConsoleOutput;
/**
@ -14,11 +15,12 @@ class LoggerFactory
{
/**
* @param Container $container
* @return Logger
*/
public static function create()
public static function create(Container $container)
{
$config = Config::getInstance();
$config = $container->get('config');
if ($config->debug) {
$verbosity = ConsoleOutput::VERBOSITY_DEBUG;
} else {