Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/Bridge/Symfony/Bundle/Command/DebugResourceCommand.php
This file contains 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,104 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Bridge\Symfony\Bundle\Command; | ||
|
||
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
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\Question\ChoiceQuestion; | ||
|
||
class DebugResourceCommand extends Command | ||
{ | ||
protected static $defaultName = 'debug:api'; | ||
|
||
private $resourceMetadataCollectionFactory; | ||
|
||
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory) | ||
{ | ||
parent::__construct(); | ||
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Debug API Platform resources') | ||
->addArgument('class', InputArgument::REQUIRED, 'The class you want to debug'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$resourceClass = $input->getArgument('class'); | ||
|
||
$resourceCollection = $this->resourceMetadataCollectionFactory->create($resourceClass); | ||
|
||
if (0 === \count($resourceCollection)) { | ||
$output->writeln(sprintf('<error>No resources found for class %s</error>', $resourceClass)); | ||
|
||
return 1; | ||
} | ||
|
||
$shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass; | ||
|
||
$helper = $this->getHelper('question'); | ||
|
||
$resources = []; | ||
foreach ($resourceCollection as $resource) { | ||
$resources[] = $resource->getUriTemplate() ?: $resource->getOperations()->getIterator()->current()->getUriTemplate(); | ||
} | ||
|
||
if (\count($resourceCollection) > 1) { | ||
$questionResource = new ChoiceQuestion( | ||
sprintf('There are %d resources declared on the class %s, which one do you want to debug ? ', \count($resourceCollection), $shortName).\PHP_EOL, | ||
$resources | ||
); | ||
|
||
$answerResource = $helper->ask($input, $output, $questionResource); | ||
$resourceIndex = array_search($answerResource, $resources, true); | ||
$selectedResource = $resourceCollection[$resourceIndex]; | ||
} else { | ||
$selectedResource = $resourceCollection[0]; | ||
$output->writeln(sprintf('Class %s declares 1 resource.', $shortName).\PHP_EOL); | ||
} | ||
|
||
$operations = ['Debug the resource itself']; | ||
foreach ($selectedResource->getOperations() as $operationName => $operation) { | ||
$operations[] = $operationName; | ||
} | ||
|
||
$questionOperation = new ChoiceQuestion( | ||
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, | ||
$operations | ||
); | ||
|
||
$answerOperation = $helper->ask($input, $output, $questionOperation); | ||
if ('Debug the resource itself' === $answerOperation) { | ||
dump($selectedResource); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
dump($resourceCollection->getOperation($answerOperation)); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains 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
This file contains 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