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

api-platform / core / 10523796051

23 Aug 2024 10:00AM UTC coverage: 6.984% (-0.7%) from 7.707%
10523796051

push

github

dunglas
chore(laravel): remove duplicated ItemNormalizer definition

11317 of 162035 relevant lines covered (6.98%)

14.3 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
    {
29
        parent::__construct();
12✔
30
    }
31

32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function configure(): void
36
    {
37
        $this
12✔
38
            ->setDescription('Debug API Platform resources')
12✔
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
    {
47
        $resourceClass = $input->getArgument('class');
12✔
48

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

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

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

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

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

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

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

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

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

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

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

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

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

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

115
    public static function getDefaultName(): string
116
    {
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