-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.php
73 lines (68 loc) · 2.21 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
<?php
use Controllers\AbstractController;
use Controllers\AppController;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use Pimple\Container;
use Twig\Environment;
use Twig\Extension\DebugExtension;
use Twig\Loader\FilesystemLoader;
const SRC_DIR = __DIR__ . '/src/';
// Autoloader
require 'vendor/autoload.php';
spl_autoload_register(function ($class) {
include SRC_DIR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
});
// Container
$container = new Container();
$container['config'] = require SRC_DIR . 'config.php';
$container['db'] = function($c) {
$db = $c['config']['database'];
$url = 'mysql:host=' . $db['host'] . ';dbname=' . $db['name'] . ';charset=utf8mb4';
return new PDO($url, $db['user'], $db['pass'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
};
$container['twig'] = function ($c) {
$loader = new FilesystemLoader(SRC_DIR . 'views');
$twig = new Environment($loader, [
'cache' => __DIR__ . '/cache/views',
'auto_reload' => true,
'debug' => $c['config']['debug'],
]);
$twig->addGlobal('app', $c);
if ($c['config']['debug']) {
$twig->addExtension(new DebugExtension());
}
return $twig;
};
if ($container['config']['debug']) {
error_reporting(E_ALL);
ini_set('display_errors', '1');
}
// Router
$dispatcher = \FastRoute\simpleDispatcher(function(RouteCollector $r) {
$r->addRoute('GET', '/', [AppController::class, 'index']);
$r->addRoute('GET', '/about[/]', [AppController::class, 'about']);
});
$uri = $_SERVER['REQUEST_URI'];
$pos = strpos($uri, '?');
if ($pos !== false) {
$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);
$routeInfo = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $uri);
switch ($routeInfo[0]) {
case Dispatcher::NOT_FOUND:
http_response_code(404);
echo $container['twig']->render('404.twig');
break;
case Dispatcher::FOUND:
[$class, $method] = $routeInfo[1];
/** @var AbstractController $instance */
$instance = new $class;
$instance->setContainer($container);
call_user_func_array([$instance, $method], [$routeInfo[2]]);
break;
}