|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Testo\Module\Interceptor\Internal; |
| 6 | + |
| 7 | +use Testo\Interceptor\Reflection\Reflection; |
| 8 | +use Testo\Module\Interceptor\InterceptorOptions; |
| 9 | +use Testo\Module\Interceptor\Internal\InterceptorMarker as TInterceptor; |
| 10 | +use Testo\Module\Interceptor\Policy\ConflictPolicy; |
| 11 | + |
| 12 | +/** |
| 13 | + * Sorts and filters interceptors. |
| 14 | + * @internal |
| 15 | + */ |
| 16 | +final class Sorter |
| 17 | +{ |
| 18 | + /** @var array<class-string<TInterceptor>, ConflictPolicy> */ |
| 19 | + private static array $conflictPolicyCache = []; |
| 20 | + |
| 21 | + /** @var array<class-string<TInterceptor>, int> */ |
| 22 | + private static array $orderCache = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * Sort and filter interceptors. |
| 26 | + * |
| 27 | + * @param TInterceptor[] $interceptors |
| 28 | + * |
| 29 | + * @return TInterceptor[] |
| 30 | + */ |
| 31 | + public static function sortAndFilter(array $interceptors): array |
| 32 | + { |
| 33 | + # Local caches |
| 34 | + $conflicts = []; |
| 35 | + $orders = []; |
| 36 | + |
| 37 | + /** @var array<int, array<class-string<TInterceptor>|int, TInterceptor>> $groups */ |
| 38 | + $groups = []; |
| 39 | + foreach ($interceptors as $interceptor) { |
| 40 | + $class = $interceptor::class; |
| 41 | + $conflict = $conflicts[$class] ??= self::getConflictPolicy($interceptor); |
| 42 | + $order = $orders[$class] ??= self::getOrder($interceptor); |
| 43 | + |
| 44 | + switch ($conflict) { |
| 45 | + case ConflictPolicy::First: |
| 46 | + $groups[$order][$class] ??= $interceptor; |
| 47 | + break; |
| 48 | + case ConflictPolicy::Last: |
| 49 | + unset($groups[$order][$class]); |
| 50 | + $groups[$order][$class] = $interceptor; |
| 51 | + break; |
| 52 | + case ConflictPolicy::Error: |
| 53 | + \array_key_exists($class, $groups[$order]) |
| 54 | + ? throw new \RuntimeException("Conflict detected for interceptor '$class' with policy 'Error'.") |
| 55 | + : $groups[$order][$class] = $interceptor; |
| 56 | + break; |
| 57 | + default: |
| 58 | + $groups[$order][] = $interceptor; |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + \ksort($groups); |
| 64 | + return \array_values(\array_merge(...$groups)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Warm up the cache for the given interceptor class. |
| 69 | + * |
| 70 | + * @param class-string<TInterceptor> $class |
| 71 | + */ |
| 72 | + private static function warmUpCache(string $class): void |
| 73 | + { |
| 74 | + /** @var list<\ReflectionAttribute<InterceptorOptions>> $attributes */ |
| 75 | + $attributes = Reflection::fetchClassAttributes( |
| 76 | + $class, |
| 77 | + attributeClass: InterceptorOptions::class, |
| 78 | + ); |
| 79 | + |
| 80 | + if ($attributes === []) { |
| 81 | + self::$conflictPolicyCache[$class] = ConflictPolicy::default(); |
| 82 | + self::$orderCache[$class] = InterceptorOptions::DEFAULT_ORDER; |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $attribute = $attributes[0]->newInstance(); |
| 87 | + self::$conflictPolicyCache[$class] = $attribute->onConflict; |
| 88 | + self::$orderCache[$class] = $attribute->order; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the conflict policy of the given interceptor by its attribute. |
| 93 | + */ |
| 94 | + private static function getConflictPolicy(TInterceptor $interceptor): ConflictPolicy |
| 95 | + { |
| 96 | + $class = $interceptor::class; |
| 97 | + \array_key_exists($class, self::$conflictPolicyCache) or self::warmUpCache($class); |
| 98 | + return self::$conflictPolicyCache[$class]; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get the order of the given interceptor by its attribute. |
| 103 | + */ |
| 104 | + private static function getOrder(TInterceptor $interceptor): int |
| 105 | + { |
| 106 | + $class = $interceptor::class; |
| 107 | + \array_key_exists($class, self::$orderCache) or self::warmUpCache($class); |
| 108 | + return self::$orderCache[$class]; |
| 109 | + } |
| 110 | +} |
0 commit comments