|
| 1 | +<?php |
| 2 | +/** @noinspection PhpPropertyOnlyWrittenInspection */ |
| 3 | +namespace Okapi\CodeTransformer\Core\Matcher; |
| 4 | + |
| 5 | +use DI\Attribute\Inject; |
| 6 | +use Okapi\CodeTransformer\Core\Cache\CacheState\EmptyResultCacheState; |
| 7 | +use Okapi\CodeTransformer\Core\Cache\CacheStateManager; |
| 8 | +use Okapi\CodeTransformer\Core\Container\TransformerContainer; |
| 9 | +use Okapi\CodeTransformer\Core\Container\TransformerManager; |
| 10 | +use Okapi\CodeTransformer\Core\DI; |
| 11 | +use Okapi\CodeTransformer\Transformer; |
| 12 | +use Okapi\Wildcards\Regex; |
| 13 | + |
| 14 | +// TODO: docs |
| 15 | +class TransformerMatcher |
| 16 | +{ |
| 17 | + // region DI |
| 18 | + |
| 19 | + #[Inject] |
| 20 | + private TransformerManager $transformerContainer; |
| 21 | + |
| 22 | + #[Inject] |
| 23 | + private CacheStateManager $cacheStateManager; |
| 24 | + |
| 25 | + // endregion |
| 26 | + |
| 27 | + /** |
| 28 | + * Cache for the query result of the transformer matching. |
| 29 | + * |
| 30 | + * @var array<class-string, Transformer[]> |
| 31 | + */ |
| 32 | + private array $matchedTransformerContainers = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * Check if the class should be transformed. |
| 36 | + * |
| 37 | + * @param string $namespacedClass |
| 38 | + * @param string $filePath |
| 39 | + * |
| 40 | + * @return bool |
| 41 | + */ |
| 42 | + public function match(string $namespacedClass, string $filePath): bool |
| 43 | + { |
| 44 | + // Get the transformers |
| 45 | + $transformerContainers = $this->transformerContainer->getTransformerContainers(); |
| 46 | + |
| 47 | + // Match the transformers |
| 48 | + $matchedTransformerContainers = []; |
| 49 | + foreach ($transformerContainers as $transformerContainer) { |
| 50 | + $wildcardPatterns = (array)$transformerContainer->transformerInstance->getTargetClass(); |
| 51 | + |
| 52 | + foreach ($wildcardPatterns as $wildcardPattern) { |
| 53 | + $regex = Regex::fromWildcard($wildcardPattern); |
| 54 | + if ($regex->matches($namespacedClass)) { |
| 55 | + // Check if the transformer has already been matched |
| 56 | + $alreadyMatched = array_reduce( |
| 57 | + $matchedTransformerContainers, |
| 58 | + function (bool $carry, TransformerContainer $container) use ($transformerContainer) { |
| 59 | + return $carry || $container === $transformerContainer; |
| 60 | + }, |
| 61 | + false, |
| 62 | + ); |
| 63 | + |
| 64 | + if ($alreadyMatched) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $matchedTransformerContainers[] = $transformerContainer; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Cache the result |
| 74 | + $this->matchedTransformerContainers[$namespacedClass] = $matchedTransformerContainers; |
| 75 | + |
| 76 | + // Cache the result |
| 77 | + if (!$matchedTransformerContainers) { |
| 78 | + $cacheState = DI::make(EmptyResultCacheState::class, [ |
| 79 | + 'data' => [ |
| 80 | + 'originalFilePath' => $filePath, |
| 81 | + 'modificationTime' => filemtime($filePath), |
| 82 | + ], |
| 83 | + ]); |
| 84 | + |
| 85 | + // Set the cache state |
| 86 | + $this->cacheStateManager->setCacheState( |
| 87 | + $filePath, |
| 88 | + $cacheState, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + return (bool)$matchedTransformerContainers; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get the matched transformers for the given class. |
| 97 | + * |
| 98 | + * @param string $namespacedClass |
| 99 | + * |
| 100 | + * @return TransformerContainer[] |
| 101 | + */ |
| 102 | + public function getMatchedTransformerContainers(string $namespacedClass): array |
| 103 | + { |
| 104 | + // Check if the query has been cached |
| 105 | + return $this->matchedTransformerContainers[$namespacedClass] ?? []; |
| 106 | + } |
| 107 | +} |
0 commit comments