-
Notifications
You must be signed in to change notification settings - Fork 31
[TwigHooks] Add debug command to display hooks and hookables #326
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
Merged
loic425
merged 5 commits into
Sylius:main
from
camilleislasse:feature/debug-twig-hooks-command
Feb 19, 2026
+603
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f30f6de
feat(twig-hooks): add debug command to display hooks and hookables
camilleislasse 37faf2e
fix(twig-hooks): apply review suggestions
camilleislasse 174541e
test(twig-hooks): add unit tests for DebugTwigHooksCommand
camilleislasse d08e25b
fix(twig-hooks): apply review suggestions
camilleislasse 7ed2b46
fix(twig-hooks): move command to Console/Command, rename getAllFor to…
camilleislasse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
|
||
| use Sylius\TwigHooks\Console\Command\DebugTwigHooksCommand; | ||
|
|
||
| return static function (ContainerConfigurator $configurator): void { | ||
| $services = $configurator->services(); | ||
|
|
||
| $services->set('sylius_twig_hooks.command.debug', DebugTwigHooksCommand::class) | ||
| ->args([ | ||
| service('sylius_twig_hooks.registry.hookables'), | ||
| ]) | ||
| ->tag('console.command') | ||
| ; | ||
| }; |
244 changes: 244 additions & 0 deletions
244
src/TwigHooks/src/Console/Command/DebugTwigHooksCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\TwigHooks\Console\Command; | ||
|
|
||
| use Sylius\TwigHooks\Hookable\AbstractHookable; | ||
| use Sylius\TwigHooks\Hookable\DisabledHookable; | ||
| use Sylius\TwigHooks\Hookable\HookableComponent; | ||
| use Sylius\TwigHooks\Hookable\HookableTemplate; | ||
| use Sylius\TwigHooks\Registry\HookablesRegistry; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Completion\CompletionInput; | ||
| use Symfony\Component\Console\Completion\CompletionSuggestions; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
| use Symfony\Component\VarExporter\VarExporter; | ||
|
|
||
| #[AsCommand(name: 'sylius:debug:twig-hooks', description: 'Debug twig hooks configuration.')] | ||
| final class DebugTwigHooksCommand extends Command | ||
| { | ||
| public function __construct( | ||
| private readonly HookablesRegistry $hookablesRegistry, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| $this | ||
| ->setDefinition([ | ||
| new InputArgument('name', InputArgument::OPTIONAL, 'A hook name or part of the hook name'), | ||
| new InputOption('all', 'a', InputOption::VALUE_NONE, 'Show all hookables including disabled ones'), | ||
| new InputOption('config', 'c', InputOption::VALUE_NONE, 'Show hookables configuration'), | ||
| ]) | ||
| ->setHelp( | ||
| <<<'EOF' | ||
| The <info>%command.name%</info> displays all Twig hooks in your application. | ||
|
|
||
| To list all hooks: | ||
|
|
||
| <info>php %command.full_name%</info> | ||
|
|
||
| To filter hooks by name: | ||
|
|
||
| <info>php %command.full_name% sylius_admin</info> | ||
|
|
||
| To get specific information about a hook: | ||
|
|
||
| <info>php %command.full_name% sylius_admin.product.index</info> | ||
|
|
||
| To include disabled hookables: | ||
|
|
||
| <info>php %command.full_name% sylius_admin.product.index --all</info> | ||
|
|
||
| To show hookables configuration: | ||
|
|
||
| <info>php %command.full_name% sylius_admin.product.index --config</info> | ||
| EOF | ||
| ); | ||
| } | ||
|
|
||
| public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void | ||
| { | ||
| if ($input->mustSuggestArgumentValuesFor('name')) { | ||
| $suggestions->suggestValues($this->hookablesRegistry->getHookNames()); | ||
| } | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $io = new SymfonyStyle($input, $output); | ||
| $name = $input->getArgument('name'); | ||
| /** @var bool $showAll */ | ||
| $showAll = $input->getOption('all'); | ||
| /** @var bool $showConfig */ | ||
| $showConfig = $input->getOption('config'); | ||
|
|
||
| $hookNames = $this->hookablesRegistry->getHookNames(); | ||
| sort($hookNames); | ||
|
|
||
| if (\is_string($name)) { | ||
| // Exact match - show details | ||
| if (\in_array($name, $hookNames, true)) { | ||
| $this->displayHookDetails($io, $name, $showAll, $showConfig); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| // Partial match - filter and show table or details (case-insensitive) | ||
| $filteredHooks = array_filter( | ||
| $hookNames, | ||
| static fn (string $hookName): bool => false !== stripos($hookName, $name), | ||
| ); | ||
|
|
||
| if (0 === \count($filteredHooks)) { | ||
| $io->warning(\sprintf('No hooks found matching "%s".', $name)); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| if (1 === \count($filteredHooks)) { | ||
| $this->displayHookDetails($io, reset($filteredHooks), $showAll, $showConfig); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| $this->displayHooksTable($io, $filteredHooks, $showAll); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| if (0 === \count($hookNames)) { | ||
| $io->warning('No hooks registered.'); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| $this->displayHooksTable($io, $hookNames, $showAll); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string> $hookNames | ||
| */ | ||
| private function displayHooksTable(SymfonyStyle $io, array $hookNames, bool $showAll): void | ||
| { | ||
| $rows = []; | ||
|
|
||
| foreach ($hookNames as $hookName) { | ||
| $hookables = $this->hookablesRegistry->getFor($hookName); | ||
| $enabledCount = \count(array_filter( | ||
| $hookables, | ||
| static fn (AbstractHookable $hookable): bool => !$hookable instanceof DisabledHookable, | ||
| )); | ||
| $disabledCount = \count($hookables) - $enabledCount; | ||
|
|
||
| $countDisplay = $showAll && $disabledCount > 0 | ||
| ? \sprintf('%d (%d disabled)', \count($hookables), $disabledCount) | ||
| : (string) $enabledCount; | ||
|
|
||
| $rows[] = [ | ||
| $hookName, | ||
| $countDisplay, | ||
| ]; | ||
| } | ||
|
|
||
| $io->table(['Hook', 'Hookables'], $rows); | ||
| $io->text(\sprintf('Total: %d hooks', \count($hookNames))); | ||
| } | ||
|
|
||
| private function displayHookDetails(SymfonyStyle $io, string $hookName, bool $showAll, bool $showConfig): void | ||
| { | ||
| $io->title($hookName); | ||
|
|
||
| $hookables = $this->hookablesRegistry->getFor($hookName); | ||
| if (!$showAll) { | ||
| $hookables = array_filter( | ||
| $hookables, | ||
| static fn (AbstractHookable $hookable): bool => !$hookable instanceof DisabledHookable, | ||
| ); | ||
| } | ||
|
|
||
| if (0 === \count($hookables)) { | ||
| $io->warning('No hookables registered for this hook.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $headers = ['Name', 'Type', 'Target', 'Priority']; | ||
| if ($showAll) { | ||
| $headers[] = 'Status'; | ||
| } | ||
| if ($showConfig) { | ||
| $headers[] = 'Configuration'; | ||
| } | ||
|
|
||
| $rows = []; | ||
| foreach ($hookables as $hookable) { | ||
| $row = [ | ||
| $hookable->name, | ||
| $this->getHookableType($hookable), | ||
| $this->getHookableTarget($hookable), | ||
| $hookable->priority(), | ||
| ]; | ||
|
|
||
| if ($showAll) { | ||
| $row[] = $hookable instanceof DisabledHookable ? 'disabled' : 'enabled'; | ||
| } | ||
|
|
||
| if ($showConfig) { | ||
| $row[] = $this->formatConfiguration($hookable->configuration); | ||
| } | ||
|
|
||
| $rows[] = $row; | ||
| } | ||
|
|
||
| $io->table($headers, $rows); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $configuration | ||
| */ | ||
| private function formatConfiguration(array $configuration): string | ||
| { | ||
| if (0 === \count($configuration)) { | ||
| return '-'; | ||
| } | ||
|
|
||
| return VarExporter::export($configuration); | ||
| } | ||
|
|
||
| private function getHookableType(AbstractHookable $hookable): string | ||
| { | ||
| return match (true) { | ||
| $hookable instanceof HookableTemplate => 'template', | ||
| $hookable instanceof HookableComponent => 'component', | ||
| default => '-', | ||
| }; | ||
| } | ||
|
|
||
| private function getHookableTarget(AbstractHookable $hookable): string | ||
| { | ||
| return match (true) { | ||
| $hookable instanceof HookableTemplate => $hookable->template, | ||
| $hookable instanceof HookableComponent => $hookable->component, | ||
| default => '-', | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.