Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Listeners to the Tokenizer Info console command #1042

Merged
merged 4 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ enabled:
- linebreak_after_opening_tag
- single_quote
- no_blank_lines_after_phpdoc
- unary_operator_spaces
- no_useless_else
- no_useless_return
- trailing_comma_in_multiline_array
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## Unreleased

- **Other Features**
- Added Tokenizer Listeners to the `Spiral\Command\Tokenizer\InfoCommand` console command.

## 3.11.0 - 2023-12-21

- **Other Features**
Expand Down
27 changes: 25 additions & 2 deletions src/Framework/Command/Tokenizer/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
use Spiral\Boot\DirectoriesInterface;
use Spiral\Console\Command;
use Spiral\Tokenizer\Config\TokenizerConfig;
use Spiral\Tokenizer\TokenizerListenerRegistryInterface;

final class InfoCommand extends Command
{
protected const NAME = 'tokenizer:info';
protected const DESCRIPTION = 'Get information about tokenizer directories to scan';

public function perform(TokenizerConfig $config, DirectoriesInterface $dirs): int
{
public function perform(
TokenizerConfig $config,
DirectoriesInterface $dirs,
TokenizerListenerRegistryInterface $listenerRegistry
): int {
$this->info('Included directories:');
$grid = $this->table(['Directory', 'Scope']);
foreach ($config->getDirectories() as $directory) {
Expand Down Expand Up @@ -62,6 +66,25 @@

$grid->render();

$listeners = \method_exists($listenerRegistry, 'getListenerClasses')
? $listenerRegistry->getListenerClasses()
: [];

Check warning on line 71 in src/Framework/Command/Tokenizer/InfoCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Framework/Command/Tokenizer/InfoCommand.php#L71

Added line #L71 was not covered by tests

$this->newLine();

$this->info('Listeners:');
$grid = $this->table(['Registered Listener', 'File']);
foreach ($listeners as $listener) {
$grid->addRow([
$listener,
\str_replace($dirs->get('root'), '', (new \ReflectionClass($listener))->getFileName()),
]);
}

$grid->render();

$this->newLine();

$this->newLine();
$this->info(
\sprintf('Tokenizer cache: %s', $config->isCacheEnabled() ? '<info>enabled</>' : '<error>disabled</>'),
Expand Down
12 changes: 12 additions & 0 deletions src/Tokenizer/src/Bootloader/TokenizerListenerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ final class TokenizerListenerBootloader extends Bootloader implements
/** @var TokenizationListenerInterface[] */
private array $listeners = [];

/** @var array<class-string<TokenizationListenerInterface>> */
private array $listenerClasses = [];

public function addListener(TokenizationListenerInterface $listener): void
{
$this->listeners[] = $listener;
$this->listenerClasses[] = $listener::class;
}

public function init(AbstractKernel $kernel): void
Expand Down Expand Up @@ -86,6 +90,14 @@ public function initCachedInterfacesLoader(
return $this->makeCachedLoader($factory, $config, CachedInterfacesLoader::class);
}

/**
* @return array<class-string<TokenizationListenerInterface>>
*/
public function getListenerClasses(): array
{
return $this->listenerClasses;
}

/**
* @template T
*
Expand Down
1 change: 1 addition & 0 deletions src/Tokenizer/src/TokenizerListenerRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* It contains all listeners that will be noticed about found classes by a class locator.
* @method getListenerClasses(): array
*/
interface TokenizerListenerRegistryInterface
{
Expand Down
45 changes: 45 additions & 0 deletions tests/Framework/Console/Command/Tokenizer/InfoCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Framework\Console\Command\Tokenizer;

use Spiral\Console\CommandLocatorListener;
use Spiral\Prototype\PrototypeLocatorListener;
use Spiral\Queue\JobHandlerLocatorListener;
use Spiral\Queue\SerializerLocatorListener;
use Spiral\Tests\Framework\ConsoleTestCase;

final class InfoCommandTest extends ConsoleTestCase
{
public function testInfoCommand(): void
{
$this->assertConsoleCommandOutputContainsStrings(command: 'tokenizer:info', strings: [
'Included directories:',
'app',
'Excluded directories:',
'app/resources/',
'app/config/',
'tests',
'migrations',
'Loaders:',
'Classes',
'enabled',
'Enums',
'disabled. To enable, add "TOKENIZER_LOAD_ENUMS=true" to your .env file.',
'Interfaces',
'disabled. To enable, add "TOKENIZER_LOAD_INTERFACES=true" to your .env file.',
'Listeners:',
CommandLocatorListener::class,
'Console/src/CommandLocatorListener.php',
JobHandlerLocatorListener::class,
'Queue/src/JobHandlerLocatorListener.php',
SerializerLocatorListener::class,
'Queue/src/SerializerLocatorListener.php',
PrototypeLocatorListener::class,
'Prototype/src/PrototypeLocatorListener.php',
'Tokenizer cache: disabled',
'To enable cache, add "TOKENIZER_CACHE_TARGETS=true" to your .env file.',
]);
}
}
Loading