src/bin/convertObjectsToSchema.php
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
4. $shortopts .= "o:"; // Required - path to object definitions
Code on global space
Code on global space
Code on global space
5. $shortopts .= "p:"; // Required - path to property definitions
Code on global space
Code on global space
Code on global space
6. $shortopts .= "s:"; // Required - path for generated Open API schema objects
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
8. $options = getopt($shortopts);
10. // Objects and their related properties are defined here.
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
11. $objectDir = realpath($options['o']);
13. // Properties are stored here.
14. // They can be extended and reused across objects.
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
15. $propertyDir = realpath($options['p']);
17. // A writeable directory for the OpenAPI schema definitions
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
18. $schemaDir = realpath($options['s']);
19. // Attempt creating directory if needed
Code on global space
Code on global space
Code on global space
Code on global space
20. if (false === $schemaDir) {
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
21. mkdir($options['s']);
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
22. $schemaDir = realpath($options['s']);
25. // Create a directory iterator for the defined objects directory
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
26. $dir = new DirectoryIterator($objectDir);
28. // Iterate through object definitions
Code on global space
Code on global space
Code on global space
29. foreach ($dir as $fileInfo) {
Code on global space
Code on global space
Code on global space
34. // definition objects
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
38. $name = $fileInfo->getFilename();
40. // Skip '.', '..' and `.*` files
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
41. if (!$fileInfo->isDot() && substr($name,0,1) != '.') {
43. // Get the full path to the JSON schema file
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
44. $file = realpath ($objectDir.'/'.$name);
46. // Read the file into a PHP string
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
47. $def = file_get_contents($file);
49. // Decode JSON into a PHP object
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
50. $obj = json_decode($def);
52. // Set as object if not defined otherwise
Code on global space
Code on global space
Code on global space
Code on global space
53. if(isset($object->type)) {
Code on global space
Code on global space
Code on global space
Code on global space
54. $obj->type = 'object';
Code on global space
Code on global space
Code on global space
Code on global space
57. if(isset($obj->properties)) {
59. // Loop through the defined properties array and load the definition for each one
Code on global space
Code on global space
Code on global space
Code on global space
60. foreach($obj->properties as $property) {
62. // Get the full path to the JSON property JSON file
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
63. $propertyFile = realpath ($propertyDir.'/'.$property.'.json');
Code on global space
Code on global space
66. // Read the file into a PHP string
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
67. $propertyDef = file_get_contents($propertyFile);
69. // Use the serialized JSON string as a JSON object
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
70. $propObj = json_decode($propertyDef);
72. // Check for references and expand or hydrate as needed
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
73. populateRefs($propObj, $defs);
75. // Use this definition as the value for the OpenAPI property
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
76. $props[$property] = $propObj;
80. // Overwrite the array of property strings with an array of property objects
Code on global space
Code on global space
Code on global space
Code on global space
81. $obj->properties = $props;
83. // Were there any definitions found?
Code on global space
Code on global space
Code on global space
Code on global space
85. // Set the object definitions
Code on global space
Code on global space
Code on global space
Code on global space
86. $obj->definitions = $defs;
90. // JSON encode the obj to get a valid spec JSON
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
91. $spec = json_encode($obj, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
93. // Create a writeable file pointer to the OpenAPI schema location
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
94. $fp = fopen($schemaDir.'/'.$name, 'w');
96. // Write the spec to the file pointer
Code on global space
Code on global space
Code on global space
Code on global space
Code on global space
99. // Close the file pointer
Code on global space
Code on global space
Code on global space
105. * Populate references
107. * @param type $propObj
110. function populateRefs($propObj, & $defs) {
112. $propType = gettype($propObj);
114. if($propType === 'object' || $propType === 'array') {
116. // Loop through looking for $ref's
117. foreach($propObj as $prop => $def) {
119. // We are only looking for $ref properties
Global function call (loadRef)
123. loadRef($def, $defs);
126. // Check if items are an object reference
129. // The $def has to be an object for this to be an array of $ref
130. $refType = gettype($def);
132. if($refType === 'object') {
133. // Loop through ref object
134. foreach($def as $refKey => $refString) {
136. // Is this our reference?
137. if($refKey === '$ref') {
138. // Load the reference
Global function call (loadRef)
139. loadRef($refString, $defs);
153. * Load a reference object
155. * @param type $refString
158. function loadRef($refString, & $defs) {
160. // Objects and their related properties are defined here.
161. $objectDir = realpath('./objects');
163. // Properties are stored here.
164. // They can be extended and reused across objects.
165. $propertyDir = realpath('./properties');
167. // If the first char is a '#' hydrate the referenced object into definitions
168. if(substr($refString, 0, 13) === '#/definitions') {
170. // We only want the final definition name from the path
171. $parts = explode('/', $refString);
173. $name = array_pop($parts);
175. if(!array_key_exists($name, $defs)) {
176. // Get the full path to the JSON schema file
177. $file = realpath ($objectDir.'/'.ucfirst($name).'.json');
183. // Read the file into a PHP string
184. $def = file_get_contents($file);
186. // Create a PHP object from decoded def
187. $obj = json_decode($def);
189. // Set the type to `object`
190. $obj->type = 'object';
192. // Array to hold defiend properties
195. // Loop through the defined properties array and load the definition for each one
196. foreach($obj->properties as $property) {
198. // Get the full path to the JSON property JSON file
199. $propertyFile = realpath ($propertyDir.'/'.$property.'.json');
202. // Read the file into a PHP string
203. $propertyDef = file_get_contents($propertyFile);
205. // Use the serialized JSON string as a JSON object
206. $propObj = json_decode($propertyDef);
208. // Check the property for a $ref to another object.
209. // If a $ref is found we need to add it's definition in this object.
Global function call (populateRefs)
210. populateRefs($propObj, $defs);
212. // Use this definition as the value for the OpenAPI property
213. $props[$property] = $propObj;
217. // Overwrite the array of property strings with an array of property objects
218. $obj->properties = $props;
220. $defs[$name] = $obj;