|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Butschster\ContextGenerator\Config\Exclude; |
| 6 | + |
| 7 | +use Butschster\ContextGenerator\Application\Logger\LoggerPrefix; |
| 8 | +use Butschster\ContextGenerator\Config\Parser\ConfigParserPluginInterface; |
| 9 | +use Butschster\ContextGenerator\Config\Registry\RegistryInterface; |
| 10 | +use Psr\Log\LoggerInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Parser plugin for the 'exclude' section in configuration |
| 14 | + */ |
| 15 | +final readonly class ExcludeParserPlugin implements ConfigParserPluginInterface |
| 16 | +{ |
| 17 | + public function __construct( |
| 18 | + private ExcludeRegistryInterface $registry, |
| 19 | + #[LoggerPrefix(prefix: 'exclude-parser')] |
| 20 | + private ?LoggerInterface $logger = null, |
| 21 | + ) {} |
| 22 | + |
| 23 | + public function getConfigKey(): string |
| 24 | + { |
| 25 | + return 'exclude'; |
| 26 | + } |
| 27 | + |
| 28 | + public function supports(array $config): bool |
| 29 | + { |
| 30 | + return isset($config['exclude']) && \is_array($config['exclude']); |
| 31 | + } |
| 32 | + |
| 33 | + public function parse(array $config, string $rootPath): ?RegistryInterface |
| 34 | + { |
| 35 | + if (!$this->supports($config)) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + $excludeConfig = $config['exclude']; |
| 40 | + |
| 41 | + // Parse patterns |
| 42 | + if (isset($excludeConfig['patterns']) && \is_array($excludeConfig['patterns'])) { |
| 43 | + $this->parsePatterns($excludeConfig['patterns']); |
| 44 | + } |
| 45 | + |
| 46 | + // Parse paths |
| 47 | + if (isset($excludeConfig['paths']) && \is_array($excludeConfig['paths'])) { |
| 48 | + $this->parsePaths($excludeConfig['paths']); |
| 49 | + } |
| 50 | + |
| 51 | + $this->logger?->info('Parsed exclusion configuration', [ |
| 52 | + 'patternCount' => \count($this->registry->getPatterns()), |
| 53 | + ]); |
| 54 | + |
| 55 | + return $this->registry; |
| 56 | + } |
| 57 | + |
| 58 | + public function updateConfig(array $config, string $rootPath): array |
| 59 | + { |
| 60 | + // We don't need to modify the config, just return it as is |
| 61 | + return $config; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Parse glob pattern exclusions |
| 66 | + */ |
| 67 | + private function parsePatterns(array $patterns): void |
| 68 | + { |
| 69 | + foreach ($patterns as $pattern) { |
| 70 | + if (!\is_string($pattern) || empty($pattern)) { |
| 71 | + $this->logger?->warning('Invalid exclusion pattern, skipping', [ |
| 72 | + 'pattern' => $pattern, |
| 73 | + ]); |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + $this->registry->addPattern(new PatternExclusion($pattern)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Parse path exclusions |
| 83 | + */ |
| 84 | + private function parsePaths(array $paths): void |
| 85 | + { |
| 86 | + foreach ($paths as $path) { |
| 87 | + if (!\is_string($path) || empty($path)) { |
| 88 | + $this->logger?->warning('Invalid exclusion path, skipping', [ |
| 89 | + 'path' => $path, |
| 90 | + ]); |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + $this->registry->addPattern(new PathExclusion($path)); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments