Skip to content

Commit

Permalink
Merge 8e50f5a into bdd8c9b
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelbigard committed Aug 31, 2021
2 parents bdd8c9b + 8e50f5a commit 87a4cdd
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/Bridge/Symfony/Bundle/Command/DebugResourceCommand.php
@@ -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;
}
}
5 changes: 5 additions & 0 deletions src/Core/Bridge/Symfony/Bundle/Resources/config/debug.xml
Expand Up @@ -20,5 +20,10 @@
<service id="debug.api_platform.data_persister" class="ApiPlatform\Core\Bridge\Symfony\Bundle\DataPersister\TraceableChainDataPersister" decorates="api_platform.data_persister">
<argument type="service" id="debug.api_platform.data_persister.inner" />
</service>

<service id="debug.api_platform.debug_resource.command" class="ApiPlatform\Bridge\Symfony\Bundle\Command\DebugResourceCommand">
<tag name="console.command" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
</service>
</services>
</container>
Expand Up @@ -291,6 +291,7 @@ public function testEnableProfilerWithDebug()
$containerBuilderProphecy->setDefinition('debug.api_platform.item_data_provider', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.api_platform.subresource_data_provider', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.api_platform.data_persister', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilderProphecy->setDefinition('debug.api_platform.debug_resource.command', Argument::type(Definition::class))->shouldBeCalled();
$containerBuilder = $containerBuilderProphecy->reveal();

$config = self::DEFAULT_CONFIG;
Expand Down Expand Up @@ -794,6 +795,7 @@ public function testKeepCachePoolClearerCacheWarmerWithDebug()
$containerBuilderProphecy->setDefinition('debug.api_platform.item_data_provider', Argument::type(Definition::class))->will(function () {});
$containerBuilderProphecy->setDefinition('debug.api_platform.subresource_data_provider', Argument::type(Definition::class))->will(function () {});
$containerBuilderProphecy->setDefinition('debug.api_platform.data_persister', Argument::type(Definition::class))->will(function () {});
$containerBuilderProphecy->setDefinition('debug.api_platform.debug_resource.command', Argument::type(Definition::class))->will(function () {});

$containerBuilder = $containerBuilderProphecy->reveal();

Expand Down

0 comments on commit 87a4cdd

Please sign in to comment.