Skip to content

Commit

Permalink
Merge pull request #1042 from spiral/feature/listeners-list
Browse files Browse the repository at this point in the history
Adding Listeners to the Tokenizer Info console command
  • Loading branch information
butschster authored Dec 25, 2023
2 parents ccffb6c + 0a2faa1 commit f76682f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
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 @@ public function perform(TokenizerConfig $config, DirectoriesInterface $dirs): in

$grid->render();

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

$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.',
]);
}
}

0 comments on commit f76682f

Please sign in to comment.