• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/src/Symfony/Bundle/Command/DebugResourceCommand.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Symfony\Bundle\Command;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Helper\QuestionHelper;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Question\ChoiceQuestion;
23
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
24

25
final class DebugResourceCommand extends Command
26
{
27
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ClonerInterface $cloner, private $dumper)
28
    {
UNCOV
29
        parent::__construct();
12✔
30
    }
31

32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function configure(): void
36
    {
UNCOV
37
        $this
12✔
UNCOV
38
            ->setDescription('Debug API Platform resources')
12✔
UNCOV
39
            ->addArgument('class', InputArgument::REQUIRED, 'The class you want to debug');
12✔
40
    }
41

42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output): int
46
    {
UNCOV
47
        $resourceClass = $input->getArgument('class');
12✔
48

UNCOV
49
        $resourceCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
12✔
50

UNCOV
51
        if (0 === \count($resourceCollection)) {
9✔
52
            $output->writeln(sprintf('<error>No resources found for class %s</error>', $resourceClass));
×
53

54
            return \defined(Command::class.'::INVALID') ? Command::INVALID : 2;
×
55
        }
56

UNCOV
57
        $shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
9✔
58

59
        /** @var QuestionHelper $helper */
UNCOV
60
        $helper = $this->getHelper('question');
9✔
61

UNCOV
62
        $resources = [];
9✔
UNCOV
63
        foreach ($resourceCollection as $resource) {
9✔
UNCOV
64
            if ($resource->getUriTemplate()) {
9✔
UNCOV
65
                $resources[] = $resource->getUriTemplate();
6✔
UNCOV
66
                continue;
6✔
67
            }
68

UNCOV
69
            foreach ($resource->getOperations() as $operation) {
9✔
UNCOV
70
                if ($operation->getUriTemplate()) {
9✔
UNCOV
71
                    $resources[] = $operation->getUriTemplate();
3✔
UNCOV
72
                    break;
3✔
73
                }
74
            }
75
        }
76

UNCOV
77
        if (\count($resourceCollection) > 1) {
9✔
UNCOV
78
            $questionResource = new ChoiceQuestion(
6✔
UNCOV
79
                sprintf('There are %d resources declared on the class %s, which one do you want to debug ? ', \count($resourceCollection), $shortName).\PHP_EOL,
6✔
UNCOV
80
                $resources
6✔
UNCOV
81
            );
6✔
82

UNCOV
83
            $answerResource = $helper->ask($input, $output, $questionResource);
6✔
UNCOV
84
            $resourceIndex = array_search($answerResource, $resources, true);
6✔
UNCOV
85
            $selectedResource = $resourceCollection[$resourceIndex];
6✔
86
        } else {
UNCOV
87
            $selectedResource = $resourceCollection[0];
3✔
UNCOV
88
            $output->writeln(sprintf('Class %s declares 1 resource.', $shortName).\PHP_EOL);
3✔
89
        }
90

UNCOV
91
        $operations = ['Debug the resource itself'];
9✔
UNCOV
92
        foreach ($selectedResource->getOperations() as $operationName => $operation) {
9✔
UNCOV
93
            $operations[] = $operationName;
9✔
94
        }
95

UNCOV
96
        $questionOperation = new ChoiceQuestion(
9✔
UNCOV
97
            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,
9✔
UNCOV
98
            $operations
9✔
UNCOV
99
        );
9✔
100

UNCOV
101
        $answerOperation = $helper->ask($input, $output, $questionOperation);
9✔
UNCOV
102
        if ('Debug the resource itself' === $answerOperation) {
9✔
UNCOV
103
            $this->dumper->dump($this->cloner->cloneVar($selectedResource));
3✔
UNCOV
104
            $output->writeln('Successfully dumped the selected resource');
3✔
105

UNCOV
106
            return \defined(Command::class.'::SUCCESS') ? Command::SUCCESS : 0;
3✔
107
        }
108

UNCOV
109
        $this->dumper->dump($this->cloner->cloneVar($resourceCollection->getOperation($answerOperation)));
6✔
UNCOV
110
        $output->writeln('Successfully dumped the selected operation');
6✔
111

UNCOV
112
        return \defined(Command::class.'::SUCCESS') ? Command::SUCCESS : 0;
6✔
113
    }
114

115
    public static function getDefaultName(): string
116
    {
UNCOV
117
        return 'debug:api-resource';
15✔
118
    }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc