Skip to content

Commit

Permalink
Merge commit 'b3d9511b716c7b8631b59796b83c60767596a6ba' into new-master
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 15, 2024
2 parents d7bf3da + b3d9511 commit e6cdc90
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 15 deletions.
42 changes: 36 additions & 6 deletions src/Tokenizer/src/Bootloader/TokenizerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 => []]),
);
}
}
}
50 changes: 41 additions & 9 deletions tests/Framework/Bootloader/Tokenizer/TokenizerBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function testInvocationsInterfaceBinding(): void
public function testClassLocatorInjector(): void
{
$this->assertTrue(
$this->getContainer()->hasInjector(ClassLocator::class)
$this->getContainer()->hasInjector(ClassLocator::class),
);
}

public function testInvocationLocatorInjector(): void
{
$this->assertTrue(
$this->getContainer()->hasInjector(InvocationLocator::class)
$this->getContainer()->hasInjector(InvocationLocator::class),
);
}

Expand All @@ -64,15 +64,15 @@ public function testConfig(): void
'migrations',
],
'cache' => [
'directory' => $this->getDirectoryByAlias('runtime') .'cache/listeners',
'enabled' => false
'directory' => $this->getDirectoryByAlias('runtime') . 'cache/listeners',
'enabled' => false,
],
'load' => [
'classes' => true,
'enums' => false,
'interfaces' => false,
],
]
],
);
}

Expand All @@ -90,22 +90,54 @@ public function testAddDirectory(): void
public function testAddScopedDirectoryWithNonExistScope(): void
{
$configs = new ConfigManager($this->createMock(LoaderInterface::class));
$configs->setDefaults(TokenizerConfig::CONFIG, ['scopes' => []]);
$configs->setDefaults(TokenizerConfig::CONFIG, ['scopes' => ['bar' => ['directories' => ['baz']]]]);

$bootloader = new TokenizerBootloader($configs);
$bootloader->addScopedDirectory('foo', 'bar');

$this->assertSame(['foo' => ['bar']], $configs->getConfig(TokenizerConfig::CONFIG)['scopes']);
$this->assertSame(['bar' => ['directories' => ['baz']], 'foo' => ['directories' => ['bar']]],
$configs->getConfig(TokenizerConfig::CONFIG)['scopes']);
}

public function testAddScopedDirectory(): void
{
$configs = new ConfigManager($this->createMock(LoaderInterface::class));
$configs->setDefaults(TokenizerConfig::CONFIG, ['scopes' => ['foo' => ['baz']]]);
$configs->setDefaults(
TokenizerConfig::CONFIG,
['scopes' => ['foo' => ['exclude' => ['baz'], 'directories' => ['baz']]]],
);

$bootloader = new TokenizerBootloader($configs);
$bootloader->addScopedDirectory('foo', 'bar');

$this->assertSame(['foo' => ['baz', 'bar']], $configs->getConfig(TokenizerConfig::CONFIG)['scopes']);
$this->assertSame(['foo' => ['exclude' => ['baz'], 'directories' => ['baz', 'bar']]],
$configs->getConfig(TokenizerConfig::CONFIG)['scopes']);
}

public function testExcludeScopedDirectoryWithNonExistScope(): void
{
$configs = new ConfigManager($this->createMock(LoaderInterface::class));
$configs->setDefaults(TokenizerConfig::CONFIG, ['scopes' => []]);

$bootloader = new TokenizerBootloader($configs);
$bootloader->excludeScopedDirectory('foo', 'bar');

$this->assertSame(['foo' => ['exclude' => ['bar']]],
$configs->getConfig(TokenizerConfig::CONFIG)['scopes']);
}

public function testExcludeScopedDirectory(): void
{
$configs = new ConfigManager($this->createMock(LoaderInterface::class));
$configs->setDefaults(
TokenizerConfig::CONFIG,
['scopes' => ['foo' => ['exclude' => ['baz'], 'directories' => ['baz']]]],
);

$bootloader = new TokenizerBootloader($configs);
$bootloader->excludeScopedDirectory('foo', 'bar');

$this->assertSame(['foo' => ['exclude' => ['baz', 'bar'], 'directories' => ['baz']]],
$configs->getConfig(TokenizerConfig::CONFIG)['scopes']);
}
}

0 comments on commit e6cdc90

Please sign in to comment.