-
Notifications
You must be signed in to change notification settings - Fork 13
/
bootstrap.php
117 lines (97 loc) · 3.93 KB
/
bootstrap.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
<?php
require_once __DIR__.'/vendor/autoload.php';
use Application\ErrorHandler;
use Application\LoggerHandler;
use Application\Repository\BlogPostRepository;
use Framework\ControllerFactory;
use Framework\ControllerListener;
use Framework\EventManager\EventManager;
use Framework\HttpKernel;
use Framework\KernelEvents;
use Framework\RouterListener;
use Framework\Routing\Router;
use Framework\Routing\Loader\CompositeFileLoader;
use Framework\Routing\Loader\PhpFileLoader;
use Framework\Routing\Loader\XmlFileLoader;
use Framework\ServiceLocator\ServiceLocator;
use Framework\Session\Driver\NativeDriver;
use Framework\Session\Session;
use Framework\Templating\TwigRendererAdapter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Symfony\Component\Yaml\Yaml;
$settings = Yaml::parse(file_get_contents(__DIR__.'/app/config/settings.yml'));
$dic = new ServiceLocator($settings['parameters']);
$dic->setParameter('database.options', [
\PDO::ATTR_AUTOCOMMIT => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
]);
$dic->setParameter('router.file', __DIR__.'/app/config/routes.xml');
$dic->setParameter('app.views_dir', __DIR__.'/app/views');
$dic->setParameter('twig.options', [
'cache' => __DIR__.'/../app/cache/twig',
'debug' => true,
]);
$dic->setParameter('logger.log_file', __DIR__.'/app/cache/app.log');
$dic->setParameter('session.options', [
'session.name' => 'lpdim2016',
'session.save_path' => __DIR__.'/app/sessions',
]);
$dic->register('repository.blog_post', function (ServiceLocator $dic) {
return new BlogPostRepository($dic->getService('database'));
});
$dic->register('database', function (ServiceLocator $dic) {
return new \PDO(
$dic->getParameter('database.dsn'),
$dic->getParameter('database.user'),
$dic->getParameter('database.password'),
$dic->getParameter('database.options')
);
});
$dic->register('twig', function (ServiceLocator $dic) {
return new \Twig_Environment(
new \Twig_Loader_Filesystem($dic->getParameter('app.views_dir')),
$dic->getParameter('twig.options')
);
});
$dic->register('renderer', function (ServiceLocator $dic) {
return new TwigRendererAdapter($dic->getService('twig'));
});
$dic->register('router', function (ServiceLocator $dic) {
$loader = new CompositeFileLoader();
$loader->add(new PhpFileLoader());
$loader->add(new XmlFileLoader());
return new Router($dic->getParameter('router.file'), $loader);
});
$dic->register('error_handler', function (ServiceLocator $dic) {
return new ErrorHandler(
$dic->getService('renderer'),
$dic->getParameter('app.environment'),
$dic->getParameter('app.debug')
);
});
$dic->register('session', function (ServiceLocator $dic) {
return new Session(new NativeDriver(), $dic->getParameter('session.options'));
});
$dic->register('logger', function (ServiceLocator $dic) {
$level = $dic->getParameter('app.debug') ? Logger::WARNING : Logger::CRITICAL;
$logger = new Logger('blog');
$logger->pushHandler(new StreamHandler($dic->getParameter('logger.log_file'), $level));
return $logger;
});
$dic->register('event_manager', function (ServiceLocator $dic) {
$manager = new EventManager();
$manager->addEventListener(KernelEvents::REQUEST, [ new RouterListener($dic->getService('router')), 'onKernelRequest' ]);
$manager->addEventListener(KernelEvents::CONTROLLER, [ new ControllerListener($dic), 'onKernelController' ]);
$manager->addEventListener(KernelEvents::EXCEPTION, [ new LoggerHandler($dic->getService('logger')), 'onKernelException' ]);
$manager->addEventListener(KernelEvents::EXCEPTION, [ $dic->getService('error_handler'), 'onKernelException' ]);
return $manager;
});
$dic->register('http_kernel', function (ServiceLocator $dic) {
return new HttpKernel(
$dic->getService('event_manager'),
new ControllerFactory()
);
});
return $dic;