-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathSchemaGenerator.php
151 lines (126 loc) · 5.35 KB
/
SchemaGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\Schema;
/**
* @internal
*/
final class SchemaGenerator
{
/**
* @param ViewHelperMetadata[] $viewHelpers
*/
public function generate(string $xmlNamespace, array $viewHelpers): \SimpleXMLElement
{
$file = $this->createXmlRootElement($xmlNamespace);
foreach ($viewHelpers as $metadata) {
$xsdElement = $file->addChild('xsd:element');
$xsdElement->addAttribute('name', $metadata->tagName);
$documentation = $metadata->documentation;
// Add deprecation information to ViewHelper documentation
if (isset($metadata->docTags['@deprecated'])) {
$documentation .= "\n@deprecated " . $metadata->docTags['@deprecated'];
}
$documentation = trim($documentation);
// Add documentation to xml
if ($documentation !== '') {
$xsdAnnotation = $xsdElement->addChild('xsd:annotation');
$xsdDocumentation = $xsdAnnotation->addChild('xsd:documentation');
$this->appendWithCdata($xsdDocumentation, $documentation);
}
$xsdComplexType = $xsdElement->addChild('xsd:complexType');
// Allow text as well as subelements
$xsdComplexType->addAttribute('mixed', 'true');
// Allow a sequence of arbitrary subelements of any type
$xsdSequence = $xsdComplexType->addChild('xsd:sequence');
$xsdAny = $xsdSequence->addChild('xsd:any');
$xsdAny->addAttribute('minOccurs', '0');
// Add argument definitions to xml
foreach ($metadata->argumentDefinitions as $argumentDefinition) {
$default = $argumentDefinition->getDefaultValue();
$type = $argumentDefinition->getType();
$xsdAttribute = $xsdComplexType->addChild('xsd:attribute');
$xsdAttribute->addAttribute('type', $this->convertPhpTypeToXsdType($type));
$xsdAttribute->addAttribute('name', $argumentDefinition->getName());
if ($argumentDefinition->isRequired()) {
$xsdAttribute->addAttribute('use', 'required');
} else {
$xsdAttribute->addAttribute('default', $this->createFluidRepresentation($default));
}
// Add PHP type to documentation text
// TODO check if there is a better field for this
$documentation = $argumentDefinition->getDescription();
$documentation .= "\n@type $type";
$documentation = trim($documentation);
// Add documentation for argument to xml
$xsdAnnotation = $xsdAttribute->addChild('xsd:annotation');
$xsdDocumentation = $xsdAnnotation->addChild('xsd:documentation');
$this->appendWithCdata($xsdDocumentation, $documentation);
}
if ($metadata->allowsArbitraryArguments) {
$xsdComplexType->addChild('xsd:anyAttribute');
}
}
return $file;
}
private function appendWithCdata(\SimpleXMLElement $parent, string $text): \SimpleXMLElement
{
$parentDomNode = dom_import_simplexml($parent);
$parentDomNode->appendChild($parentDomNode->ownerDocument->createCDATASection($text));
return simplexml_import_dom($parentDomNode);
}
private function createXmlRootElement(string $targetNamespace): \SimpleXMLElement
{
return new \SimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="' . $targetNamespace . '"></xsd:schema>',
);
}
private function convertPhpTypeToXsdType(string $type): string
{
switch ($type) {
case 'integer':
return 'xsd:integer';
case 'float':
return 'xsd:float';
case 'double':
return 'xsd:double';
case 'boolean':
case 'bool':
return 'xsd:boolean';
case 'string':
return 'xsd:string';
case 'array':
case 'mixed':
default:
return 'xsd:anySimpleType';
}
}
private function createFluidRepresentation(mixed $input, bool $isRoot = true): string
{
if (is_array($input)) {
$fluidArray = [];
foreach ($input as $key => $value) {
$fluidArray[] = $this->createFluidRepresentation($key, false) . ': ' . $this->createFluidRepresentation($value, false);
}
return '{' . implode(', ', $fluidArray) . '}';
}
if (is_string($input) && !$isRoot) {
return "'" . addcslashes($input, "'") . "'";
}
if (is_bool($input)) {
return ($input) ? 'true' : 'false';
}
if (is_null($input)) {
return 'NULL';
}
// Generally, this wouldn't be correct, since it's not the correct representation,
// but in the context of XSD files we merely need to provide *any* string representation
if (is_object($input)) {
return '';
}
return (string)$input;
}
}