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\Provider\AbstractProvider; |
15: | use Salient\Core\Pipeline; |
16: | use Salient\Sync\Exception\FilterPolicyViolationException; |
17: | use Salient\Sync\Exception\SyncEntityRecursionException; |
18: | use Salient\Sync\Reflection\SyncProviderReflection; |
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, $this->App); |
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: | protected function run(SyncContextInterface $context, Closure $operation) |
162: | { |
163: | return $this->filterOperationOutput( |
164: | $context, |
165: | $this->runOperation($context, $operation), |
166: | ); |
167: | } |
168: | |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | protected function pipelineFrom(string $entity): PipelineInterface |
178: | { |
179: | |
180: | return Pipeline::create(); |
181: | } |
182: | |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | protected function pipelineTo(string $entity): PipelineInterface |
192: | { |
193: | |
194: | return Pipeline::create(); |
195: | } |
196: | |
197: | |
198: | |
199: | |
200: | final public static function getServices(): array |
201: | { |
202: | $provider = new SyncProviderReflection(static::class); |
203: | return $provider->getSyncProviderInterfaces(); |
204: | } |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | final public function with(string $entity, ?SyncContextInterface $context = null): SyncEntityProvider |
213: | { |
214: | if ($context) { |
215: | if ($context->recursionDetected()) { |
216: | throw new SyncEntityRecursionException(sprintf( |
217: | 'Circular reference detected: %s', |
218: | $context->getLastEntity()->getUri($this->Store), |
219: | )); |
220: | } |
221: | $container = $context->getContainer(); |
222: | } else { |
223: | $container = $this->App; |
224: | } |
225: | |
226: | $container = $container->inContextOf(static::class); |
227: | $context = ($context ?? $this->getContext())->withContainer($container); |
228: | |
229: | return $container->get( |
230: | SyncEntityProvider::class, |
231: | ['entity' => $entity, 'provider' => $this, 'context' => $context], |
232: | ); |
233: | } |
234: | |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | |
242: | final public function runOperation(SyncContextInterface $context, Closure $operation) |
243: | { |
244: | if (!$context->hasOperation()) { |
245: | throw new LogicException('Context has no operation'); |
246: | } |
247: | |
248: | if ($context->hasFilter()) { |
249: | $policy = $context->getProvider()->getFilterPolicy() |
250: | ?? FilterPolicy::THROW_EXCEPTION; |
251: | |
252: | switch ($policy) { |
253: | case FilterPolicy::IGNORE: |
254: | break; |
255: | |
256: | case FilterPolicy::THROW_EXCEPTION: |
257: | throw new FilterPolicyViolationException( |
258: | $this, |
259: | $context->getEntityType(), |
260: | $context->getFilters(), |
261: | ); |
262: | |
263: | case FilterPolicy::RETURN_EMPTY: |
264: | |
265: | return SyncUtil::isListOperation($context->getOperation()) |
266: | ? [] |
267: | : null; |
268: | |
269: | case FilterPolicy::FILTER: |
270: | break; |
271: | |
272: | default: |
273: | throw new LogicException(sprintf( |
274: | 'Invalid unclaimed filter policy: %d', |
275: | $policy, |
276: | )); |
277: | } |
278: | } |
279: | |
280: | return $operation(); |
281: | } |
282: | |
283: | |
284: | |
285: | |
286: | final public function filterOperationOutput(SyncContextInterface $context, $output) |
287: | { |
288: | if (!$context->hasOperation()) { |
289: | throw new LogicException('Context has no operation'); |
290: | } |
291: | |
292: | if ( |
293: | $context->hasFilter() |
294: | && $context->getProvider()->getFilterPolicy() === FilterPolicy::FILTER |
295: | ) { |
296: | throw new LogicException('Unclaimed filter policy not implemented'); |
297: | } |
298: | |
299: | return $output; |
300: | } |
301: | |
302: | |
303: | |
304: | |
305: | |
306: | final public function __call(string $name, array $arguments) |
307: | { |
308: | $name = Str::lower($name); |
309: | if (array_key_exists($name, $this->MagicMethodClosures)) { |
310: | $closure = $this->MagicMethodClosures[$name]; |
311: | } else { |
312: | $closure = SyncIntrospector::get(static::class)->getMagicSyncOperationClosure($name, $this); |
313: | $this->MagicMethodClosures[$name] = $closure; |
314: | } |
315: | |
316: | if ($closure) { |
317: | return $closure(...$arguments); |
318: | } |
319: | |
320: | throw new LogicException('Call to undefined method: ' . static::class . "::$name()"); |
321: | } |
322: | } |
323: | |