|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Bridge\Symfony\Bundle\Command; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Symfony\Component\Console\Question\ChoiceQuestion; |
| 22 | +use Symfony\Component\VarDumper\Cloner\ClonerInterface; |
| 23 | + |
| 24 | +final class DebugResourceCommand extends Command |
| 25 | +{ |
| 26 | + protected static $defaultName = 'debug:api-resource'; |
| 27 | + |
| 28 | + private $resourceMetadataCollectionFactory; |
| 29 | + private $cloner; |
| 30 | + private $dumper; |
| 31 | + |
| 32 | + public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ClonerInterface $cloner, $dumper) |
| 33 | + { |
| 34 | + parent::__construct(); |
| 35 | + $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory; |
| 36 | + $this->cloner = $cloner; |
| 37 | + $this->dumper = $dumper; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritdoc} |
| 42 | + */ |
| 43 | + protected function configure(): void |
| 44 | + { |
| 45 | + $this |
| 46 | + ->setDescription('Debug API Platform resources') |
| 47 | + ->addArgument('class', InputArgument::REQUIRED, 'The class you want to debug'); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 54 | + { |
| 55 | + $resourceClass = $input->getArgument('class'); |
| 56 | + |
| 57 | + $resourceCollection = $this->resourceMetadataCollectionFactory->create($resourceClass); |
| 58 | + |
| 59 | + if (0 === \count($resourceCollection)) { |
| 60 | + $output->writeln(sprintf('<error>No resources found for class %s</error>', $resourceClass)); |
| 61 | + |
| 62 | + return Command::INVALID; |
| 63 | + } |
| 64 | + |
| 65 | + $shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass; |
| 66 | + |
| 67 | + $helper = $this->getHelper('question'); |
| 68 | + |
| 69 | + $resources = []; |
| 70 | + foreach ($resourceCollection as $resource) { |
| 71 | + if ($resource->getUriTemplate()) { |
| 72 | + $resources[] = $resource->getUriTemplate(); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + foreach ($resource->getOperations() as $operation) { |
| 77 | + if ($operation->getUriTemplate()) { |
| 78 | + $resources[] = $operation->getUriTemplate(); |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (\count($resourceCollection) > 1) { |
| 85 | + $questionResource = new ChoiceQuestion( |
| 86 | + sprintf('There are %d resources declared on the class %s, which one do you want to debug ? ', \count($resourceCollection), $shortName).\PHP_EOL, |
| 87 | + $resources |
| 88 | + ); |
| 89 | + |
| 90 | + $answerResource = $helper->ask($input, $output, $questionResource); |
| 91 | + $resourceIndex = array_search($answerResource, $resources, true); |
| 92 | + $selectedResource = $resourceCollection[$resourceIndex]; |
| 93 | + } else { |
| 94 | + $selectedResource = $resourceCollection[0]; |
| 95 | + $output->writeln(sprintf('Class %s declares 1 resource.', $shortName).\PHP_EOL); |
| 96 | + } |
| 97 | + |
| 98 | + $operations = ['Debug the resource itself']; |
| 99 | + foreach ($selectedResource->getOperations() as $operationName => $operation) { |
| 100 | + $operations[] = $operationName; |
| 101 | + } |
| 102 | + |
| 103 | + $questionOperation = new ChoiceQuestion( |
| 104 | + sprintf('There are %d operation%s declared on the resource, which one do you want to debug ? ', $selectedResource->getOperations()->count(), $selectedResource->getOperations()->count() > 1 ? 's' : '').\PHP_EOL, |
| 105 | + $operations |
| 106 | + ); |
| 107 | + |
| 108 | + $answerOperation = $helper->ask($input, $output, $questionOperation); |
| 109 | + if ('Debug the resource itself' === $answerOperation) { |
| 110 | + $this->dumper->dump($this->cloner->cloneVar($selectedResource)); |
| 111 | + $output->writeln('Successfully dumped the selected resource'); |
| 112 | + |
| 113 | + return Command::SUCCESS; |
| 114 | + } |
| 115 | + |
| 116 | + $this->dumper->dump($this->cloner->cloneVar($resourceCollection->getOperation($answerOperation))); |
| 117 | + $output->writeln('Successfully dumped the selected operation'); |
| 118 | + |
| 119 | + return Command::SUCCESS; |
| 120 | + } |
| 121 | +} |
0 commit comments