|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\ClassResolver\CustomService\CustomServiceResolver; |
|
8
|
|
|
use Gacela\Framework\ClassResolver\CustomService\DocBlockParser; |
|
9
|
|
|
use Gacela\Framework\ClassResolver\CustomService\MissingMethodException; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
|
|
12
|
|
|
use function is_string; |
|
13
|
|
|
|
|
14
|
|
|
trait CustomServicesResolverAwareTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var array<string,?object> */ |
|
17
|
|
|
private array $customServices = []; |
|
18
|
|
|
|
|
19
|
1 |
|
public function __call(string $method, array $arguments = []): ?object |
|
20
|
|
|
{ |
|
21
|
1 |
|
if (!isset($this->customServices[$method])) { |
|
22
|
1 |
|
$className = $this->getClassFromDocComment($method); |
|
23
|
1 |
|
$resolvableType = $this->normalizeResolvableType($className); |
|
24
|
|
|
|
|
25
|
1 |
|
$this->customServices[$method] = (new CustomServiceResolver($resolvableType)) |
|
26
|
1 |
|
->resolve($className); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
return $this->customServices[$method]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return class-string |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
1 |
|
private function getClassFromDocComment(string $method): string |
|
36
|
|
|
{ |
|
37
|
1 |
|
$reflectionClass = new ReflectionClass(static::class); |
|
38
|
1 |
|
$docBlock = (string)$reflectionClass->getDocComment(); |
|
39
|
|
|
|
|
40
|
1 |
|
$repositoryClass = (new DocBlockParser())->getClassFromMethod($docBlock, $method); |
|
41
|
|
|
|
|
42
|
1 |
|
if (class_exists($repositoryClass)) { |
|
43
|
1 |
|
return $repositoryClass; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
throw MissingMethodException::missingOverriding($method, static::class); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
private function normalizeResolvableType(string $resolvableType): string |
|
50
|
|
|
{ |
|
51
|
|
|
/** @var list<string> $resolvableTypeParts */ |
|
52
|
1 |
|
$resolvableTypeParts = explode('\\', ltrim($resolvableType, '\\')); |
|
53
|
1 |
|
$normalizedResolvableType = end($resolvableTypeParts); |
|
54
|
|
|
|
|
55
|
1 |
|
return is_string($normalizedResolvableType) |
|
56
|
1 |
|
? $normalizedResolvableType |
|
57
|
1 |
|
: $resolvableType; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|