1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Smr\Container; |
4
|
|
|
|
5
|
|
|
use DI\ContainerBuilder; |
6
|
|
|
use Dotenv\Dotenv; |
7
|
|
|
use mysqli; |
8
|
|
|
use Smr\Database; |
9
|
|
|
use Smr\DatabaseProperties; |
10
|
|
|
use Smr\Epoch; |
11
|
|
|
use Smr\SectorLock; |
12
|
|
|
use Smr\Session; |
13
|
|
|
use Smr\Template; |
14
|
|
|
use function DI\autowire; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A wrapper around the DI\Container functionality that will allow |
18
|
|
|
* static usage of container methods. |
19
|
|
|
*/ |
20
|
|
|
class DiContainer { |
21
|
|
|
|
22
|
|
|
private static DiContainer $instance; |
23
|
|
|
private readonly ResettableContainer|ResettableCompiledContainer $container; |
24
|
|
|
|
25
|
|
|
private function __construct(bool $enableCompilation) { |
26
|
|
|
$this->container = $this->buildContainer($enableCompilation); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return array<string, mixed> |
31
|
|
|
*/ |
32
|
|
|
private function getDefinitions(): array { |
33
|
|
|
return [ |
34
|
|
|
/* |
35
|
|
|
* mysqli is a 3rd party library, and we do not have control over its constructor. |
36
|
|
|
* In order for PHP-DI to construct an instance of a class, each constructor argument must |
37
|
|
|
* be able to be constructed. |
38
|
|
|
* PHP-DI cannot construct mysqli by itself, because all of its arguments are primitive types. |
39
|
|
|
* Therefore, we need to declare a provider factory for the container to use when constructing new instances. |
40
|
|
|
* |
41
|
|
|
* The factories themselves are able to use dependency injection as well, so we can provide the DatabaseProperties |
42
|
|
|
* typehint to make sure the container constructs an instance and provides it to the factory. |
43
|
|
|
*/ |
44
|
|
|
mysqli::class => function(DatabaseProperties $dbProperties): mysqli { |
45
|
|
|
return Database::mysqliFactory($dbProperties); |
46
|
|
|
}, |
47
|
|
|
Dotenv::class => function(): Dotenv { |
48
|
|
|
return Dotenv::createArrayBacked(CONFIG, 'env'); |
49
|
|
|
}, |
50
|
|
|
'DatabaseName' => function(DatabaseProperties $dbProperties): string { |
51
|
|
|
return $dbProperties->getDatabaseName(); |
52
|
|
|
}, |
53
|
|
|
'NPC_SCRIPT' => false, |
54
|
|
|
// Explicitly name all classes that are autowired, so we can take advantage of |
55
|
|
|
// the compiled container feature for a performance boost |
56
|
|
|
Epoch::class => autowire(), |
57
|
|
|
DatabaseProperties::class => autowire(), |
58
|
|
|
Database::class => autowire() |
59
|
|
|
->constructorParameter('dbName', \DI\get('DatabaseName')), |
60
|
|
|
SectorLock::class => autowire(), |
61
|
|
|
Session::class => autowire(), |
62
|
|
|
Template::class => autowire(), |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function buildContainer(bool $enableCompilation): ResettableContainer|ResettableCompiledContainer { |
67
|
|
|
$builder = new ContainerBuilder(ResettableContainer::class); |
68
|
|
|
$builder |
69
|
|
|
->addDefinitions($this->getDefinitions()) |
70
|
|
|
->useAnnotations(false) |
71
|
|
|
->useAutowiring(true); |
72
|
|
|
if ($enableCompilation) { |
73
|
|
|
// The CompiledContainer.php will be saved to the /tmp directory on the Docker container once |
74
|
|
|
// during its lifecycle (first request) |
75
|
|
|
$builder->enableCompilation('/tmp', containerParentClass: ResettableCompiledContainer::class); |
76
|
|
|
} |
77
|
|
|
return $builder->build(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create a new DI\Container instance. |
82
|
|
|
* This needs to be done once during a bootstrapping script, like bootstrap.php |
83
|
|
|
*/ |
84
|
|
|
public static function initialize(bool $enableCompilation): void { |
85
|
|
|
self::$instance = new self($enableCompilation); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retrieve the managed instance of $className, or construct a new instance with all dependencies. |
90
|
|
|
* @param string $className The name of the class to retrieve from the container. |
91
|
|
|
* @throws \DI\DependencyException |
92
|
|
|
* @throws \DI\NotFoundException |
93
|
|
|
*/ |
94
|
|
|
public static function get(string $className): mixed { |
95
|
|
|
return self::getContainer()->get($className); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Construct a fresh instance of $className. Dependencies will be retrieved from the container if they |
100
|
|
|
* are already managed, and created themselves if they are not. |
101
|
|
|
* @param string $className The name of the class to construct. |
102
|
|
|
* @throws \DI\DependencyException |
103
|
|
|
* @throws \DI\NotFoundException |
104
|
|
|
*/ |
105
|
|
|
public static function make(string $className): mixed { |
106
|
|
|
return self::getContainer()->make($className); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check if a managed instance of $className has been created. |
111
|
|
|
* If the container itself has not been initialized yet, will always return false. |
112
|
|
|
*/ |
113
|
|
|
public static function initialized(string $className): bool { |
114
|
|
|
return isset(self::$instance) && self::$instance->container->initialized($className); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return the raw dependency injection Container instance for more robust |
119
|
|
|
* container management operations. |
120
|
|
|
*/ |
121
|
|
|
public static function getContainer(): ResettableContainer|ResettableCompiledContainer { |
122
|
|
|
return self::$instance->container; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|