|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
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\Core\JsonSchema\Command; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\OperationType; |
|
17
|
|
|
use ApiPlatform\Core\JsonSchema\SchemaFactoryInterface; |
|
18
|
|
|
use Symfony\Component\Console\Command\Command; |
|
19
|
|
|
use Symfony\Component\Console\Exception\InvalidOptionException; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
24
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Generates a resource JSON Schema. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Jacques Lefebvre <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
final class JsonSchemaGenerateCommand extends Command |
|
32
|
|
|
{ |
|
33
|
|
|
private $schemaFactory; |
|
34
|
|
|
private $formats; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(SchemaFactoryInterface $schemaFactory, array $formats) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->schemaFactory = $schemaFactory; |
|
39
|
|
|
$this->formats = array_keys($formats); |
|
40
|
|
|
|
|
41
|
|
|
parent::__construct(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function configure() |
|
48
|
|
|
{ |
|
49
|
|
|
$this |
|
50
|
|
|
->setName('api:json-schema:generate') |
|
51
|
|
|
->setDescription('Generates the JSON Schema for a resource operation.') |
|
52
|
|
|
->addArgument('resource', InputArgument::REQUIRED, 'The Fully Qualified Class Name (FQCN) of the resource') |
|
53
|
|
|
->addOption('itemOperation', null, InputOption::VALUE_REQUIRED, 'The item operation') |
|
54
|
|
|
->addOption('collectionOperation', null, InputOption::VALUE_REQUIRED, 'The collection operation') |
|
55
|
|
|
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The response format', (string) $this->formats[0]) |
|
56
|
|
|
->addOption('type', null, InputOption::VALUE_REQUIRED, 'The type of schema to generate (input or output)', 'input'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
63
|
|
|
{ |
|
64
|
|
|
$io = new SymfonyStyle($input, $output); |
|
65
|
|
|
|
|
66
|
|
|
/** @var string $resource */ |
|
67
|
|
|
$resource = $input->getArgument('resource'); |
|
68
|
|
|
/** @var ?string $itemOperation */ |
|
69
|
|
|
$itemOperation = $input->getOption('itemOperation'); |
|
70
|
|
|
/** @var ?string $collectionOperation */ |
|
71
|
|
|
$collectionOperation = $input->getOption('collectionOperation'); |
|
72
|
|
|
/** @var string $format */ |
|
73
|
|
|
$format = $input->getOption('format'); |
|
74
|
|
|
/** @var string $outputType */ |
|
75
|
|
|
$outputType = $input->getOption('type'); |
|
76
|
|
|
|
|
77
|
|
|
if (!\in_array($outputType, ['input', 'output'], true)) { |
|
78
|
|
|
$io->error('You can only use "input" or "output" for the "type" option'); |
|
79
|
|
|
|
|
80
|
|
|
return 1; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!\in_array($format, $this->formats, true)) { |
|
84
|
|
|
throw new InvalidOptionException(sprintf('The response format "%s" is not supported. Supported formats are : %s.', $format, implode(', ', $this->formats))); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** @var ?string $operationType */ |
|
88
|
|
|
$operationType = null; |
|
89
|
|
|
/** @var ?string $operationName */ |
|
90
|
|
|
$operationName = null; |
|
91
|
|
|
|
|
92
|
|
|
if ($itemOperation && $collectionOperation) { |
|
93
|
|
|
$io->error('You can only use one of "--itemOperation" and "--collectionOperation" options at the same time.'); |
|
94
|
|
|
|
|
95
|
|
|
return 1; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (null !== $itemOperation || null !== $collectionOperation) { |
|
99
|
|
|
$operationType = $itemOperation ? OperationType::ITEM : OperationType::COLLECTION; |
|
100
|
|
|
$operationName = $itemOperation ?? $collectionOperation; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$schema = $this->schemaFactory->buildSchema($resource, $format, 'output' === $outputType, $operationType, $operationName); |
|
104
|
|
|
|
|
105
|
|
|
if (null !== $operationType && null !== $operationName && !$schema->isDefined()) { |
|
106
|
|
|
$io->error(sprintf('There is no %ss defined for the operation "%s" of the resource "%s".', $outputType, $operationName, $resource)); |
|
107
|
|
|
|
|
108
|
|
|
return 1; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$io->text((string) json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
|
112
|
|
|
|
|
113
|
|
|
return 0; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|