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

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 hits per line

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

80.0
/src/JsonSchema/Command/JsonSchemaGenerateCommand.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\JsonSchema\Command;
15

16
use ApiPlatform\JsonSchema\Schema;
17
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
18
use ApiPlatform\Metadata\HttpOperation;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Exception\InvalidOptionException;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26

27
/**
28
 * Generates a resource JSON Schema.
29
 *
30
 * @author Jacques Lefebvre <jacques@les-tilleuls.coop>
31
 */
32
final class JsonSchemaGenerateCommand extends Command
33
{
34
    // @noRector \Rector\Php81\Rector\Property\ReadOnlyPropertyRector
35
    private array $formats;
36

37
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory, array $formats)
38
    {
39
        $this->formats = array_keys($formats);
48✔
40

41
        parent::__construct();
48✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function configure(): void
48
    {
49
        $this
48✔
50
            ->setDescription('Generates the JSON Schema for a resource operation.')
48✔
51
            ->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource')
48✔
52
            ->addOption('operation', null, InputOption::VALUE_REQUIRED, 'The operation name')
48✔
53
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The response format', (string) $this->formats[0])
48✔
54
            ->addOption('type', null, InputOption::VALUE_REQUIRED, \sprintf('The type of schema to generate (%s or %s)', Schema::TYPE_INPUT, Schema::TYPE_OUTPUT), Schema::TYPE_INPUT);
48✔
55
    }
56

57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function execute(InputInterface $input, OutputInterface $output): int
61
    {
62
        $io = new SymfonyStyle($input, $output);
48✔
63

64
        /** @var string $resource */
65
        $resource = $input->getArgument('resource');
48✔
66
        $operation = $input->getOption('operation');
48✔
67
        /** @var string $format */
68
        $format = $input->getOption('format');
48✔
69
        /** @var string $type */
70
        $type = $input->getOption('type');
48✔
71

72
        if (!\in_array($type, [Schema::TYPE_INPUT, Schema::TYPE_OUTPUT], true)) {
48✔
NEW
73
            $io->error(\sprintf('You can only use "%s" or "%s" for the "type" option', Schema::TYPE_INPUT, Schema::TYPE_OUTPUT));
×
74

75
            return 1;
×
76
        }
77

78
        if (!\in_array($format, $this->formats, true)) {
48✔
NEW
79
            throw new InvalidOptionException(\sprintf('The response format "%s" is not supported. Supported formats are : %s.', $format, implode(', ', $this->formats)));
×
80
        }
81

82
        $schema = $this->schemaFactory->buildSchema($resource, $format, $type, $operation ? (new class extends HttpOperation {})->withName($operation) : null);
48✔
83

84
        if (!$schema->isDefined()) {
48✔
NEW
85
            $io->error(\sprintf('There is no %s defined for the operation "%s" of the resource "%s".', $type, $operation, $resource));
×
86

87
            return 1;
×
88
        }
89

90
        $io->text((string) json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
48✔
91

92
        return 0;
48✔
93
    }
94

95
    public static function getDefaultName(): string
96
    {
97
        return 'api:json-schema:generate';
51✔
98
    }
99
}
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

© 2026 Coveralls, Inc