1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Reflection\InjectionStrategy; |
5
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
7
|
|
|
InjectionStrategyInterface, |
8
|
|
|
Exception\InvalidArgumentException, |
9
|
|
|
Exception\PropertyCannotBeInjectedException |
10
|
|
|
}; |
11
|
|
|
use Innmind\Immutable\{ |
12
|
|
|
StreamInterface, |
13
|
|
|
Map |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
final class DelegationStrategy implements InjectionStrategyInterface |
17
|
|
|
{ |
18
|
|
|
private $strategies; |
19
|
|
|
private $cache; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param StreamInterface<InjectionStrategyInterface> $strategies |
|
|
|
|
23
|
|
|
*/ |
24
|
7 |
View Code Duplication |
public function __construct(StreamInterface $strategies) |
|
|
|
|
25
|
|
|
{ |
26
|
7 |
|
if ((string) $strategies->type() !== InjectionStrategyInterface::class) { |
27
|
|
|
throw new InvalidArgumentException; |
28
|
|
|
} |
29
|
|
|
|
30
|
7 |
|
$this->strategies = $strategies; |
31
|
7 |
|
$this->cache = new Map('string', InjectionStrategyInterface::class); |
32
|
7 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
1 |
|
public function supports($object, string $property, $value): bool |
38
|
|
|
{ |
39
|
|
|
return $this |
40
|
1 |
|
->strategies |
41
|
1 |
|
->reduce( |
42
|
1 |
|
false, |
43
|
|
|
function(bool $supports, InjectionStrategyInterface $strategy) use ($object, $property, $value): bool { |
44
|
1 |
|
if ($supports === true) { |
45
|
1 |
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return $strategy->supports($object, $property, $value); |
49
|
1 |
|
} |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
9 |
|
public function inject($object, string $property, $value): void |
57
|
|
|
{ |
58
|
9 |
|
$key = $this->generateKey($object, $property); |
59
|
|
|
|
60
|
9 |
|
if ($this->cache->contains($key)) { |
|
|
|
|
61
|
|
|
$this |
62
|
1 |
|
->cache |
63
|
1 |
|
->get($key) |
|
|
|
|
64
|
1 |
|
->inject($object, $property, $value); |
65
|
|
|
|
66
|
1 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
$strategy = $this->strategies->reduce( |
70
|
9 |
|
null, |
71
|
9 |
|
function($target, InjectionStrategyInterface $strategy) use ($object, $property, $value) { |
72
|
9 |
|
if ($target instanceof InjectionStrategyInterface) { |
73
|
4 |
|
return $target; |
74
|
|
|
} |
75
|
|
|
|
76
|
9 |
|
if ($strategy->supports($object, $property, $value)) { |
77
|
6 |
|
return $strategy; |
78
|
|
|
} |
79
|
9 |
|
} |
80
|
|
|
); |
81
|
|
|
|
82
|
9 |
|
if (!$strategy instanceof InjectionStrategyInterface) { |
83
|
3 |
|
throw new PropertyCannotBeInjectedException($property); |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
$this->cache = $this->cache->put($key, $strategy); |
|
|
|
|
87
|
|
|
|
88
|
6 |
|
$strategy->inject($object, $property, $value); |
89
|
6 |
|
} |
90
|
|
|
|
91
|
9 |
|
private function generateKey($object, string $property): string |
92
|
|
|
{ |
93
|
9 |
|
return get_class($object).'::'.$property; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.