|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Di; |
|
6
|
|
|
|
|
7
|
|
|
use Ray\Aop\BindInterface; |
|
8
|
|
|
use Ray\Aop\CompilerInterface; |
|
9
|
|
|
|
|
10
|
|
|
use function array_keys; |
|
11
|
|
|
use function implode; |
|
12
|
|
|
use function method_exists; |
|
13
|
|
|
use function sprintf; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @codeCoverageIgnore |
|
17
|
|
|
*/ |
|
18
|
|
|
final class SpyCompiler implements CompilerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritDoc} |
|
22
|
|
|
* |
|
23
|
|
|
* @psalm-suppress InvalidReturnType |
|
24
|
|
|
* @template T of object |
|
25
|
|
|
*/ |
|
26
|
|
|
public function newInstance(string $class, array $args, BindInterface $bind) |
|
27
|
|
|
{ |
|
28
|
|
|
// never called // @phpstan-ignore-line |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Return "logging" class name |
|
33
|
|
|
* |
|
34
|
|
|
* Dummy classes are used for logging and don't really exist. |
|
35
|
|
|
* So the code breaks the QA rules as shown below. |
|
36
|
|
|
* NOTE: psalm-suppress is acceptable here for dummy/logging infrastructure |
|
37
|
|
|
* |
|
38
|
|
|
* @psalm-suppress MoreSpecificReturnType |
|
39
|
|
|
* @psalm-suppress LessSpecificReturnStatement |
|
40
|
|
|
*/ |
|
41
|
|
|
public function compile(string $class, BindInterface $bind): string |
|
42
|
|
|
{ |
|
43
|
|
|
if ($this->hasNoBinding($class, $bind)) { |
|
44
|
|
|
return $class; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $class . $this->getInterceptors($bind); // @phpstan-ignore-line |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param class-string $class |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
|
|
private function hasNoBinding(string $class, BindInterface $bind): bool |
|
54
|
|
|
{ |
|
55
|
|
|
$hasMethod = $this->hasBoundMethod($class, $bind); |
|
56
|
|
|
|
|
57
|
|
|
return ! $bind->getBindings() && ! $hasMethod; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param class-string $class |
|
|
|
|
|
|
62
|
|
|
*/ |
|
63
|
|
|
private function hasBoundMethod(string $class, BindInterface $bind): bool |
|
64
|
|
|
{ |
|
65
|
|
|
$bindingMethods = array_keys($bind->getBindings()); |
|
|
|
|
|
|
66
|
|
|
$hasMethod = false; |
|
67
|
|
|
foreach ($bindingMethods as $bindingMethod) { |
|
68
|
|
|
if (method_exists($class, $bindingMethod)) { |
|
69
|
|
|
$hasMethod = true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $hasMethod; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function getInterceptors(BindInterface $bind): string |
|
77
|
|
|
{ |
|
78
|
|
|
$bindings = $bind->getBindings(); |
|
79
|
|
|
if (! $bindings) { |
|
|
|
|
|
|
80
|
|
|
return ''; // @codeCoverageIgnore |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$log = ' (aop)'; |
|
84
|
|
|
foreach ($bindings as $method => $interceptors) { |
|
85
|
|
|
/** |
|
86
|
|
|
* @phpstan-var array<string> $interceptors |
|
87
|
|
|
* @psalm-ignore-var |
|
88
|
|
|
*/ |
|
89
|
|
|
$log .= sprintf( |
|
90
|
|
|
' +%s(%s)', |
|
91
|
|
|
$method, |
|
92
|
|
|
implode(', ', $interceptors) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $log; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|