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