From 280618bb6b0b60d89c57a8960bcdf796ab8d2602 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Wed, 15 Jul 2020 23:05:41 +0200 Subject: [PATCH] Use a factory to Config and LocaleManager (see #298) --- classes/Config.php | 1 + classes/ConfigFactory.php | 40 +++++++ classes/Controller/BaseController.php | 2 +- classes/Exception/ConfigException.php | 4 + classes/Exception/DependencyException.php | 14 +++ classes/LocaleManager.php | 1 + classes/LocaleManagerFactory.php | 26 ++++ classes/LoggerFactory.php | 6 +- index.php | 138 +++++++++------------- 9 files changed, 145 insertions(+), 87 deletions(-) create mode 100644 classes/ConfigFactory.php create mode 100644 classes/Exception/DependencyException.php create mode 100644 classes/LocaleManagerFactory.php diff --git a/classes/Config.php b/classes/Config.php index 0460765..a37497e 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -261,6 +261,7 @@ class Config * Get Config singleton instance. * * @return Config + * @todo Stop using a singleton. */ public static function getInstance() { diff --git a/classes/ConfigFactory.php b/classes/ConfigFactory.php new file mode 100644 index 0000000..03cae73 --- /dev/null +++ b/classes/ConfigFactory.php @@ -0,0 +1,40 @@ +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; + } +} diff --git a/classes/Controller/BaseController.php b/classes/Controller/BaseController.php index 30a157c..a152f11 100644 --- a/classes/Controller/BaseController.php +++ b/classes/Controller/BaseController.php @@ -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); diff --git a/classes/Exception/ConfigException.php b/classes/Exception/ConfigException.php index f533b35..98c1207 100644 --- a/classes/Exception/ConfigException.php +++ b/classes/Exception/ConfigException.php @@ -4,6 +4,10 @@ namespace Alltube\Exception; use Exception; +/** + * Class ConfigException + * @package Alltube\Exception + */ class ConfigException extends Exception { diff --git a/classes/Exception/DependencyException.php b/classes/Exception/DependencyException.php new file mode 100644 index 0000000..e5f04e1 --- /dev/null +++ b/classes/Exception/DependencyException.php @@ -0,0 +1,14 @@ +get('config'); if ($config->debug) { $verbosity = ConsoleOutput::VERBOSITY_DEBUG; } else { diff --git a/index.php b/index.php index ecbdea5..9814f47 100644 --- a/index.php +++ b/index.php @@ -2,118 +2,88 @@ require_once __DIR__ . '/vendor/autoload.php'; -use Alltube\Config; +use Alltube\ConfigFactory; use Alltube\Controller\DownloadController; use Alltube\Controller\FrontController; use Alltube\Controller\JsonController; -use Alltube\LocaleManager; +use Alltube\LocaleManagerFactory; use Alltube\LocaleMiddleware; use Alltube\LoggerFactory; -use Alltube\UglyRouter; use Alltube\ViewFactory; use Slim\App; use Slim\Container; -use Symfony\Component\ErrorHandler\Debug; if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) { header('Location: ' . str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI'])); 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 { + // 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); -} catch (SmartyException $e) { - die('Could not load Smarty: ' . $e->getMessage()); -} -// Logger. -$container['logger'] = LoggerFactory::create(); + // Logger. + $container['logger'] = LoggerFactory::create($container); -// Controllers. -$frontController = new FrontController($container); -$jsonController = new JsonController($container); -$downloadController = new DownloadController($container); + // Controllers. + $frontController = new FrontController($container); + $jsonController = new JsonController($container); + $downloadController = new DownloadController($container); -// Error handling. -$container['errorHandler'] = [$frontController, 'error']; -$container['phpErrorHandler'] = [$frontController, 'error']; + // Error handling. + $container['errorHandler'] = [$frontController, 'error']; + $container['phpErrorHandler'] = [$frontController, 'error']; -// Routes. -$app->get( - '/', - [$frontController, 'index'] -)->setName('index'); + // Routes. + $app->get( + '/', + [$frontController, 'index'] + )->setName('index'); -$app->get( - '/extractors', - [$frontController, 'extractors'] -)->setName('extractors'); + $app->get( + '/extractors', + [$frontController, 'extractors'] + )->setName('extractors'); -$app->any( - '/info', - [$frontController, 'info'] -)->setName('info'); + $app->any( + '/info', + [$frontController, 'info'] + )->setName('info'); -$app->any( - '/watch', - [$frontController, 'info'] -); + $app->any( + '/watch', + [$frontController, 'info'] + ); -$app->any( - '/download', - [$downloadController, 'download'] -)->setName('download'); + $app->any( + '/download', + [$downloadController, 'download'] + )->setName('download'); -$app->get( - '/locale/{locale}', - [$frontController, 'locale'] -)->setName('locale'); + $app->get( + '/locale/{locale}', + [$frontController, 'locale'] + )->setName('locale'); -$app->get( - '/json', - [$jsonController, 'json'] -)->setName('json'); + $app->get( + '/json', + [$jsonController, 'json'] + )->setName('json'); -try { $app->run(); -} catch (SmartyException $e) { - die('Smarty could not compile the template file: ' . $e->getMessage()); } catch (Throwable $e) { // 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()));