Open Journal Systems  3.3.0
buildSwagger.php
1 <?php
2 
16 define('APP_ROOT', dirname(dirname(dirname(dirname(__FILE__)))));
17 require(APP_ROOT . '/tools/bootstrap.inc.php');
18 
20 
23 
28  function __construct($argv = array()) {
29  parent::__construct($argv);
30  $this->outputFile = array_shift($this->argv);
31  }
32 
36  function usage() {
37  echo "Command-line tool to compile swagger.json API definitions\n"
38  . "Usage:\n"
39  . "\t{$this->scriptName} [outputFile]: Compile swagger file and save to [outputFile]\n"
40  . "\t{$this->scriptName} usage: Display usage information this tool\n";
41  }
42 
46  function execute() {
47  if (empty($this->outputFile)) {
48  $this->usage();
49  exit();
50  } elseif ((file_exists($this->outputFile) && !is_writable($this->outputFile)) ||
51  (!is_writeable(dirname($this->outputFile)))) {
52  echo "You do not have permission to write to this file.\n";
53  exit;
54  } else {
55  $source = file_get_contents(APP_ROOT . '/docs/dev/swagger-source.json');
56  if (!$source) {
57  $this->usage();
58  exit;
59  }
60 
61  import('classes.core.Services');
62  $locales = ['en_US', 'fr_CA'];
63 
64  $apiSchema = json_decode($source);
65  foreach ($apiSchema->definitions as $definitionName => $definition) {
66  // We assume a definition that is not a string does not need to be compiled
67  // from the schema files. It has already been defined.
68  if (!is_string($definition)) {
69  continue;
70  }
71 
72  $editDefinition = $summaryDefinition = $readDefinition = ['type' => 'object', 'properties' => []];
73  $entitySchema = \Services::get('schema')->get($definition, true);
74  foreach ($entitySchema->properties as $propName => $propSchema) {
75 
76  // Skip prop schemas with a `$ref`. They are already set up for the
77  // API docs but have no been converted to use SchemaDAO yet.
78  if (!empty($propSchema->{'$ref'})) {
79  continue;
80  }
81 
82  $editPropSchema = clone $propSchema;
83  $readPropSchema = clone $propSchema;
84  $summaryPropSchema = clone $propSchema;
85 
86  // Special handling to catch readOnly, writeOnly and apiSummary props in objects
87  if ($propSchema->type === 'object') {
88  $subPropsEdit = $subPropsRead = $subPropsSummary = [];
89  foreach ($propSchema->properties as $subPropName => $subPropSchema) {
90  if (empty($subPropSchema->readOnly)) {
91  $subPropsEdit[$subPropName] = $subPropSchema;
92  }
93  if (empty($subPropSchema->writeOnly)) {
94  $subPropsRead[$subPropName] = $subPropSchema;
95  }
96  if (!empty($subPropSchema->apiSummary)) {
97  $subPropsSummary[$subPropName] = $subPropSchema;
98  }
99  }
100  if (!empty($propSchema->multilingual)) {
101  $subPropsSchemaEdit = $subPropsSchemaRead = $subPropsSchemaSummary = [
102  'type' => 'object',
103  'properties' => [],
104  ];
105  foreach ($locales as $localeKey) {
106  $subPropsSchemaEdit[$localeKey]['properties'] = $subPropsEdit;
107  $subPropsSchemaRead[$localeKey]['properties'] = $subPropsRead;
108  $subPropsSchemaSummary[$localeKey]['properties'] = $subPropsSummary;
109  }
110  } else {
111  $subPropsSchemaEdit = $subPropsEdit;
112  $subPropsSchemaRead = $subPropsRead;
113  $subPropsSchemaSummary = $subPropsSummary;
114  }
115  if (empty($propSchema->readOnly)) {
116  $editPropSchema->properties = $subPropsSchemaEdit;
117  }
118  if (empty($propSchema->writeOnly)) {
119  $readPropSchema->properties = $subPropsSchemaRead;
120  }
121  if (!empty($propSchema->apiSummary)) {
122  $summaryPropSchema->properties = $subPropsSchemaSummary;
123  }
124 
125  // All non-object props
126  } else {
127  if (!empty($propSchema->multilingual)) {
128  if ($propSchema->type === 'array') {
129  $subProperties = [];
130  foreach ($locales as $localeKey) {
131  $subProperties[$localeKey] = $propSchema->items;
132  }
133  if (empty($propSchema->readOnly)) {
134  $editPropSchema->properties = $subProperties;
135  }
136  if (empty($propSchema->writeOnly)) {
137  $readPropSchema->properties = $subProperties;
138  }
139  if (!empty($propSchema->apiSummary)) {
140  $summaryPropSchema->properties = $subProperties;
141  }
142  } else {
143  if (empty($propSchema->readOnly)) {
144  $editPropSchema = ['$ref' => '#/definitions/LocaleObject'];
145  }
146  if (empty($propSchema->writeOnly)) {
147  $readPropSchema = ['$ref' => '#/definitions/LocaleObject'];
148  }
149  if (!empty($propSchema->apiSummary)) {
150  $summaryPropSchema = ['$ref' => '#/definitions/LocaleObject'];
151  }
152  }
153  }
154  }
155 
156  if (empty($propSchema->readOnly)) {
157  $editDefinition['properties'][$propName] = $editPropSchema;
158  }
159  if (empty($propSchema->writeOnly)) {
160  $readDefinition['properties'][$propName] = $readPropSchema;
161  }
162  if (!empty($propSchema->apiSummary)) {
163  $summaryDefinition['properties'][$propName] = $summaryPropSchema;
164  }
165  }
166  if (!empty($editDefinition['properties'])) {
167  $definitionEditableName = $definitionName . 'Editable';
168  ksort($editDefinition['properties']);
169  $apiSchema->definitions->{$definitionEditableName} = $editDefinition;
170  }
171  if (!empty($readDefinition['properties'])) {
172  ksort($readDefinition['properties']);
173  $apiSchema->definitions->{$definitionName} = $readDefinition;
174  }
175  if (!empty($summaryDefinition['properties'])) {
176  $definitionSummaryName = $definitionName . 'Summary';
177  ksort($summaryDefinition['properties']);
178  $apiSchema->definitions->{$definitionSummaryName} = $summaryDefinition;
179  }
180  }
181 
182  file_put_contents($this->outputFile, json_encode($apiSchema, JSON_PRETTY_PRINT));
183 
184  echo "Done\n";
185  }
186  }
187 
188 }
189 
190 $tool = new buildSwagger(isset($argv) ? $argv : array());
191 $tool->execute();
192 ?>
CommandLineTool
Initialization code for command-line scripts.
Definition: CliTool.inc.php:44
$tool
$tool
Definition: mergeCoverageReportTool.php:120
buildSwagger\$outputFile
$outputFile
Definition: buildSwagger.php:21
buildSwagger\__construct
__construct($argv=array())
Definition: buildSwagger.php:28
buildSwagger\$parameters
$parameters
Definition: buildSwagger.php:22
buildSwagger
CLI tool to compile a complete swagger.json file for hosting API documentation.
Definition: buildSwagger.php:19
buildSwagger\execute
execute()
Definition: buildSwagger.php:46
buildSwagger\usage
usage()
Definition: buildSwagger.php:36
GuzzleHttp\json_encode
json_encode($value, $options=0, $depth=512)
Definition: guzzlehttp/guzzle/src/functions.php:324
CommandLineTool\$argv
$argv
Definition: CliTool.inc.php:53
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49