-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap.php
67 lines (55 loc) · 1.8 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
<?php
declare(strict_types=1);
use Game\Engine\DBConnection;
use League\Container\Container;
use League\Container\ReflectionContainer;
use Psr\Container\ContainerInterface;
require_once __DIR__ . '/vendor/autoload.php';
if (!file_exists(__DIR__ . '/installed')) {
exit('Please, perform installation before running the app');
}
const PROJECT_ROOT = __DIR__;
final class DI
{
private static ?ContainerInterface $container = null;
public static function init(): void
{
if (self::$container !== null) {
return ;
}
self::$container = new Container();
self::$container->defaultToShared(true);
self::$container->delegate(new ReflectionContainer(true));
// configure db
$config = require __DIR__ . '/config.php';
self::$container->add(DBConnection::class)
->addArgument($config['dbHost'])
->addArgument($config['dbName'])
->addArgument($config['dbUser'])
->addArgument($config['dbPass']);
// configure template engine
self::$container->add(\Twig\Loader\FilesystemLoader::class)
->addArgument(PROJECT_ROOT . '/src/UI/Template');
self::$container->add(\Twig\Environment::class)
->addArgument(\Twig\Loader\FilesystemLoader::class)
->addArgument([
'cache' => PROJECT_ROOT . '/var/twig-cache',
'strict_variables' => true,
'auto_reload' => true,
]);
}
/**
* @template T of object
*
* @param class-string<T> $id
* @return T
*/
public static function getService(string $id): object
{
if (self::$container === null) {
self::init();
}
// @phpstan-ignore-next-line
return self::$container->get($id);
}
}