@@ 15-62 (lines=48) @@ | ||
12 | * |
|
13 | * @author Hugues Maignol <[email protected]> |
|
14 | */ |
|
15 | final class DefaultExtractionStrategies implements ExtractionStrategies |
|
16 | { |
|
17 | ||
18 | use StrategyCachingCapabilities; |
|
19 | ||
20 | private $strategies; |
|
21 | ||
22 | public function all(): TypedCollectionInterface |
|
23 | { |
|
24 | if ($this->strategies == null) { |
|
25 | return $this->strategies = new TypedCollection( |
|
26 | ExtractionStrategyInterface::class, |
|
27 | [ |
|
28 | new GetterStrategy, |
|
29 | new NamedMethodStrategy, |
|
30 | new ReflectionStrategy, |
|
31 | ] |
|
32 | ); |
|
33 | } |
|
34 | ||
35 | return $this->strategies; |
|
36 | } |
|
37 | ||
38 | public function get($object, string $key): ExtractionStrategyInterface |
|
39 | { |
|
40 | $strategy = $this->getCachedStrategy(get_class($object), $key); |
|
41 | if (null !== $strategy) { |
|
42 | return $strategy; |
|
43 | } |
|
44 | ||
45 | foreach ($this->all() as $strategy) { |
|
46 | if ($strategy->supports($object, $key)) { |
|
47 | ||
48 | $this->setCachedStrategy(get_class($object), $key, $strategy); |
|
49 | ||
50 | return $strategy; |
|
51 | } |
|
52 | } |
|
53 | ||
54 | throw new LogicException( |
|
55 | sprintf( |
|
56 | 'Property "%s" cannot be extracted', |
|
57 | $key |
|
58 | ) |
|
59 | ); |
|
60 | } |
|
61 | ||
62 | } |
|
63 |
@@ 15-60 (lines=46) @@ | ||
12 | * |
|
13 | * @author Hugues Maignol <[email protected]> |
|
14 | */ |
|
15 | final class DefaultInjectionStrategies implements InjectionStrategies |
|
16 | { |
|
17 | use StrategyCachingCapabilities; |
|
18 | ||
19 | private $strategies; |
|
20 | ||
21 | public function all(): TypedCollectionInterface |
|
22 | { |
|
23 | if ($this->strategies == null) { |
|
24 | return $this->strategies = new TypedCollection( |
|
25 | InjectionStrategyInterface::class, |
|
26 | [ |
|
27 | new SetterStrategy, |
|
28 | new NamedMethodStrategy, |
|
29 | new ReflectionStrategy, |
|
30 | ] |
|
31 | ); |
|
32 | } |
|
33 | ||
34 | return $this->strategies; |
|
35 | } |
|
36 | ||
37 | public function get($object, string $key, $value): InjectionStrategyInterface |
|
38 | { |
|
39 | $strategy = $this->getCachedStrategy(get_class($object), $key); |
|
40 | if (null !== $strategy) { |
|
41 | return $strategy; |
|
42 | } |
|
43 | ||
44 | foreach ($this->all() as $strategy) { |
|
45 | if ($strategy->supports($object, $key, $value)) { |
|
46 | ||
47 | $this->setCachedStrategy(get_class($object), $key, $strategy); |
|
48 | ||
49 | return $strategy; |
|
50 | } |
|
51 | } |
|
52 | ||
53 | throw new LogicException( |
|
54 | sprintf( |
|
55 | 'Property "%s" cannot be injected', |
|
56 | $key |
|
57 | ) |
|
58 | ); |
|
59 | } |
|
60 | } |
|
61 |