1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Core\Concern; |
4: | |
5: | use Salient\Contract\Core\Entity\Extensible; |
6: | use Salient\Contract\Core\Provider\Providable; |
7: | use Salient\Contract\Core\Provider\ProviderContextInterface; |
8: | use Salient\Contract\Core\Provider\ProviderInterface; |
9: | use Salient\Contract\Core\ListConformity; |
10: | use Salient\Contract\Iterator\FluentIteratorInterface; |
11: | use Salient\Core\Introspector; |
12: | use Salient\Iterator\IterableIterator; |
13: | use Generator; |
14: | use LogicException; |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | trait ProvidableTrait |
25: | { |
26: | |
27: | private $Provider; |
28: | |
29: | private $Context; |
30: | |
31: | private $Service; |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | final public function setProvider(ProviderInterface $provider) |
38: | { |
39: | if ($this->Provider) { |
40: | throw new LogicException('Provider already set'); |
41: | } |
42: | $this->Provider = $provider; |
43: | |
44: | return $this; |
45: | } |
46: | |
47: | |
48: | |
49: | |
50: | final public function getProvider(): ?ProviderInterface |
51: | { |
52: | return $this->Provider; |
53: | } |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | final public function setContext(ProviderContextInterface $context) |
60: | { |
61: | $this->Context = $context; |
62: | |
63: | return $this; |
64: | } |
65: | |
66: | |
67: | |
68: | |
69: | final public function getContext(): ?ProviderContextInterface |
70: | { |
71: | return $this->Context; |
72: | } |
73: | |
74: | |
75: | |
76: | |
77: | final public function setService(string $service): void |
78: | { |
79: | $this->Service = $service; |
80: | } |
81: | |
82: | |
83: | |
84: | |
85: | final public function getService(): string |
86: | { |
87: | return $this->Service ?? static::class; |
88: | } |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | final public static function provide( |
107: | array $data, |
108: | ProviderInterface $provider, |
109: | ?ProviderContextInterface $context = null |
110: | ) { |
111: | $container = $context |
112: | ? $context->getContainer() |
113: | : $provider->getContainer(); |
114: | $container = $container->inContextOf(get_class($provider)); |
115: | |
116: | $context = ($context ?? $provider->getContext())->withContainer($container); |
117: | |
118: | $closure = Introspector::getService( |
119: | $container, static::class |
120: | )->getCreateProvidableFromClosure(); |
121: | |
122: | return $closure($data, $provider, $context); |
123: | } |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | final public static function provideMultiple( |
137: | iterable $list, |
138: | ProviderInterface $provider, |
139: | int $conformity = ListConformity::NONE, |
140: | ?ProviderContextInterface $context = null |
141: | ): FluentIteratorInterface { |
142: | return IterableIterator::from( |
143: | self::_provideMultiple($list, $provider, $conformity, $context) |
144: | ); |
145: | } |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | private static function _provideMultiple( |
155: | iterable $list, |
156: | ProviderInterface $provider, |
157: | $conformity, |
158: | ?ProviderContextInterface $context |
159: | ): Generator { |
160: | $container = $context |
161: | ? $context->getContainer() |
162: | : $provider->getContainer(); |
163: | $container = $container->inContextOf(get_class($provider)); |
164: | |
165: | $conformity = $context |
166: | ? max($context->getConformity(), $conformity) |
167: | : $conformity; |
168: | |
169: | $context = ($context ?? $provider->getContext())->withContainer($container); |
170: | |
171: | $introspector = Introspector::getService($container, static::class); |
172: | |
173: | foreach ($list as $key => $data) { |
174: | if (!isset($closure)) { |
175: | $closure = $conformity === ListConformity::PARTIAL || $conformity === ListConformity::COMPLETE |
176: | ? $introspector->getCreateProvidableFromSignatureClosure(array_keys($data)) |
177: | : $introspector->getCreateProvidableFromClosure(); |
178: | } |
179: | |
180: | yield $key => $closure($data, $provider, $context); |
181: | } |
182: | } |
183: | } |
184: | |