blob: 2cf64616cdcc578d3a1006d7c8d3db57d7aefad3 (
plain)
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
|
<?php
namespace Studip;
use DI\Container;
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
class DIContainer
{
/**
* The current globally available container.
*/
protected static ?Container $instance = null;
/**
* Get the globally available instance of the container.
*
* @return Container
* @throws \Exception
*/
public static function getInstance(): Container
{
if (static::$instance === null) {
$builder = static::createBuilder();
static::$instance = $builder->build();
}
return static::$instance;
}
/**
* Set the instance of the container.
*
* @param ContainerInterface|null $container
* @return ContainerInterface
*/
public static function setInstance(?ContainerInterface $container = null): ContainerInterface
{
return static::$instance = $container;
}
/**
* Set up the ContainerBuilder.
*/
protected static function createBuilder(): ContainerBuilder
{
$builder = new ContainerBuilder();
if (ENV === 'production') {
$builder->enableCompilation(
self::getCompilationPath(),
self::getCompilationClass()
);
}
$builder->useAttributes(true);
$builder->addDefinitions('lib/bootstrap-definitions.php');
$jsonapiSettings = require 'lib/classes/JsonApi/settings.php';
$jsonapiSettings($builder);
$jsonapiDependencies = require 'lib/classes/JsonApi/dependencies.php';
$jsonapiDependencies($builder);
return $builder;
}
/**
* Returns the path to the compiled container
*/
public static function getCompilationPath(): string
{
return $GLOBALS['TMP_PATH'];
}
/**
* Returns the class of the compiled container
*/
public static function getCompilationClass(): string
{
return 'CompiledContainer';
}
}
|