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