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

api-platform / core / 15063570239

16 May 2025 07:56AM UTC coverage: 27.494% (-0.001%) from 27.495%
15063570239

push

github

web-flow
fix: error formats (#7148)

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

11139 existing lines in 371 files now uncovered.

13476 of 49015 relevant lines covered (27.49%)

74.77 hits per line

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

95.56
/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\Attribute\AsCommand;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Helper\QuestionHelper;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Question\ChoiceQuestion;
24
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
25

26
#[AsCommand(name: 'debug:api-resource')]
27
final class DebugResourceCommand extends Command
28
{
29
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ClonerInterface $cloner, private $dumper)
30
    {
UNCOV
31
        parent::__construct();
8✔
32
    }
33

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

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

UNCOV
51
        $resourceCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
8✔
52

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

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

UNCOV
59
        $shortName = (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
6✔
60

61
        /** @var QuestionHelper $helper */
UNCOV
62
        $helper = $this->getHelper('question');
6✔
63

UNCOV
64
        $resources = [];
6✔
UNCOV
65
        foreach ($resourceCollection as $resource) {
6✔
UNCOV
66
            if ($resource->getUriTemplate()) {
6✔
UNCOV
67
                $resources[] = ($resource->getRoutePrefix() ?? '').$resource->getUriTemplate();
4✔
UNCOV
68
                continue;
4✔
69
            }
70

UNCOV
71
            foreach ($resource->getOperations() as $operation) {
6✔
UNCOV
72
                if ($operation->getUriTemplate()) {
6✔
UNCOV
73
                    $resources[] = ($resource->getRoutePrefix() ?? '').$operation->getUriTemplate();
2✔
UNCOV
74
                    break;
2✔
75
                }
76
            }
77
        }
78

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

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

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

UNCOV
98
        $questionOperation = new ChoiceQuestion(
6✔
UNCOV
99
            \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,
6✔
UNCOV
100
            $operations
6✔
UNCOV
101
        );
6✔
102

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

UNCOV
108
            return \defined(Command::class.'::SUCCESS') ? Command::SUCCESS : 0;
2✔
109
        }
110

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

UNCOV
114
        return \defined(Command::class.'::SUCCESS') ? Command::SUCCESS : 0;
4✔
115
    }
116
}
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