1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\DocBlockResolver; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\ClassResolver\Cache\GacelaCache; |
8
|
|
|
use Gacela\Framework\ClassResolver\ClassNameCacheInterface; |
9
|
|
|
use Gacela\Framework\ClassResolver\DocBlockService\CustomServicesCache; |
10
|
|
|
use Gacela\Framework\ClassResolver\DocBlockService\DocBlockParser; |
11
|
|
|
use Gacela\Framework\ClassResolver\DocBlockService\MissingClassDefinitionException; |
12
|
|
|
use Gacela\Framework\ClassResolver\DocBlockService\UseBlockParser; |
13
|
|
|
use Gacela\Framework\ClassResolver\InMemoryCache; |
14
|
|
|
use Gacela\Framework\Config\Config; |
15
|
|
|
use ReflectionClass; |
16
|
|
|
|
17
|
|
|
use function get_class; |
18
|
|
|
use function is_string; |
19
|
|
|
|
20
|
|
|
final class DocBlockResolvable |
21
|
|
|
{ |
22
|
|
|
/** @var array<string,string> [fileName => fileContent] cached */ |
23
|
|
|
private static array $fileContentCache = []; |
24
|
|
|
|
25
|
|
|
/** @var class-string */ |
|
|
|
|
26
|
|
|
private string $callerClass; |
27
|
|
|
|
28
|
|
|
/** @var class-string|string */ |
|
|
|
|
29
|
|
|
private string $callerParentClass; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param class-string $callerClass |
|
|
|
|
33
|
|
|
* @param class-string|string $callerParentClass |
34
|
|
|
*/ |
35
|
13 |
|
private function __construct(string $callerClass, string $callerParentClass) |
36
|
|
|
{ |
37
|
13 |
|
$this->callerClass = $callerClass; |
38
|
13 |
|
$this->callerParentClass = $callerParentClass; |
39
|
|
|
} |
40
|
|
|
|
41
|
13 |
|
public static function fromCaller(object $caller): self |
42
|
|
|
{ |
43
|
13 |
|
return new self( |
44
|
13 |
|
get_class($caller), |
45
|
13 |
|
get_parent_class($caller) ?: '' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
13 |
|
public function getClassName(string $method): string |
50
|
|
|
{ |
51
|
13 |
|
$cacheKey = $this->generateCacheKey($method); |
52
|
13 |
|
$cache = $this->createCustomServicesCache(); |
53
|
|
|
|
54
|
13 |
|
if (!$cache->has($cacheKey)) { |
55
|
11 |
|
$className = $this->getClassFromDoc($method); |
56
|
9 |
|
$cache->put($cacheKey, $className); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
60
|
11 |
|
return $cache->get($cacheKey); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
public function hasParentClass(): bool |
65
|
|
|
{ |
66
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
67
|
|
|
return $this->callerParentClass !== '' |
68
|
|
|
&& method_exists($this->callerParentClass, '__call'); |
69
|
|
|
} |
70
|
|
|
|
71
|
11 |
|
public function normalizeResolvableType(string $resolvableType): string |
72
|
|
|
{ |
73
|
|
|
/** @var list<string> $resolvableTypeParts */ |
74
|
11 |
|
$resolvableTypeParts = explode('\\', ltrim($resolvableType, '\\')); |
75
|
11 |
|
$normalizedResolvableType = end($resolvableTypeParts); |
76
|
|
|
|
77
|
11 |
|
return is_string($normalizedResolvableType) |
78
|
11 |
|
? $normalizedResolvableType |
79
|
11 |
|
: $resolvableType; |
80
|
|
|
} |
81
|
|
|
|
82
|
13 |
|
public function createCustomServicesCache(): ClassNameCacheInterface |
83
|
|
|
{ |
84
|
13 |
|
if (!$this->isProjectCacheEnabled()) { |
85
|
4 |
|
return new InMemoryCache(CustomServicesCache::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
9 |
|
return new CustomServicesCache( |
89
|
9 |
|
Config::getInstance()->getCacheDir() |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return class-string |
|
|
|
|
95
|
|
|
*/ |
96
|
11 |
|
private function getClassFromDoc(string $method): string |
97
|
|
|
{ |
98
|
11 |
|
$reflectionClass = new ReflectionClass($this->callerClass); |
99
|
11 |
|
$className = $this->searchClassOverDocBlock($reflectionClass, $method); |
100
|
11 |
|
if (class_exists($className)) { |
101
|
|
|
return $className; |
102
|
|
|
} |
103
|
11 |
|
$className = $this->searchClassOverUseStatements($reflectionClass, $className); |
104
|
11 |
|
if (class_exists($className)) { |
105
|
9 |
|
return $className; |
106
|
|
|
} |
107
|
2 |
|
throw MissingClassDefinitionException::missingDefinition($this->callerClass, $method, $className); |
108
|
|
|
} |
109
|
|
|
|
110
|
11 |
|
private function searchClassOverDocBlock(ReflectionClass $reflectionClass, string $method): string |
111
|
|
|
{ |
112
|
11 |
|
$docBlock = (string)$reflectionClass->getDocComment(); |
113
|
|
|
|
114
|
11 |
|
return (new DocBlockParser())->getClassFromMethod($docBlock, $method); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Look the uses, to find the fully-qualified class name for the className. |
119
|
|
|
*/ |
120
|
11 |
|
private function searchClassOverUseStatements(ReflectionClass $reflectionClass, string $className): string |
121
|
|
|
{ |
122
|
11 |
|
$fileName = (string)$reflectionClass->getFileName(); |
123
|
11 |
|
if (!isset(self::$fileContentCache[$fileName])) { |
124
|
9 |
|
self::$fileContentCache[$fileName] = (string)file_get_contents($fileName); |
125
|
|
|
} |
126
|
11 |
|
$phpFile = self::$fileContentCache[$fileName]; |
127
|
|
|
|
128
|
11 |
|
return (new UseBlockParser())->getUseStatement($className, $phpFile); |
129
|
|
|
} |
130
|
|
|
|
131
|
13 |
|
private function generateCacheKey(string $method): string |
132
|
|
|
{ |
133
|
13 |
|
return $this->callerClass . '::' . $method; |
134
|
|
|
} |
135
|
|
|
|
136
|
13 |
|
private function isProjectCacheEnabled(): bool |
137
|
|
|
{ |
138
|
13 |
|
return (new GacelaCache(Config::getInstance())) |
139
|
13 |
|
->isProjectCacheEnabled(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|