Innmind /
Reflection
| 1 | <?php |
||||
| 2 | declare(strict_types = 1); |
||||
| 3 | |||||
| 4 | namespace Innmind\Reflection\InjectionStrategy; |
||||
| 5 | |||||
| 6 | use Innmind\Reflection\{ |
||||
| 7 | InjectionStrategy, |
||||
| 8 | Exception\InvalidArgumentException, |
||||
| 9 | Exception\PropertyCannotBeInjected, |
||||
| 10 | }; |
||||
| 11 | use Innmind\Immutable\{ |
||||
| 12 | Sequence, |
||||
| 13 | Map, |
||||
| 14 | }; |
||||
| 15 | |||||
| 16 | final class DelegationStrategy implements InjectionStrategy |
||||
| 17 | { |
||||
| 18 | /** @var Sequence<InjectionStrategy> */ |
||||
| 19 | private Sequence $strategies; |
||||
| 20 | /** @var Map<string, InjectionStrategy> */ |
||||
| 21 | 7 | private Map $cache; |
|||
| 22 | |||||
| 23 | 7 | public function __construct(InjectionStrategy ...$strategies) |
|||
| 24 | 7 | { |
|||
| 25 | 7 | /** @var Sequence<InjectionStrategy> */ |
|||
| 26 | $this->strategies = Sequence::of(InjectionStrategy::class, ...$strategies); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 27 | /** @var Map<string, InjectionStrategy> */ |
||||
| 28 | $this->cache = Map::of('string', InjectionStrategy::class); |
||||
|
0 ignored issues
–
show
|
|||||
| 29 | } |
||||
| 30 | 1 | ||||
| 31 | /** |
||||
| 32 | * {@inheritdoc} |
||||
| 33 | 1 | */ |
|||
| 34 | 1 | public function supports(object $object, string $property, $value): bool |
|||
| 35 | 1 | { |
|||
| 36 | return $this |
||||
|
0 ignored issues
–
show
|
|||||
| 37 | 1 | ->strategies |
|||
| 38 | 1 | ->reduce( |
|||
| 39 | false, |
||||
|
0 ignored issues
–
show
false of type false is incompatible with the type Innmind\Immutable\R expected by parameter $carry of Innmind\Immutable\Sequence::reduce().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 40 | static function(bool $supports, InjectionStrategy $strategy) use ($object, $property, $value): bool { |
||||
| 41 | return $supports || $strategy->supports($object, $property, $value); |
||||
| 42 | }, |
||||
| 43 | ); |
||||
| 44 | } |
||||
| 45 | 9 | ||||
| 46 | /** |
||||
| 47 | 9 | * {@inheritdoc} |
|||
| 48 | */ |
||||
| 49 | 9 | public function inject(object $object, string $property, $value): object |
|||
| 50 | { |
||||
| 51 | 1 | $key = $this->generateKey($object, $property); |
|||
| 52 | 1 | ||||
| 53 | 1 | if ($this->cache->contains($key)) { |
|||
| 54 | return $this |
||||
| 55 | ->cache |
||||
| 56 | 9 | ->get($key) |
|||
| 57 | 9 | ->inject($object, $property, $value); |
|||
| 58 | } |
||||
| 59 | 9 | ||||
| 60 | 9 | $strategy = $this->strategies->reduce( |
|||
| 61 | null, |
||||
| 62 | static function(?InjectionStrategy $target, InjectionStrategy $strategy) use ($object, $property, $value): ?InjectionStrategy { |
||||
| 63 | 9 | return $target ?? ($strategy->supports($object, $property, $value) ? $strategy : null); |
|||
| 64 | 3 | }, |
|||
| 65 | ); |
||||
| 66 | |||||
| 67 | 6 | if (!$strategy instanceof InjectionStrategy) { |
|||
| 68 | throw new PropertyCannotBeInjected($property); |
||||
| 69 | 6 | } |
|||
| 70 | |||||
| 71 | $this->cache = ($this->cache)($key, $strategy); |
||||
| 72 | 9 | ||||
| 73 | return $strategy->inject($object, $property, $value); |
||||
| 74 | 9 | } |
|||
| 75 | |||||
| 76 | private function generateKey(object $object, string $property): string |
||||
| 77 | { |
||||
| 78 | return \get_class($object).'::'.$property; |
||||
| 79 | } |
||||
| 80 | } |
||||
| 81 |