|
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
|
|
|
use Ray\Di\Bindings\AopInfo; |
|
10
|
|
|
|
|
11
|
|
|
use function array_keys; |
|
12
|
|
|
use function method_exists; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @codeCoverageIgnore |
|
16
|
|
|
*/ |
|
17
|
|
|
final class SpyCompiler implements CompilerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritDoc} |
|
21
|
|
|
* |
|
22
|
|
|
* @psalm-suppress InvalidReturnType |
|
23
|
|
|
* @template T of object |
|
24
|
|
|
*/ |
|
25
|
|
|
public function newInstance(string $class, array $args, BindInterface $bind) |
|
26
|
|
|
{ |
|
27
|
|
|
// never called // @phpstan-ignore-line |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Return "logging" class name |
|
32
|
|
|
* |
|
33
|
|
|
* Dummy classes are used for logging and don't really exist. |
|
34
|
|
|
* So the code breaks the QA rules as shown below. |
|
35
|
|
|
* NOTE: psalm-suppress is acceptable here for dummy/logging infrastructure |
|
36
|
|
|
* |
|
37
|
|
|
* @psalm-suppress MoreSpecificReturnType |
|
38
|
|
|
* @psalm-suppress LessSpecificReturnStatement |
|
39
|
|
|
*/ |
|
40
|
|
|
public function compile(string $class, BindInterface $bind): string |
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->hasNoBinding($class, $bind)) { |
|
43
|
|
|
return $class; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$aopInfo = $this->getInterceptors($bind); |
|
47
|
|
|
return $class . (string) $aopInfo; // @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
|
|
|
public function getAopInfo(BindInterface $bind): AopInfo |
|
77
|
|
|
{ |
|
78
|
|
|
$bindings = $bind->getBindings(); |
|
79
|
|
|
return new AopInfo($bindings); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function getInterceptors(BindInterface $bind): AopInfo |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->getAopInfo($bind); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: