1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Sync; |
4: | |
5: | use Salient\Contract\Container\ContainerInterface; |
6: | use Salient\Contract\Container\HasContextualBindings; |
7: | use Salient\Contract\Container\HasServices; |
8: | use Salient\Contract\Core\Pipeline\PipelineInterface; |
9: | use Salient\Contract\Sync\FilterPolicy; |
10: | use Salient\Contract\Sync\SyncContextInterface; |
11: | use Salient\Contract\Sync\SyncEntityInterface; |
12: | use Salient\Contract\Sync\SyncProviderInterface; |
13: | use Salient\Contract\Sync\SyncStoreInterface; |
14: | use Salient\Core\AbstractProvider; |
15: | use Salient\Core\Pipeline; |
16: | use Salient\Sync\Exception\FilterPolicyViolationException; |
17: | use Salient\Sync\Exception\SyncEntityRecursionException; |
18: | use Salient\Sync\Reflection\ReflectionSyncProvider; |
19: | use Salient\Sync\Support\SyncContext; |
20: | use Salient\Sync\Support\SyncEntityProvider; |
21: | use Salient\Sync\Support\SyncIntrospector; |
22: | use Salient\Sync\Support\SyncPipelineArgument; |
23: | use Salient\Utility\Regex; |
24: | use Salient\Utility\Str; |
25: | use Closure; |
26: | use LogicException; |
27: | |
28: | |
29: | |
30: | |
31: | abstract class AbstractSyncProvider extends AbstractProvider implements |
32: | SyncProviderInterface, |
33: | HasServices, |
34: | HasContextualBindings |
35: | { |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | public static function getContextualBindings(): array |
56: | { |
57: | return []; |
58: | } |
59: | |
60: | protected SyncStoreInterface $Store; |
61: | private int $Id; |
62: | |
63: | private array $MagicMethodClosures = []; |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | public function __construct(ContainerInterface $app, SyncStoreInterface $store) |
72: | { |
73: | parent::__construct($app); |
74: | |
75: | $this->Store = $store; |
76: | $this->Store->registerProvider($this); |
77: | } |
78: | |
79: | |
80: | |
81: | |
82: | public function getContext(): SyncContextInterface |
83: | { |
84: | return new SyncContext($this->App, $this); |
85: | } |
86: | |
87: | |
88: | |
89: | |
90: | public function getFilterPolicy(): ?int |
91: | { |
92: | return null; |
93: | } |
94: | |
95: | |
96: | |
97: | |
98: | public function isValidIdentifier($id, string $entity): bool |
99: | { |
100: | return is_int($id) |
101: | || Regex::match(Regex::delimit('^' . Regex::MONGODB_OBJECTID . '$', '/'), $id) |
102: | || Regex::match(Regex::delimit('^' . Regex::UUID . '$', '/'), $id); |
103: | } |
104: | |
105: | |
106: | |
107: | |
108: | final public function getStore(): SyncStoreInterface |
109: | { |
110: | return $this->Store; |
111: | } |
112: | |
113: | |
114: | |
115: | |
116: | final public function getProviderId(): int |
117: | { |
118: | return $this->Id ??= $this->Store->getProviderId($this); |
119: | } |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: | protected function run(SyncContextInterface $context, Closure $operation) |
163: | { |
164: | return $this->filterOperationOutput( |
165: | $context, |
166: | $this->runOperation($context, $operation), |
167: | ); |
168: | } |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | protected function pipelineFrom(string $entity): PipelineInterface |
179: | { |
180: | |
181: | return Pipeline::create($this->App); |
182: | } |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | protected function pipelineTo(string $entity): PipelineInterface |
193: | { |
194: | |
195: | return Pipeline::create($this->App); |
196: | } |
197: | |
198: | |
199: | |
200: | |
201: | final public static function getServices(): array |
202: | { |
203: | $provider = new ReflectionSyncProvider(static::class); |
204: | return $provider->getSyncProviderInterfaces(); |
205: | } |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | final public function with(string $entity, ?SyncContextInterface $context = null): SyncEntityProvider |
214: | { |
215: | if ($context) { |
216: | if ($context->recursionDetected()) { |
217: | throw new SyncEntityRecursionException(sprintf( |
218: | 'Circular reference detected: %s', |
219: | $context->getLastEntity()->getUri($this->Store), |
220: | )); |
221: | } |
222: | $container = $context->getContainer(); |
223: | } else { |
224: | $container = $this->App; |
225: | } |
226: | |
227: | $container = $container->inContextOf(static::class); |
228: | $context = ($context ?? $this->getContext())->withContainer($container); |
229: | |
230: | return $container->get( |
231: | SyncEntityProvider::class, |
232: | ['entity' => $entity, 'provider' => $this, 'context' => $context], |
233: | ); |
234: | } |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | final public function runOperation(SyncContextInterface $context, Closure $operation) |
244: | { |
245: | if (!$context->hasOperation()) { |
246: | throw new LogicException('Context has no operation'); |
247: | } |
248: | |
249: | if ($context->hasFilter()) { |
250: | $policy = $context->getProvider()->getFilterPolicy() |
251: | ?? FilterPolicy::THROW_EXCEPTION; |
252: | |
253: | switch ($policy) { |
254: | case FilterPolicy::IGNORE: |
255: | break; |
256: | |
257: | case FilterPolicy::THROW_EXCEPTION: |
258: | throw new FilterPolicyViolationException( |
259: | $this, |
260: | $context->getEntityType(), |
261: | $context->getFilters(), |
262: | ); |
263: | |
264: | case FilterPolicy::RETURN_EMPTY: |
265: | |
266: | return SyncUtil::isListOperation($context->getOperation()) |
267: | ? [] |
268: | : null; |
269: | |
270: | case FilterPolicy::FILTER: |
271: | break; |
272: | |
273: | default: |
274: | throw new LogicException(sprintf( |
275: | 'Invalid unclaimed filter policy: %d', |
276: | $policy, |
277: | )); |
278: | } |
279: | } |
280: | |
281: | return $operation(); |
282: | } |
283: | |
284: | |
285: | |
286: | |
287: | final public function filterOperationOutput(SyncContextInterface $context, $output) |
288: | { |
289: | if (!$context->hasOperation()) { |
290: | throw new LogicException('Context has no operation'); |
291: | } |
292: | |
293: | if ( |
294: | $context->hasFilter() |
295: | && $context->getProvider()->getFilterPolicy() === FilterPolicy::FILTER |
296: | ) { |
297: | throw new LogicException('Unclaimed filter policy not implemented'); |
298: | } |
299: | |
300: | return $output; |
301: | } |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | final public function __call(string $name, array $arguments) |
308: | { |
309: | $name = Str::lower($name); |
310: | if (array_key_exists($name, $this->MagicMethodClosures)) { |
311: | $closure = $this->MagicMethodClosures[$name]; |
312: | } else { |
313: | $closure = SyncIntrospector::get(static::class)->getMagicSyncOperationClosure($name, $this); |
314: | $this->MagicMethodClosures[$name] = $closure; |
315: | } |
316: | |
317: | if ($closure) { |
318: | return $closure(...$arguments); |
319: | } |
320: | |
321: | throw new LogicException('Call to undefined method: ' . static::class . "::$name()"); |
322: | } |
323: | } |
324: | |