Add a command to debug template events

This commit is contained in:
Kamil Kokot 2019-12-27 17:50:20 +01:00
parent c815e4c506
commit 4b969d3fcd
No known key found for this signature in database
GPG key ID: 7BD76F7054D93C89
2 changed files with 83 additions and 0 deletions

View file

@ -0,0 +1,78 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\Bundle\UiBundle\Command;
use Sylius\Bundle\UiBundle\Registry\TemplateBlock;
use Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
final class DebugTemplateEventCommand extends Command
{
protected static $defaultName = 'sylius:debug:template-event';
/** @var TemplateBlockRegistryInterface */
private $templateBlockRegistry;
public function __construct(TemplateBlockRegistryInterface $templateBlockRegistry)
{
parent::__construct();
$this->templateBlockRegistry = $templateBlockRegistry;
}
protected function configure(): void
{
$this
->setDescription('Debug template events and associated blocks')
->addArgument('event', InputArgument::OPTIONAL, 'Template event name', null)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$eventName = $input->getArgument('event');
if ($eventName === null) {
$io->title('List of template events');
$io->listing(array_keys($this->templateBlockRegistry->all()));
return 0;
}
$io->title(sprintf('Blocks registered for the template event "%s"', $eventName));
$io->table(
['Block name', 'Template', 'Priority', 'Enabled'],
array_map(
static function (TemplateBlock $templateBlock): array {
return [
$templateBlock->getName(),
$templateBlock->getTemplate(),
$templateBlock->getPriority(),
$templateBlock->isEnabled() ? 'TRUE' : 'FALSE',
];
},
$this->templateBlockRegistry->all()[$eventName] ?? []
)
);
return 0;
}
}

View file

@ -26,5 +26,10 @@
<argument type="service" id="Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface" />
<argument type="service" id="Sylius\Bundle\UiBundle\Renderer\TemplateBlockRendererInterface" />
</service>
<service id="Sylius\Bundle\UiBundle\Command\DebugTemplateEventCommand">
<argument type="service" id="Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface" />
<tag name="console.command" command="sylius:debug:template-event" />
</service>
</services>
</container>