|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenSolid\CallableInvoker\DependecyInjection\Compiler; |
| 6 | + |
| 7 | +use OpenSolid\CallableInvoker\CallableInvokerInterface; |
| 8 | +use Symfony\Component\DependencyInjection\Argument\IteratorArgument; |
| 9 | +use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
| 10 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 12 | +use Symfony\Component\DependencyInjection\Reference; |
| 13 | + |
| 14 | +final readonly class CallableInvokerPass implements CompilerPassInterface |
| 15 | +{ |
| 16 | + public function process(ContainerBuilder $container): void |
| 17 | + { |
| 18 | + [$decoratorGrouped, $decoratorUngrouped] = $this->collectTaggedServices($container, 'callable_invoker.decorator'); |
| 19 | + [$resolverGrouped, $resolverUngrouped] = $this->collectTaggedServices($container, 'callable_invoker.value_resolver'); |
| 20 | + |
| 21 | + // Unified group universe from both tags |
| 22 | + $allExplicitGroups = array_keys($decoratorGrouped + $resolverGrouped); |
| 23 | + |
| 24 | + $container->getDefinition('callable_invoker.decorator_groups') |
| 25 | + ->setArgument(0, new ServiceLocatorArgument($this->buildGroups($decoratorGrouped, $decoratorUngrouped, $allExplicitGroups))); |
| 26 | + $container->getDefinition('callable_invoker.value_resolver_groups') |
| 27 | + ->setArgument(0, new ServiceLocatorArgument($this->buildGroups($resolverGrouped, $resolverUngrouped, $allExplicitGroups))); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return array{ |
| 32 | + * array<string, array<string, array{ref: Reference, priority: int}>>, |
| 33 | + * array<string, array{ref: Reference, priority: int}> |
| 34 | + * } |
| 35 | + */ |
| 36 | + private function collectTaggedServices(ContainerBuilder $container, string $tagName): array |
| 37 | + { |
| 38 | + $grouped = []; |
| 39 | + $ungrouped = []; |
| 40 | + |
| 41 | + foreach ($container->findTaggedServiceIds($tagName) as $id => $tags) { |
| 42 | + $ref = new Reference($id); |
| 43 | + $hasExplicitGroup = false; |
| 44 | + /** @var int|null $maxPriority */ |
| 45 | + $maxPriority = null; |
| 46 | + |
| 47 | + /** @var array{priority?: int, groups?: list<string>} $tag */ |
| 48 | + foreach ($tags as $tag) { |
| 49 | + $priority = $tag['priority'] ?? 0; |
| 50 | + $maxPriority = null === $maxPriority ? $priority : max($maxPriority, $priority); |
| 51 | + foreach ($tag['groups'] ?? [] as $group) { |
| 52 | + $grouped[$group][$id] = ['ref' => $ref, 'priority' => $priority]; |
| 53 | + $hasExplicitGroup = true; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (!$hasExplicitGroup) { |
| 58 | + $ungrouped[$id] = ['ref' => $ref, 'priority' => $maxPriority ?? 0]; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return [$grouped, $ungrouped]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param array<string, array<string, array{ref: Reference, priority: int}>> $grouped |
| 67 | + * @param array<string, array{ref: Reference, priority: int}> $ungrouped |
| 68 | + * @param list<string> $allExplicitGroups |
| 69 | + * |
| 70 | + * @return array<string, IteratorArgument> |
| 71 | + */ |
| 72 | + private function buildGroups(array $grouped, array $ungrouped, array $allExplicitGroups): array |
| 73 | + { |
| 74 | + if ($ungrouped) { |
| 75 | + $defaultGroup = CallableInvokerInterface::DEFAULT_GROUP; |
| 76 | + $grouped[$defaultGroup] = ($grouped[$defaultGroup] ?? []) + $ungrouped; |
| 77 | + foreach ($allExplicitGroups as $group) { |
| 78 | + if ($defaultGroup !== $group) { |
| 79 | + $grouped[$group] = ($grouped[$group] ?? []) + $ungrouped; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + $result = []; |
| 85 | + foreach ($grouped as $group => $entries) { |
| 86 | + uasort($entries, static fn (array $a, array $b) => $b['priority'] <=> $a['priority']); |
| 87 | + $result[$group] = new IteratorArgument(array_column($entries, 'ref')); |
| 88 | + } |
| 89 | + |
| 90 | + return $result; |
| 91 | + } |
| 92 | +} |
0 commit comments