|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[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 | +namespace Symfony\Component\Config\Definition; |
| 13 | + |
| 14 | +use Symfony\Component\Config\Definition\Builder\ExprBuilder; |
| 15 | + |
| 16 | +final readonly class JsonSchemaGenerator |
| 17 | +{ |
| 18 | + public function __construct(private string $outputPath) |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + public function build(NodeInterface $node, array $schema = []): void |
| 23 | + { |
| 24 | + $schema = array_replace_recursive([ |
| 25 | + '$schema' => 'http://json-schema.org/draft-06/schema#', |
| 26 | + 'definitions' => [ |
| 27 | + 'param' => [ |
| 28 | + '$comment' => 'Container parameter', |
| 29 | + 'type' => 'string', |
| 30 | + 'pattern' => '^%[^%]+%$', |
| 31 | + ], |
| 32 | + ], |
| 33 | + ], $this->buildSingleNode($node, allowParam: false), $schema); |
| 34 | + |
| 35 | + file_put_contents($this->outputPath, json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_THROW_ON_ERROR)); |
| 36 | + } |
| 37 | + |
| 38 | + private function buildArrayNode(ArrayNode $node): array |
| 39 | + { |
| 40 | + $schema = [ |
| 41 | + 'type' => 'object', |
| 42 | + 'additionalProperties' => $node->shouldIgnoreExtraKeys(), |
| 43 | + ]; |
| 44 | + |
| 45 | + foreach ($node->getChildren() as $child) { |
| 46 | + $schema['properties'][$child->getName()] = $this->buildSingleNode($child); |
| 47 | + |
| 48 | + if ($child->isRequired()) { |
| 49 | + $schema['required'][] = $child->getName(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return $schema; |
| 54 | + } |
| 55 | + |
| 56 | + private function buildSingleNode(NodeInterface $node, bool $allowParam = true): array|\ArrayObject |
| 57 | + { |
| 58 | + $schema = match (\count($types = $this->createSubSchemas($node, $allowParam))) { |
| 59 | + 1 => $types[0], |
| 60 | + default => ['anyOf' => $types], |
| 61 | + }; |
| 62 | + |
| 63 | + if ($node->hasDefaultValue()) { |
| 64 | + $schema['default'] = $node->getDefaultValue(); |
| 65 | + } |
| 66 | + |
| 67 | + if ($node instanceof BaseNode) { |
| 68 | + if ($info = $node->getInfo()) { |
| 69 | + $schema['description'] = $info; |
| 70 | + } |
| 71 | + |
| 72 | + if ($node->isDeprecated()) { |
| 73 | + $schema['deprecated'] = true; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return $schema; |
| 78 | + } |
| 79 | + |
| 80 | + private function createSubSchemas(NodeInterface $node, bool $allowParam = true): array |
| 81 | + { |
| 82 | + $paramTypes = []; |
| 83 | + |
| 84 | + $getType = fn ($value) => match (get_debug_type($value)) { |
| 85 | + 'string' => 'string', |
| 86 | + 'int' => 'integer', |
| 87 | + default => null, |
| 88 | + }; |
| 89 | + |
| 90 | + $removeNulls = fn (array $array) => array_filter($array, fn ($value) => null !== $value); |
| 91 | + |
| 92 | + if ($node instanceof BaseNode && !$node instanceof StringNode && \in_array(ExprBuilder::TYPE_STRING, $node->getNormalizedTypes(), true)) { |
| 93 | + $paramTypes[] = ['type' => 'string']; |
| 94 | + } |
| 95 | + |
| 96 | + $schema = match (true) { |
| 97 | + $node instanceof BooleanNode => [['type' => 'boolean']], |
| 98 | + $node instanceof IntegerNode => [$removeNulls(['type' => 'integer', 'minimum' => $node->getMin(), 'maximum' => $node->getMax()])], |
| 99 | + $node instanceof NumericNode => [$removeNulls(['type' => 'number', 'minimum' => $node->getMin(), 'maximum' => $node->getMax()])], |
| 100 | + $node instanceof StringNode => [['type' => 'string']], |
| 101 | + $node instanceof EnumNode => [$removeNulls( |
| 102 | + ($nonEnumValues = array_values(array_filter($node->getValues(), fn ($v) => !$v instanceof \UnitEnum))) ? [ |
| 103 | + 'type' => array_values( |
| 104 | + array_unique(array_filter(array_map($getType, $nonEnumValues))) |
| 105 | + ), |
| 106 | + 'enum' => $nonEnumValues, |
| 107 | + ] : [] |
| 108 | + )], |
| 109 | + $node instanceof PrototypedArrayNode => $this->buildPrototypedArray($node), |
| 110 | + $node instanceof ArrayNode => [$this->buildArrayNode($node)], |
| 111 | + $node instanceof ScalarNode => [['type' => ['string', 'number', 'boolean']]], |
| 112 | + default => null, |
| 113 | + }; |
| 114 | + |
| 115 | + if ($schema) { |
| 116 | + array_push($paramTypes, ...$schema); |
| 117 | + if ($allowParam) { |
| 118 | + $paramTypes[] = ['$ref' => '#/definitions/param']; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if ( |
| 123 | + !($node instanceof NumericNode) && ( |
| 124 | + ($node instanceof BooleanNode && $node->isNullable()) |
| 125 | + || ($node->isRequired() && $node instanceof StringNode && $node->getAllowEmptyValue()) |
| 126 | + || (!$node->isRequired() && ($node->hasDefaultValue() || ($node instanceof VariableNode && $node->getAllowEmptyValue()))) |
| 127 | + || ($node->hasDefaultValue() && null === $node->getDefaultValue()) |
| 128 | + ) |
| 129 | + ) { |
| 130 | + foreach ($paramTypes as &$subSchema) { |
| 131 | + if (!isset($subSchema['type'])) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + $subSchema['type'] = (array) $subSchema['type']; |
| 136 | + $subSchema['type'][] = 'null'; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return $paramTypes ?: [new \ArrayObject()]; |
| 141 | + } |
| 142 | + |
| 143 | + private function buildPrototypedArray(PrototypedArrayNode $node): array |
| 144 | + { |
| 145 | + $items = $this->buildSingleNode($node->getPrototype()); |
| 146 | + |
| 147 | + $schema = [ |
| 148 | + ['type' => 'array', 'items' => $items], |
| 149 | + ['type' => 'object', 'additionalProperties' => $items], |
| 150 | + ]; |
| 151 | + |
| 152 | + if ($node->getMinNumberOfElements() > 0) { |
| 153 | + $schema[0]['minItems'] = $schema[1]['minItems'] = $node->getMinNumberOfElements(); |
| 154 | + } |
| 155 | + |
| 156 | + return $schema; |
| 157 | + } |
| 158 | +} |
0 commit comments