1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\ClassResolver; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\ClassResolver\ClassNameFinder\ClassNameFinderInterface; |
8
|
|
|
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal; |
9
|
|
|
use Gacela\Framework\ClassResolver\InstanceCreator\InstanceCreator; |
10
|
|
|
use Gacela\Framework\Config\Config; |
11
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface; |
12
|
|
|
|
13
|
|
|
use function is_array; |
14
|
|
|
use function is_object; |
15
|
|
|
|
16
|
|
|
abstract class AbstractClassResolver |
17
|
|
|
{ |
18
|
|
|
/** @var array<string,null|object> */ |
19
|
|
|
private static array $cachedInstances = []; |
20
|
|
|
|
21
|
|
|
private ?ClassNameFinderInterface $classNameFinder = null; |
22
|
|
|
|
23
|
|
|
private ?GacelaConfigFileInterface $gacelaFileConfig = null; |
24
|
|
|
|
25
|
|
|
private ?InstanceCreator $instanceCreator = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @internal remove all cached instances: facade, factory, config, dependency-provider |
29
|
|
|
*/ |
30
|
8 |
|
public static function resetCache(): void |
31
|
|
|
{ |
32
|
8 |
|
self::$cachedInstances = []; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param object|class-string $caller |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
abstract public function resolve($caller): ?object; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param object|class-string $caller |
|
|
|
|
42
|
|
|
*/ |
43
|
28 |
|
public function doResolve($caller): ?object |
44
|
|
|
{ |
45
|
28 |
|
$classInfo = ClassInfo::from($caller, $this->getResolvableType()); |
46
|
|
|
|
47
|
28 |
|
$cacheKey = $classInfo->getCacheKey(); |
48
|
|
|
|
49
|
28 |
|
$resolvedClass = $this->resolveCached($cacheKey); |
50
|
28 |
|
if ($resolvedClass !== null) { |
51
|
8 |
|
return $resolvedClass; |
52
|
|
|
} |
53
|
|
|
|
54
|
21 |
|
$resolvedClassName = $this->findClassName($classInfo); |
55
|
21 |
|
if ($resolvedClassName === null) { |
56
|
|
|
// Try again with its parent class |
57
|
4 |
|
if (is_object($caller)) { |
58
|
4 |
|
$parentClass = get_parent_class($caller); |
59
|
4 |
|
if ($parentClass !== false) { |
60
|
4 |
|
return $this->doResolve($parentClass); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
20 |
|
self::$cachedInstances[$cacheKey] = $this->createInstance($resolvedClassName); |
68
|
|
|
|
69
|
20 |
|
return self::$cachedInstances[$cacheKey]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
abstract protected function getResolvableType(): string; |
73
|
|
|
|
74
|
28 |
|
private function resolveCached(string $cacheKey): ?object |
75
|
|
|
{ |
76
|
28 |
|
return AnonymousGlobal::getByKey($cacheKey) |
77
|
27 |
|
?? self::$cachedInstances[$cacheKey] |
78
|
|
|
?? null; |
79
|
|
|
} |
80
|
|
|
|
81
|
21 |
|
private function findClassName(ClassInfo $classInfo): ?string |
82
|
|
|
{ |
83
|
21 |
|
return $this->getClassNameFinder()->findClassName( |
84
|
|
|
$classInfo, |
85
|
21 |
|
$this->getPossibleResolvableTypes() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
21 |
|
private function getClassNameFinder(): ClassNameFinderInterface |
90
|
|
|
{ |
91
|
21 |
|
if ($this->classNameFinder === null) { |
92
|
21 |
|
$this->classNameFinder = (new ClassResolverFactory()) |
93
|
21 |
|
->createClassNameFinder(); |
94
|
|
|
} |
95
|
|
|
|
96
|
21 |
|
return $this->classNameFinder; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Allow overriding gacela suffixes resolvable types. |
101
|
|
|
* |
102
|
|
|
* @return list<string> |
103
|
|
|
*/ |
104
|
21 |
|
private function getPossibleResolvableTypes(): array |
105
|
|
|
{ |
106
|
21 |
|
$suffixTypes = $this->getGacelaConfigFile()->getSuffixTypes(); |
107
|
|
|
|
108
|
21 |
|
$resolvableTypes = $suffixTypes[$this->getResolvableType()] ?? $this->getResolvableType(); |
109
|
|
|
|
110
|
21 |
|
return is_array($resolvableTypes) ? $resolvableTypes : [$resolvableTypes]; |
111
|
|
|
} |
112
|
|
|
|
113
|
20 |
|
private function createInstance(string $resolvedClassName): ?object |
114
|
|
|
{ |
115
|
20 |
|
if ($this->instanceCreator === null) { |
116
|
20 |
|
$this->instanceCreator = new InstanceCreator($this->getGacelaConfigFile()); |
117
|
|
|
} |
118
|
|
|
|
119
|
20 |
|
return $this->instanceCreator->createByClassName($resolvedClassName); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
21 |
|
private function getGacelaConfigFile(): GacelaConfigFileInterface |
123
|
|
|
{ |
124
|
21 |
|
if ($this->gacelaFileConfig === null) { |
125
|
21 |
|
$this->gacelaFileConfig = Config::getInstance() |
126
|
21 |
|
->getFactory() |
127
|
21 |
|
->createGacelaFileConfig(); |
128
|
|
|
} |
129
|
|
|
|
130
|
21 |
|
return $this->gacelaFileConfig; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|