From 607e4ef32890b4b3daf9d24f66fe18ff6f55601b Mon Sep 17 00:00:00 2001 From: spiralbot Date: Wed, 15 May 2024 17:45:36 +0000 Subject: [PATCH] Merge pull request #1093 from spiral/hotfix/tokenizer-scopes-config --- src/Bootloader/TokenizerBootloader.php | 42 ++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Bootloader/TokenizerBootloader.php b/src/Bootloader/TokenizerBootloader.php index d51b232..09a362e 100644 --- a/src/Bootloader/TokenizerBootloader.php +++ b/src/Bootloader/TokenizerBootloader.php @@ -9,6 +9,7 @@ use Spiral\Boot\EnvironmentInterface; use Spiral\Config\ConfiguratorInterface; use Spiral\Config\Patch\Append; +use Spiral\Config\Patch\Set; use Spiral\Core\Attribute\Singleton; use Spiral\Core\BinderInterface; use Spiral\Tokenizer\ClassesInterface; @@ -93,19 +94,48 @@ public function addDirectory(string $directory): void /** * Add directory for indexation into specific scope. + * @param non-empty-string $scope + * @param non-empty-string $directory */ public function addScopedDirectory(string $scope, string $directory): void { - if (!isset($this->config->getConfig(TokenizerConfig::CONFIG)['scopes'][$scope])) { + $this->ensureScopeExists($scope, 'directories'); + + $this->config->modify( + TokenizerConfig::CONFIG, + new Append('scopes.' . $scope . '.directories', null, $directory), + ); + } + + /** + * Exclude directory from indexation in specific scope. + * @param non-empty-string $scope + * @param non-empty-string $directory + */ + public function excludeScopedDirectory(string $scope, string $directory): void + { + $this->ensureScopeExists($scope, 'exclude'); + + $this->config->modify( + TokenizerConfig::CONFIG, + new Append('scopes.' . $scope . '.exclude', null, $directory), + ); + } + + private function ensureScopeExists(string $scope, string $section): void + { + if (!isset($this->config->getConfig(TokenizerConfig::CONFIG)['scopes'])) { $this->config->modify( TokenizerConfig::CONFIG, - new Append('scopes', $scope, []), + new Set('scopes', []), ); } - $this->config->modify( - TokenizerConfig::CONFIG, - new Append('scopes.' . $scope, null, $directory), - ); + if (!isset($this->config->getConfig(TokenizerConfig::CONFIG)['scopes'][$scope])) { + $this->config->modify( + TokenizerConfig::CONFIG, + new Append('scopes', $scope, [$section => []]), + ); + } } }