2020-10-22 21:08:40 +00:00
|
|
|
<?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;
|
2021-02-06 14:00:26 +00:00
|
|
|
use Alltube\Factory\DebugBarFactory;
|
2020-10-22 21:08:40 +00:00
|
|
|
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;
|
2021-02-06 14:00:26 +00:00
|
|
|
use DebugBar\DebugBarException;
|
2020-10-22 21:08:40 +00:00
|
|
|
use Slim\Container;
|
|
|
|
use SmartyException;
|
|
|
|
|
|
|
|
class App extends \Slim\App
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* App constructor.
|
|
|
|
* @throws ConfigException
|
|
|
|
* @throws DependencyException
|
|
|
|
* @throws SmartyException
|
2021-02-06 14:00:26 +00:00
|
|
|
* @throws DebugBarException
|
2020-10-22 21:08:40 +00:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
/** @var Container $container */
|
|
|
|
$container = $this->getContainer();
|
|
|
|
|
2021-02-09 21:17:29 +00:00
|
|
|
$container['root_path'] = $this->getRootPath();
|
|
|
|
|
2020-10-22 21:08:40 +00:00
|
|
|
// Config.
|
|
|
|
$container['config'] = ConfigFactory::create($container);
|
|
|
|
|
|
|
|
// Session.
|
|
|
|
$container['session'] = SessionFactory::create($container);
|
|
|
|
|
|
|
|
// Locales.
|
|
|
|
$container['locale'] = LocaleManagerFactory::create($container);
|
|
|
|
|
|
|
|
// Logger.
|
|
|
|
$container['logger'] = LoggerFactory::create($container);
|
|
|
|
|
2021-02-06 14:00:26 +00:00
|
|
|
if ($container->get('config')->debug) {
|
|
|
|
// Debug bar.
|
|
|
|
$container['debugbar'] = DebugBarFactory::create($container);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Smarty.
|
|
|
|
$container['view'] = ViewFactory::create($container);
|
|
|
|
|
2020-10-22 21:08:40 +00:00
|
|
|
// Middlewares.
|
|
|
|
$this->add(new LocaleMiddleware($container));
|
|
|
|
$this->add(new CspMiddleware($container));
|
2022-02-20 12:55:44 +00:00
|
|
|
$this->add(new LinkHeaderMiddleware());
|
2020-10-22 21:08:40 +00:00
|
|
|
$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',
|
2022-02-27 22:32:08 +00:00
|
|
|
[$frontController, 'watch']
|
2020-10-22 21:08:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->any(
|
|
|
|
'/download',
|
|
|
|
[$downloadController, 'download']
|
|
|
|
)->setName('download');
|
|
|
|
|
|
|
|
$this->get(
|
|
|
|
'/locale/{locale}',
|
|
|
|
[$frontController, 'locale']
|
|
|
|
)->setName('locale');
|
|
|
|
|
|
|
|
$this->get(
|
|
|
|
'/json',
|
|
|
|
[$jsonController, 'json']
|
|
|
|
)->setName('json');
|
|
|
|
}
|
2021-02-09 21:17:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
private function getRootPath(): ?string
|
|
|
|
{
|
|
|
|
// realpath() can return false but we prefer using null.
|
|
|
|
if ($rootPath = realpath(__DIR__ . '/../')) {
|
|
|
|
return $rootPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-22 21:08:40 +00:00
|
|
|
}
|