1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace LaravelUi5\OData\Console\Concerns;
6:
7: use LaravelUi5\OData\Service\Contracts\ODataServiceInterface;
8: use LaravelUi5\OData\Service\Contracts\ODataServiceRegistryInterface;
9:
10: /**
11: * Resolves the target services for the cache commands: every registry service, plus any
12: * named via `--class=FQCN1,FQCN2` (route-composed / bound services that are deliberately
13: * NOT in the registry). Deduped by class. Prints an error and returns null on a bad entry.
14: */
15: trait ResolvesServices
16: {
17: /**
18: * @return list<ODataServiceInterface>|null null = a --class entry was invalid (already reported)
19: */
20: protected function resolveServices(ODataServiceRegistryInterface $registry): ?array
21: {
22: $services = [];
23: $seen = [];
24:
25: foreach ($registry->services() as $service) {
26: $services[] = $service;
27: $seen[ltrim($service::class, '\\')] = true;
28: }
29:
30: $option = (string) ($this->option('class') ?? '');
31:
32: foreach (array_filter(array_map('trim', explode(',', $option))) as $fqcn) {
33: $fqcn = ltrim($fqcn, '\\');
34:
35: if (isset($seen[$fqcn])) {
36: continue; // already provided by the registry
37: }
38:
39: if (!class_exists($fqcn)) {
40: $this->error("Class not found: {$fqcn}");
41: return null;
42: }
43:
44: $instance = app($fqcn);
45:
46: if (!$instance instanceof ODataServiceInterface) {
47: $this->error("{$fqcn} does not implement ODataServiceInterface.");
48: return null;
49: }
50:
51: $services[] = $instance;
52: $seen[$fqcn] = true;
53: }
54:
55: return $services;
56: }
57: }
58: