Skip to content

Commit e5ff4ed

Browse files
authored
Improve Tokenizer Info console command (#979)
1 parent 2f631b2 commit e5ff4ed

5 files changed

Lines changed: 111 additions & 9 deletions

File tree

‎src/Framework/Bootloader/CommandBootloader.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Spiral\Boot\Bootloader\Bootloader;
99
use Spiral\Command\Encrypter;
1010
use Spiral\Command\Router;
11+
use Spiral\Command\Tokenizer;
1112
use Spiral\Command\Translator;
1213
use Spiral\Command\Views;
1314
use Spiral\Command\CleanCommand;
@@ -18,6 +19,7 @@
1819
use Spiral\Encrypter\EncryptionInterface;
1920
use Spiral\Files\FilesInterface;
2021
use Spiral\Router\RouterInterface;
22+
use Spiral\Tokenizer\ClassesInterface;
2123
use Spiral\Translator\Config\TranslatorConfig;
2224
use Spiral\Translator\TranslatorInterface;
2325
use Spiral\Views\ViewsInterface;
@@ -61,6 +63,10 @@ private function configureExtensions(ConsoleBootloader $console, ContainerInterf
6163
if ($container->has(RouterInterface::class)) {
6264
$console->addCommand(Router\ListCommand::class);
6365
}
66+
67+
if ($container->has(ClassesInterface::class)) {
68+
$console->addCommand(Tokenizer\InfoCommand::class);
69+
}
6470
}
6571

6672
private function configureTranslator(ConsoleBootloader $console): void

‎src/Framework/Command/Tokenizer/InfoCommand.php‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
namespace Spiral\Command\Tokenizer;
66

77
use Spiral\Boot\DirectoriesInterface;
8-
use Spiral\Console\Attribute\AsCommand;
98
use Spiral\Console\Command;
109
use Spiral\Tokenizer\Config\TokenizerConfig;
1110

12-
#[AsCommand(
13-
name: 'tokenizer:info',
14-
description: 'Get information about tokenizer directories to scan'
15-
)]
1611
final class InfoCommand extends Command
1712
{
13+
protected const NAME = 'tokenizer:info';
14+
protected const DESCRIPTION = 'Get information about tokenizer directories to scan';
15+
1816
public function perform(TokenizerConfig $config, DirectoriesInterface $dirs): int
1917
{
2018
$this->info('Included directories:');
@@ -45,9 +43,28 @@ public function perform(TokenizerConfig $config, DirectoriesInterface $dirs): in
4543

4644
$grid->render();
4745

46+
$this->newLine();
47+
48+
$this->info('Loaders:');
49+
$grid = $this->table(['Loader', 'Status']);
50+
51+
$grid->addRow(['Classes', $config->isLoadClassesEnabled()
52+
? '<info>enabled</>'
53+
: '<error>disabled</>. <comment>To enable, add "TOKENIZER_LOAD_CLASSES=true" to your .env file.</>']);
54+
$grid->addRow(['Enums', $config->isLoadEnumsEnabled()
55+
? '<info>enabled</>'
56+
: '<error>disabled</>. <comment>To enable, add "TOKENIZER_LOAD_ENUMS=true" to your .env file.</>']);
57+
$grid->addRow(
58+
['Interfaces', $config->isLoadInterfacesEnabled()
59+
? '<info>enabled</>'
60+
: '<error>disabled</>. <comment>To enable, add "TOKENIZER_LOAD_INTERFACES=true" to your .env file.</>']
61+
);
62+
63+
$grid->render();
64+
4865
$this->newLine();
4966
$this->info(
50-
\sprintf('Tokenizer cache: %s', $config->isCacheEnabled() ? '<success>enabled</>' : '<error>disabled</>'),
67+
\sprintf('Tokenizer cache: %s', $config->isCacheEnabled() ? '<info>enabled</>' : '<error>disabled</>'),
5168
);
5269
if (!$config->isCacheEnabled()) {
5370
$this->comment('To enable cache, add "TOKENIZER_CACHE_TARGETS=true" to your .env file.');

‎src/Tokenizer/src/Bootloader/TokenizerBootloader.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public function init(BinderInterface $binder, DirectoriesInterface $dirs, Enviro
7070
'directory' => $dirs->get('runtime') . 'cache/listeners',
7171
'enabled' => \filter_var($env->get('TOKENIZER_CACHE_TARGETS', false), \FILTER_VALIDATE_BOOL),
7272
],
73+
'load' => [
74+
'classes' => \filter_var($env->get('TOKENIZER_LOAD_CLASSES', true), \FILTER_VALIDATE_BOOL),
75+
'enums' => \filter_var($env->get('TOKENIZER_LOAD_ENUMS', false), \FILTER_VALIDATE_BOOL),
76+
'interfaces' => \filter_var($env->get('TOKENIZER_LOAD_INTERFACES', false), \FILTER_VALIDATE_BOOL),
77+
],
7378
],
7479
);
7580
}

‎src/Tokenizer/tests/Bootloader/TokenizerBootloaderTest.php‎

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717

1818
final class TokenizerBootloaderTest extends TestCase
1919
{
20-
#[DataProvider('readCacheDataProvider')]
20+
#[DataProvider('boolValuesDataProvider')]
2121
public function testCastingReadCacheEnvVariable(mixed $readCache, bool $expected): void
2222
{
2323
$binder = m::spy(BinderInterface::class);
2424
$dirs = m::mock(DirectoriesInterface::class);
2525
$env = m::mock(EnvironmentInterface::class);
2626

2727
$env->shouldReceive('get')->once()->with('TOKENIZER_CACHE_TARGETS', false)->andReturn($readCache);
28+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_CLASSES', true)->andReturn(true);
29+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_ENUMS', false)->andReturn(false);
30+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_INTERFACES', false)->andReturn(false);
2831

2932
$dirs->shouldReceive('get')->once()->with('app')->andReturn('app/');
3033
$dirs->shouldReceive('get')->once()->with('resources')->andReturn('resources/');
@@ -40,7 +43,73 @@ public function testCastingReadCacheEnvVariable(mixed $readCache, bool $expected
4043
$this->assertSame($expected, $initConfig['cache']['enabled']);
4144
}
4245

43-
public static function readCacheDataProvider(): \Traversable
46+
#[DataProvider('boolValuesDataProvider')]
47+
public function testCastingLoadClassesEnvVariable(mixed $classes, bool $expected): void
48+
{
49+
$dirs = m::mock(DirectoriesInterface::class);
50+
$env = m::mock(EnvironmentInterface::class);
51+
52+
$env->shouldReceive('get')->once()->with('TOKENIZER_CACHE_TARGETS', false)->andReturn(false);
53+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_CLASSES', true)->andReturn($classes);
54+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_ENUMS', false)->andReturn(false);
55+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_INTERFACES', false)->andReturn(false);
56+
57+
$dirs->shouldReceive('get')->once()->with('app')->andReturn('app/');
58+
$dirs->shouldReceive('get')->once()->with('resources')->andReturn('resources/');
59+
$dirs->shouldReceive('get')->once()->with('config')->andReturn('config/');
60+
$dirs->shouldReceive('get')->once()->with('runtime')->andReturn('runtime/');
61+
62+
$bootloader = new TokenizerBootloader($config = new ConfigManager(new DirectoryLoader('config')));
63+
$bootloader->init(m::spy(BinderInterface::class), $dirs, $env);
64+
65+
$this->assertSame($expected, $config->getConfig(TokenizerConfig::CONFIG)['load']['classes']);
66+
}
67+
68+
#[DataProvider('boolValuesDataProvider')]
69+
public function testCastingLoadEnumsEnvVariable(mixed $enums, bool $expected): void
70+
{
71+
$dirs = m::mock(DirectoriesInterface::class);
72+
$env = m::mock(EnvironmentInterface::class);
73+
74+
$env->shouldReceive('get')->once()->with('TOKENIZER_CACHE_TARGETS', false)->andReturn(false);
75+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_CLASSES', true)->andReturn(false);
76+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_ENUMS', false)->andReturn($enums);
77+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_INTERFACES', false)->andReturn(false);
78+
79+
$dirs->shouldReceive('get')->once()->with('app')->andReturn('app/');
80+
$dirs->shouldReceive('get')->once()->with('resources')->andReturn('resources/');
81+
$dirs->shouldReceive('get')->once()->with('config')->andReturn('config/');
82+
$dirs->shouldReceive('get')->once()->with('runtime')->andReturn('runtime/');
83+
84+
$bootloader = new TokenizerBootloader($config = new ConfigManager(new DirectoryLoader('config')));
85+
$bootloader->init(m::spy(BinderInterface::class), $dirs, $env);
86+
87+
$this->assertSame($expected, $config->getConfig(TokenizerConfig::CONFIG)['load']['enums']);
88+
}
89+
90+
#[DataProvider('boolValuesDataProvider')]
91+
public function testCastingLoadInterfacesEnvVariable(mixed $interfaces, bool $expected): void
92+
{
93+
$dirs = m::mock(DirectoriesInterface::class);
94+
$env = m::mock(EnvironmentInterface::class);
95+
96+
$env->shouldReceive('get')->once()->with('TOKENIZER_CACHE_TARGETS', false)->andReturn(false);
97+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_CLASSES', true)->andReturn(false);
98+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_ENUMS', false)->andReturn(false);
99+
$env->shouldReceive('get')->once()->with('TOKENIZER_LOAD_INTERFACES', false)->andReturn($interfaces);
100+
101+
$dirs->shouldReceive('get')->once()->with('app')->andReturn('app/');
102+
$dirs->shouldReceive('get')->once()->with('resources')->andReturn('resources/');
103+
$dirs->shouldReceive('get')->once()->with('config')->andReturn('config/');
104+
$dirs->shouldReceive('get')->once()->with('runtime')->andReturn('runtime/');
105+
106+
$bootloader = new TokenizerBootloader($config = new ConfigManager(new DirectoryLoader('config')));
107+
$bootloader->init(m::spy(BinderInterface::class), $dirs, $env);
108+
109+
$this->assertSame($expected, $config->getConfig(TokenizerConfig::CONFIG)['load']['interfaces']);
110+
}
111+
112+
public static function boolValuesDataProvider(): \Traversable
44113
{
45114
yield [true, true];
46115
yield [false, false];

‎tests/Framework/Bootloader/Tokenizer/TokenizerBootloaderTest.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ public function testConfig(): void
6666
'cache' => [
6767
'directory' => $this->getDirectoryByAlias('runtime') .'cache/listeners',
6868
'enabled' => false
69-
]
69+
],
70+
'load' => [
71+
'classes' => true,
72+
'enums' => false,
73+
'interfaces' => false,
74+
],
7075
]
7176
);
7277
}

0 commit comments

Comments
 (0)