-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainer.php
More file actions
128 lines (105 loc) · 3.15 KB
/
Container.php
File metadata and controls
128 lines (105 loc) · 3.15 KB
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
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Sirius;
use Sirius\database\DatabaseResolver;
use Sirius\database\EntityManager;
use Sirius\utils\JsonParser;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class Container
{
private static ?Container $instance = null;
private ?array $services = null;
private ?Environment $twig = null;
private ?EntityManager $entityManager = null;
private ?string $rootPath = null;
private function __construct()
{
$this->setRootDir();
if (file_exists($this->rootPath.'/config/services.json')) {
$this->services = JsonParser::parseFile($this->rootPath . '/config/services.json');
}
$this->setTwig();
}
private function setRootDir(): ?string
{
if (null === $this->rootPath) {
$r = new \ReflectionObject($this);
if (!is_file($dir = $r->getFileName())) {
throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".', $r->name));
}
$dir = \dirname(\dirname($dir));
while (!is_file($dir.'/composer.json')) {
if ($dir === \dirname($dir)) {
break;
}
$dir = \dirname($dir);
}
$this->rootPath = $dir;
define('ROOT_DIR', $dir);
}
return $this->rootPath;
}
public static function getInstance():Container
{
if (self::$instance == null)
{
self::$instance = new Container();
}
return self::$instance;
}
public function getEntityManager(): ?EntityManager
{
if (!$this->entityManager) {
try {
$this->entityManager = DatabaseResolver::instantiateManager();
} catch (\Exception $e) {
throw $e;
}
}
return $this->entityManager;
}
public function setRootPath(string $rootPath)
{
$this->rootPath = $rootPath;
}
/**
* @return string
*/
public function getRootPath(): string
{
return $this->rootPath;
}
private function setTwig()
{
if (is_dir($this->rootPath.'/templates/')) {
$loader = new FilesystemLoader($this->rootPath . '/templates/');
if (isset($_ENV['ENV']) && $_ENV['ENV'] === 'dev') {
$this->twig = new Environment($loader, [
'debug' => true
]);
$this->twig->addExtension(new \Twig\Extension\DebugExtension());
} else {
$this->twig = new Environment($loader, [
'debug' => false
]);
}
}
$this->setTwigServices();
}
private function setTwigServices()
{
if (isset ($this->services['twig']['extensions'])) {
foreach ($this->services['twig']['extensions'] as $extension) {
$class = $extension['class'];
$this->twig->addExtension(new $class());
}
}
}
/**
* @return Environment|null
*/
public function getTwig(): ?Environment
{
return $this->twig;
}
}