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. * Get Config singleton instance.
* *
* @return Config * @return Config
* @todo Stop using a singleton.
*/ */
public static function getInstance() 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) public function __construct(ContainerInterface $container)
{ {
$this->config = Config::getInstance(); $this->config = $container->get('config');
$this->container = $container; $this->container = $container;
$session = SessionManager::getSession(); $session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class); $this->sessionSegment = $session->getSegment(self::class);

View File

@ -4,6 +4,10 @@ namespace Alltube\Exception;
use Exception; use Exception;
/**
* Class ConfigException
* @package Alltube\Exception
*/
class ConfigException extends 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. * Get LocaleManager singleton instance.
* *
* @return LocaleManager * @return LocaleManager
* @todo Stop using a singleton.
*/ */
public static function getInstance() 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\Logger;
use Consolidation\Log\LogOutputStyler; use Consolidation\Log\LogOutputStyler;
use Slim\Container;
use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\ConsoleOutput;
/** /**
@ -14,11 +15,12 @@ class LoggerFactory
{ {
/** /**
* @param Container $container
* @return Logger * @return Logger
*/ */
public static function create() public static function create(Container $container)
{ {
$config = Config::getInstance(); $config = $container->get('config');
if ($config->debug) { if ($config->debug) {
$verbosity = ConsoleOutput::VERBOSITY_DEBUG; $verbosity = ConsoleOutput::VERBOSITY_DEBUG;
} else { } else {

138
index.php
View File

@ -2,118 +2,88 @@
require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/vendor/autoload.php';
use Alltube\Config; use Alltube\ConfigFactory;
use Alltube\Controller\DownloadController; use Alltube\Controller\DownloadController;
use Alltube\Controller\FrontController; use Alltube\Controller\FrontController;
use Alltube\Controller\JsonController; use Alltube\Controller\JsonController;
use Alltube\LocaleManager; use Alltube\LocaleManagerFactory;
use Alltube\LocaleMiddleware; use Alltube\LocaleMiddleware;
use Alltube\LoggerFactory; use Alltube\LoggerFactory;
use Alltube\UglyRouter;
use Alltube\ViewFactory; use Alltube\ViewFactory;
use Slim\App; use Slim\App;
use Slim\Container; use Slim\Container;
use Symfony\Component\ErrorHandler\Debug;
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) { if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) {
header('Location: ' . str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI'])); header('Location: ' . str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI']));
die; die;
} }
if (is_file(__DIR__ . '/config/config.yml')) {
try {
Config::setFile(__DIR__ . '/config/config.yml');
} catch (Exception $e) {
die('Could not load config file: ' . $e->getMessage());
}
}
// Create app.
$app = new App();
/** @var Container $container */
$container = $app->getContainer();
// Load config.
$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();
}
// Locales.
if (!class_exists('Locale')) {
die('You need to install the intl extension for PHP.');
}
$container['locale'] = LocaleManager::getInstance();
$app->add(new LocaleMiddleware($container));
// Smarty.
try { try {
// Create app.
$app = new App();
/** @var Container $container */
$container = $app->getContainer();
// Config.
$container['config'] = ConfigFactory::create();
// Locales.
$container['locale'] = LocaleManagerFactory::create();
$app->add(new LocaleMiddleware($container));
// Smarty.
$container['view'] = ViewFactory::create($container); $container['view'] = ViewFactory::create($container);
} catch (SmartyException $e) {
die('Could not load Smarty: ' . $e->getMessage());
}
// Logger. // Logger.
$container['logger'] = LoggerFactory::create(); $container['logger'] = LoggerFactory::create($container);
// Controllers. // Controllers.
$frontController = new FrontController($container); $frontController = new FrontController($container);
$jsonController = new JsonController($container); $jsonController = new JsonController($container);
$downloadController = new DownloadController($container); $downloadController = new DownloadController($container);
// Error handling. // Error handling.
$container['errorHandler'] = [$frontController, 'error']; $container['errorHandler'] = [$frontController, 'error'];
$container['phpErrorHandler'] = [$frontController, 'error']; $container['phpErrorHandler'] = [$frontController, 'error'];
// Routes. // Routes.
$app->get( $app->get(
'/', '/',
[$frontController, 'index'] [$frontController, 'index']
)->setName('index'); )->setName('index');
$app->get( $app->get(
'/extractors', '/extractors',
[$frontController, 'extractors'] [$frontController, 'extractors']
)->setName('extractors'); )->setName('extractors');
$app->any( $app->any(
'/info', '/info',
[$frontController, 'info'] [$frontController, 'info']
)->setName('info'); )->setName('info');
$app->any( $app->any(
'/watch', '/watch',
[$frontController, 'info'] [$frontController, 'info']
); );
$app->any( $app->any(
'/download', '/download',
[$downloadController, 'download'] [$downloadController, 'download']
)->setName('download'); )->setName('download');
$app->get( $app->get(
'/locale/{locale}', '/locale/{locale}',
[$frontController, 'locale'] [$frontController, 'locale']
)->setName('locale'); )->setName('locale');
$app->get( $app->get(
'/json', '/json',
[$jsonController, 'json'] [$jsonController, 'json']
)->setName('json'); )->setName('json');
try {
$app->run(); $app->run();
} catch (SmartyException $e) {
die('Smarty could not compile the template file: ' . $e->getMessage());
} catch (Throwable $e) { } catch (Throwable $e) {
// Last resort if the error has not been caught by the error handler for some reason. // Last resort if the error has not been caught by the error handler for some reason.
die('Error when starting the app: ' . htmlentities($e->getMessage())); die('Error when starting the app: ' . htmlentities($e->getMessage()));