From 7ad0040f6084a38b2bcf097a0b4fef0425cf3071 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Thu, 22 Oct 2020 23:08:40 +0200 Subject: [PATCH] Move container creation to a new App class --- classes/App.php | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ index.php | 86 +-------------------------------------- 2 files changed, 106 insertions(+), 85 deletions(-) create mode 100644 classes/App.php diff --git a/classes/App.php b/classes/App.php new file mode 100644 index 0000000..a2b8434 --- /dev/null +++ b/classes/App.php @@ -0,0 +1,105 @@ +getContainer(); + + // Config. + $container['config'] = ConfigFactory::create($container); + + // Session. + $container['session'] = SessionFactory::create($container); + + // Locales. + $container['locale'] = LocaleManagerFactory::create($container); + + // Smarty. + $container['view'] = ViewFactory::create($container); + + // Logger. + $container['logger'] = LoggerFactory::create($container); + + // Middlewares. + $this->add(new LocaleMiddleware($container)); + $this->add(new CspMiddleware($container)); + $this->add(new LinkHeaderMiddleware($container)); + $this->add(new RouterPathMiddleware($container)); + + // Controllers. + $frontController = new FrontController($container); + $jsonController = new JsonController($container); + $downloadController = new DownloadController($container); + + // Error handling. + $container['errorHandler'] = [$frontController, 'error']; + $container['phpErrorHandler'] = [$frontController, 'error']; + $container['notFoundHandler'] = [$frontController, 'notFound']; + $container['notAllowedHandler'] = [$frontController, 'notAllowed']; + + // Routes. + $this->get( + '/', + [$frontController, 'index'] + )->setName('index'); + + $this->get( + '/extractors', + [$frontController, 'extractors'] + )->setName('extractors'); + + $this->any( + '/info', + [$frontController, 'info'] + )->setName('info'); + + $this->any( + '/watch', + [$frontController, 'info'] + ); + + $this->any( + '/download', + [$downloadController, 'download'] + )->setName('download'); + + $this->get( + '/locale/{locale}', + [$frontController, 'locale'] + )->setName('locale'); + + $this->get( + '/json', + [$jsonController, 'json'] + )->setName('json'); + } +} diff --git a/index.php b/index.php index f386247..112fb7d 100644 --- a/index.php +++ b/index.php @@ -2,21 +2,8 @@ require_once __DIR__ . '/vendor/autoload.php'; -use Alltube\Controller\DownloadController; -use Alltube\Controller\FrontController; -use Alltube\Controller\JsonController; +use Alltube\App; use Alltube\ErrorHandler; -use Alltube\Factory\ConfigFactory; -use Alltube\Factory\LocaleManagerFactory; -use Alltube\Factory\LoggerFactory; -use Alltube\Factory\SessionFactory; -use Alltube\Factory\ViewFactory; -use Alltube\Middleware\CspMiddleware; -use Alltube\Middleware\LinkHeaderMiddleware; -use Alltube\Middleware\LocaleMiddleware; -use Alltube\Middleware\RouterPathMiddleware; -use Slim\App; -use Slim\Container; if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) { header('Location: ' . str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI'])); @@ -27,77 +14,6 @@ try { // Create app. $app = new App(); - /** @var Container $container */ - $container = $app->getContainer(); - - // Config. - $container['config'] = ConfigFactory::create($container); - - // Session. - $container['session'] = SessionFactory::create($container); - - // Locales. - $container['locale'] = LocaleManagerFactory::create($container); - - // Smarty. - $container['view'] = ViewFactory::create($container); - - // Logger. - $container['logger'] = LoggerFactory::create($container); - - // Middlewares. - $app->add(new LocaleMiddleware($container)); - $app->add(new CspMiddleware($container)); - $app->add(new LinkHeaderMiddleware($container)); - $app->add(new RouterPathMiddleware($container)); - - // Controllers. - $frontController = new FrontController($container); - $jsonController = new JsonController($container); - $downloadController = new DownloadController($container); - - // Error handling. - $container['errorHandler'] = [$frontController, 'error']; - $container['phpErrorHandler'] = [$frontController, 'error']; - $container['notFoundHandler'] = [$frontController, 'notFound']; - $container['notAllowedHandler'] = [$frontController, 'notAllowed']; - - // Routes. - $app->get( - '/', - [$frontController, 'index'] - )->setName('index'); - - $app->get( - '/extractors', - [$frontController, 'extractors'] - )->setName('extractors'); - - $app->any( - '/info', - [$frontController, 'info'] - )->setName('info'); - - $app->any( - '/watch', - [$frontController, 'info'] - ); - - $app->any( - '/download', - [$downloadController, 'download'] - )->setName('download'); - - $app->get( - '/locale/{locale}', - [$frontController, 'locale'] - )->setName('locale'); - - $app->get( - '/json', - [$jsonController, 'json'] - )->setName('json'); - $app->run(); } catch (Throwable $e) { ErrorHandler::handle($e);