1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Container; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @internal |
11
|
|
|
*/ |
12
|
|
|
final class Locator implements LocatorInterface |
13
|
|
|
{ |
14
|
|
|
private static ?Locator $instance = null; |
15
|
|
|
|
16
|
|
|
/** @var array<string, mixed> */ |
17
|
|
|
private array $instanceCache = []; |
18
|
|
|
|
19
|
|
|
private ContainerInterface $container; |
20
|
|
|
|
21
|
7 |
|
private function __construct( |
22
|
|
|
?ContainerInterface $container = null, |
23
|
|
|
) { |
24
|
7 |
|
$this->container = $container ?? new Container(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @codeCoverageIgnore |
29
|
|
|
*/ |
30
|
|
|
private function __clone() |
31
|
|
|
{ |
32
|
|
|
} |
33
|
|
|
|
34
|
51 |
|
public static function resetInstance(): void |
35
|
|
|
{ |
36
|
51 |
|
self::$instance = null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @template T |
41
|
|
|
* |
42
|
|
|
* @param class-string<T> $key |
|
|
|
|
43
|
|
|
* @param T $value |
44
|
|
|
*/ |
45
|
1 |
|
public static function addSingleton(string $key, mixed $value): void |
46
|
|
|
{ |
47
|
1 |
|
self::getInstance()->add($key, $value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @template T |
52
|
|
|
* |
53
|
|
|
* @param class-string<T> $className |
|
|
|
|
54
|
|
|
* |
55
|
|
|
* @return T|null |
56
|
|
|
*/ |
57
|
9 |
|
public static function getSingleton(string $className, ?Container $container = null) |
58
|
|
|
{ |
59
|
9 |
|
return self::getInstance($container)->get($className); |
60
|
|
|
} |
61
|
|
|
|
62
|
11 |
|
public static function getInstance(?Container $container = null): self |
63
|
|
|
{ |
64
|
11 |
|
if (self::$instance === null) { |
65
|
7 |
|
self::$instance = new self($container); |
66
|
|
|
} |
67
|
|
|
|
68
|
11 |
|
return self::$instance; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @template T |
73
|
|
|
* |
74
|
|
|
* @param class-string<T> $className |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @return T|null |
77
|
|
|
*/ |
78
|
11 |
|
public function get(string $className) |
79
|
|
|
{ |
80
|
11 |
|
if (isset($this->instanceCache[$className])) { |
81
|
|
|
/** @var T $instance */ |
82
|
7 |
|
$instance = $this->instanceCache[$className]; |
83
|
|
|
|
84
|
7 |
|
return $instance; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @var T|null $locatedInstance */ |
88
|
6 |
|
$locatedInstance = AnonymousGlobal::getByClassName($className) |
89
|
6 |
|
?? $this->container->get($className); |
90
|
|
|
|
91
|
6 |
|
$this->add($className, $locatedInstance); |
92
|
|
|
|
93
|
6 |
|
return $locatedInstance; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @template T |
98
|
|
|
* |
99
|
|
|
* @param class-string<T> $key |
|
|
|
|
100
|
|
|
* @param T|null $value |
101
|
|
|
*/ |
102
|
7 |
|
private function add(string $key, mixed $value = null): self |
103
|
|
|
{ |
104
|
7 |
|
$this->instanceCache[$key] = $value; |
105
|
|
|
|
106
|
7 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|