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

api-platform / core / 11176269767

04 Oct 2024 08:02AM UTC coverage: 7.725% (+0.3%) from 7.441%
11176269767

push

github

soyuka
chore: remove useless require-dev

12744 of 164973 relevant lines covered (7.72%)

27.04 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
    private array $formats;
35

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

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

43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function configure(): void
47
    {
48
        $this
48✔
49
            ->setDescription('Generates the JSON Schema for a resource operation.')
48✔
50
            ->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource')
48✔
51
            ->addOption('operation', null, InputOption::VALUE_REQUIRED, 'The operation name')
48✔
52
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The response format', (string) $this->formats[0])
48✔
53
            ->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✔
54
    }
55

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

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

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

74
            return 1;
×
75
        }
76

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

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

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

86
            return 1;
×
87
        }
88

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

91
        return 0;
48✔
92
    }
93

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