-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
85 lines (60 loc) · 2.28 KB
/
index.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
<?php
use Tracy\Debugger;
use App\Config;
use App\Utils\Env;
use App\User;
use App\Request;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use App\Controller\Error;
require_once ('vendor/autoload.php');
// loading configuration
$config = new Config();
$config->load('Config/global.php');
$envUtils = new Env();
if ($envUtils->getEnv('ENVIRONMENT') !== false) {
$config->load('Config/'. $envUtils->getEnv('ENVIRONMENT') .'.php');
}
// starting debugger
if ($config->get('env.showDebbuger')) {
Debugger::enable(Debugger::DEVELOPMENT);
}
// matching requests
/** @var App\Request $request */
$request = Request::createFromGlobals();
$sessionStorage = new NativeSessionStorage([ 'cookie_lifetime' => 60 * 15]);
$request->setSession(new Session($sessionStorage));
$user = new User($request);
$routes = new RouteCollection();
$routes->add('index', new Route('/', ['_controller' => 'Index']));
$routes->add('logout', new Route('/logout', ['_controller' => 'Logout']));
$login = new Route('/login', ['_controller' => 'Login']);
$login->setMethods(['GET']);
$routes->add('login', $login);
$loginPost = new Route('/login', ['_controller' => 'Login']);
$loginPost->setMethods(['POST']);
$routes->add('postLogin', $loginPost);
$context = new RequestContext();
$context->fromRequest($request);
$router = new UrlMatcher($routes, $context);
try {
$match = $router->matchRequest($request);
// calling controller
$calledControllerName = 'App\Controller\\' . $match['_controller'];
if (class_exists($calledControllerName)) {
/** @var \App\Controller $controller */
$controller = new $calledControllerName($config, $request, $match);
} else {
throw new ResourceNotFoundException('Controller '. $match['_controller'] .' not found.');
}
} catch (ResourceNotFoundException $e) {
$controller = new Error($config, $request, []);
}
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = $controller->run();
$response->send();