-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
71 lines (60 loc) · 2.38 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
<?php
require 'vendor/autoload.php';
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\Helper\SlotsHelper;
use Symfony\Component\Templating\Helper\AssetsHelper;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Stark\Framework\Config;
use Stark\Framework\Router;
use Stark\Framework\ErrorHandler;
use Stark\Framework\Exception\HttpExceptionInterface;
// Creating the DIC
$container = new ContainerBuilder();
// Importing configuration data to the container
$config = new Config(__DIR__ . '/config.ini');
$config->import($container);
// Define error handling behavior
ini_set('display_errors', 0);
if ($container->getParameter('app.debug')) {
ErrorHandler::register(-1);
} else {
ErrorHandler::register(E_ALL ^ E_NOTICE);
}
// Creating the View object
$loader = new FilesystemLoader(__DIR__ . '/views/%name%');
$view = new PhpEngine(new TemplateNameParser(), $loader);
$view->set(new SlotsHelper());
$view->set(new AssetsHelper(__DIR__ . '/assets/', $container->getParameter('app.url') . '/assets/'));
$container->set('view', $view);
// Creating the Database object
$connection = new Connection(
$container->getParameter('database.driver'),
$container->getParameter('database.host'),
$container->getParameter('database.name'),
$container->getParameter('database.user'),
$container->getParameter('database.password')
);
$container->set('db', new Manager($connection));
// Log manager
$log = new Logger($container->getParameter('app.name'));
$log->pushHandler(new StreamHandler(__DIR__ . '/error.log', Logger::WARNING));
$container->set('log', $log);
// Creating the Request object
$request = Request::createFromGlobals();
$container->set('request', $request);
// Parse the request path and dispatch available routes
try {
$router = new Router($request->getPathInfo(), $container);
$response = $router->dispatch();
} catch (HttpExceptionInterface $e) {
$message = sprintf('Error %s at %s in line %s: %s', $e->getCode(), $e->getFile(), $e->getLine(), $e->getMessage());
$log->addError($message);
$response = new Response($e->getMessage(), $e->getStatusCode());
}
$response->send();