forked from Rudloff/alltube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.php
128 lines (105 loc) · 3.48 KB
/
App.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?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\DebugBarFactory;
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 DebugBar\DebugBarException;
use Slim\Container;
use SmartyException;
class App extends \Slim\App
{
/**
* App constructor.
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
* @throws DebugBarException
*/
public function __construct()
{
parent::__construct();
/** @var Container $container */
$container = $this->getContainer();
$container['root_path'] = $this->getRootPath();
// Config.
$container['config'] = ConfigFactory::create($container);
// Session.
$container['session'] = SessionFactory::create($container);
// Locales.
$container['locale'] = LocaleManagerFactory::create($container);
// Logger.
$container['logger'] = LoggerFactory::create($container);
if ($container->get('config')->debug) {
// Debug bar.
$container['debugbar'] = DebugBarFactory::create($container);
}
// Smarty.
$container['view'] = ViewFactory::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');
}
/**
* @return string|null
*/
private function getRootPath(): ?string
{
// realpath() can return false but we prefer using null.
if ($rootPath = realpath(__DIR__ . '/../')) {
return $rootPath;
}
return null;
}
}