Skip to content

Instantly share code, notes, and snippets.

@vvasiloi
Created May 5, 2019 00:10
Show Gist options
  • Save vvasiloi/e97bd9aa1d5de8abf262d9046c2062fb to your computer and use it in GitHub Desktop.
Save vvasiloi/e97bd9aa1d5de8abf262d9046c2062fb to your computer and use it in GitHub Desktop.
Sylius resource routing debug
<?php
namespace App\Command;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Component\Resource\Metadata\RegistryInterface;
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Command\Command;
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\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
final class SyliusResourceRouterDebugCommand extends Command
{
/**
* @var RouterInterface
*/
private $router;
/**
* @var RegistryInterface
*/
private $resourceRegistry;
/**
* @param RouterInterface $router
* @param RegistryInterface $resourceRegistry
*/
public function __construct(RouterInterface $router, /** @sylius.resource_registry **/ RegistryInterface $resourceRegistry)
{
parent::__construct();
$this->router = $router;
$this->resourceRegistry = $resourceRegistry;
}
protected function configure(): void
{
$this
->setName('sylius:debug:router')
->addArgument('resource', InputArgument::REQUIRED, 'Resource alias')
->addOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt')
->addOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)')
->addOption('detailed', null, InputOption::VALUE_NONE, 'To output detailed route information')
;
}
protected function execute(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
$alias = $input->getArgument('resource');
$helper = new DescriptorHelper();
$metadata = $this->resourceRegistry->get($alias);
$routes = $this->router->getRouteCollection();
$matchingRoutes = new RouteCollection();
foreach ($routes as $routeName => $route) {
if (!$route->hasDefault('_sylius')) {
continue;
}
$sylius = $route->getDefault('_sylius');
$pattern = $this->getRouteNamePattern($metadata, $sylius['section'] ?? null);
if (1 === preg_match($pattern, $routeName)) {
$matchingRoutes->add($routeName, $route);
}
}
if ($input->getOption('detailed')) {
foreach ($matchingRoutes as $routeName => $route) {
$helper->describe($io, $route, [
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'name' => $routeName,
'output' => $io,
]);
}
} else {
$helper->describe($io, $matchingRoutes, [
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
'output' => $io,
]);
}
}
/**
* @param MetadataInterface $metadata
* @param string|null $prefix
* @return string
*/
private function getRouteNamePattern(MetadataInterface $metadata, string $prefix = null): string
{
$actions = ['show', 'index', 'create', 'update', 'delete', 'bulk_delete'];
return sprintf(
'/^%s_%s%s_(?:%s)$/',
$metadata->getApplicationName(),
$prefix ? $prefix . '_' : '',
$metadata->getName(),
implode('|', $actions)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment