Move container creation to a new App class

This commit is contained in:
Pierre Rudloff 2020-10-22 23:08:40 +02:00
parent 3d2b518cb4
commit 7ad0040f60
2 changed files with 106 additions and 85 deletions

105
classes/App.php Normal file
View file

@ -0,0 +1,105 @@
<?php
namespace Alltube;
use Alltube\Controller\DownloadController;
use Alltube\Controller\FrontController;
use Alltube\Controller\JsonController;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
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\Container;
use SmartyException;
class App extends \Slim\App
{
/**
* App constructor.
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
*/
public function __construct()
{
parent::__construct();
/** @var Container $container */
$container = $this->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');
}
}

View file

@ -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);