1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwaggerGen; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Main class of SwaggerGen which serves as a facade for the entire library. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2015 Martijn van der Lee |
9
|
|
|
* |
10
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
11
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
12
|
|
|
* in the Software without restriction, including without limitation the rights |
13
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
14
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
15
|
|
|
* furnished to do so, subject to the following conditions: |
16
|
|
|
* |
17
|
|
|
* The above copyright notice and this permission notice shall be included in |
18
|
|
|
* all copies or substantial portions of the Software. |
19
|
|
|
* |
20
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
23
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
25
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
26
|
|
|
* THE SOFTWARE. |
27
|
|
|
* |
28
|
|
|
* @package SwaggerGen |
29
|
|
|
* @author Martijn van der Lee <[email protected]> |
30
|
|
|
* @copyright 2014-2015 Martijn van der Lee |
31
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
32
|
|
|
*/ |
33
|
|
|
class SwaggerGen |
34
|
|
|
{ |
35
|
|
|
const FORMAT_ARRAY = ''; |
36
|
|
|
const FORMAT_JSON = 'json'; |
37
|
|
|
const FORMAT_JSON_PRETTY = 'json+'; |
38
|
|
|
const FORMAT_YAML = 'yaml'; |
39
|
|
|
|
40
|
|
|
private $host; |
41
|
|
|
private $basePath; |
42
|
|
|
private $dirs = array(); |
43
|
|
|
private $defines = array(); |
44
|
|
|
|
45
|
|
|
public function __construct($host = '', $basePath = '', $dirs = array()) |
46
|
|
|
{ |
47
|
|
|
$this->host = $host; |
48
|
|
|
$this->basePath = $basePath; |
49
|
|
|
$this->dirs = $dirs; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function define($name, $value = 1) |
53
|
|
|
{ |
54
|
|
|
$this->defines[$name] = $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function undefine($name) |
58
|
|
|
{ |
59
|
|
|
unset($this->defines[$name]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $file |
64
|
|
|
* @param string[] $dirs |
65
|
|
|
* @return Php\Entity\Statement[] |
66
|
|
|
*/ |
67
|
|
|
private function parsePhpFile($file, $dirs) |
68
|
|
|
{ |
69
|
|
|
$Parser = new Parser\Php\Parser(); |
70
|
|
|
return $Parser->parse($file, $dirs, $this->defines); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $file |
75
|
|
|
* @param string[] $dirs |
76
|
|
|
* @return Statement[] |
77
|
|
|
*/ |
78
|
|
|
private function parseTextFile($file, $dirs) |
79
|
|
|
{ |
80
|
|
|
$Parser = new Parser\Text\Parser(); |
81
|
|
|
return $Parser->parse($file, $dirs, $this->defines); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $text |
86
|
|
|
* @param string[] $dirs |
87
|
|
|
* @return Php\Entity\Statement[] |
88
|
|
|
*/ |
89
|
|
|
private function parseText($text, $dirs) |
90
|
|
|
{ |
91
|
|
|
$Parser = new Parser\Text\Parser(); |
92
|
|
|
return $Parser->parseText($text, $dirs, $this->defines); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Creates Swagger\Swagger object and populates it with statements |
97
|
|
|
* |
98
|
|
|
* This effectively converts the linear list of statements into parse-tree |
99
|
|
|
* like structure, performing some checks (like rejecting unknown |
100
|
|
|
* subcommands) during the process. Returned Swagger\Swagger object is |
101
|
|
|
* ready for serialization with {@see Swagger\Swagger::toArray} |
102
|
|
|
* |
103
|
|
|
* @param string $host |
104
|
|
|
* @param string $basePath |
105
|
|
|
* @param Statement[] $statements |
106
|
|
|
* @return Swagger\Swagger |
107
|
|
|
* @throws StatementException |
108
|
|
|
*/ |
109
|
|
|
public static function parseStatements($host, $basePath, $statements) |
110
|
|
|
{ |
111
|
|
|
$swagger = new Swagger\Swagger($host, $basePath); |
112
|
|
|
|
113
|
|
|
$stack = array($swagger); /* @var Swagger\AbstractObject[] $stack */ |
114
|
|
|
foreach ($statements as $statement) { |
115
|
|
|
try { |
116
|
|
|
$top = end($stack); |
117
|
|
|
|
118
|
|
|
do { |
119
|
|
|
$result = $top->handleCommand($statement->getCommand(), $statement->getData()); |
120
|
|
|
|
121
|
|
|
if ($result) { |
122
|
|
|
if ($result !== $top) { |
123
|
|
|
// Remove all similar classes from array first! |
124
|
|
|
$classname = get_class($result); |
125
|
|
|
$stack = array_filter($stack, function($class) use($classname) { |
126
|
|
|
return !(is_a($class, $classname)); |
127
|
|
|
}); |
128
|
|
|
|
129
|
|
|
$stack[] = $result; |
130
|
|
|
} |
131
|
|
|
} else { |
132
|
|
|
$top = prev($stack); |
133
|
|
|
} |
134
|
|
|
} while (!$result && $top); |
135
|
|
|
} catch (\SwaggerGen\Exception $e) { |
136
|
|
|
throw new \SwaggerGen\StatementException($e->getMessage(), $e->getCode(), $e, $statement); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!$result && !$top) { |
140
|
|
|
$messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}"); |
141
|
|
|
|
142
|
|
|
$stacktrace = array(); |
143
|
|
|
foreach ($stack as $object) { |
144
|
|
|
$stacktrace[] = (string) $object; |
145
|
|
|
} |
146
|
|
|
$messages[] = join(", \n", $stacktrace); |
147
|
|
|
|
148
|
|
|
throw new \SwaggerGen\StatementException(join('. ', $messages), 0, null, $statement); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $swagger; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get Swagger 2.x output |
157
|
|
|
* |
158
|
|
|
* @param string[] $files |
159
|
|
|
* @param string[] $dirs |
160
|
|
|
* @param string $format |
161
|
|
|
* @return type |
162
|
|
|
* @throws \SwaggerGen\Exception |
163
|
|
|
*/ |
164
|
|
|
public function getSwagger($files, $dirs = array(), $format = self::FORMAT_ARRAY) |
165
|
|
|
{ |
166
|
|
|
$dirs = array_merge($this->dirs, $dirs); |
167
|
|
|
|
168
|
|
|
$statements = array(); |
169
|
|
|
foreach ($files as $file) { |
170
|
|
|
switch (pathinfo($file, PATHINFO_EXTENSION)) { |
171
|
|
|
case 'php': |
172
|
|
|
$fileStatements = $this->parsePhpFile($file, $dirs); |
173
|
|
|
break; |
174
|
|
|
|
175
|
|
|
case 'txt': |
176
|
|
|
$fileStatements = $this->parseTextFile($file, $dirs); |
177
|
|
|
break; |
178
|
|
|
|
179
|
|
|
default: |
180
|
|
|
$fileStatements = $this->parseText($file, $dirs); |
181
|
|
|
break; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$statements = array_merge($statements, $fileStatements); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$output = self::parseStatements($this->host, $this->basePath, $statements)->toArray(); |
188
|
|
|
|
189
|
|
|
switch ($format) { |
190
|
|
|
case self::FORMAT_JSON: |
191
|
|
|
$output = json_encode($output); |
192
|
|
|
break; |
193
|
|
|
|
194
|
|
|
case self::FORMAT_JSON_PRETTY: |
195
|
|
|
$flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0 |
196
|
|
|
$output = json_encode($output, $flags); |
197
|
|
|
break; |
198
|
|
|
|
199
|
|
|
case self::FORMAT_YAML: |
200
|
|
|
if (!function_exists('yaml_emit')) { |
201
|
|
|
throw new Exception('YAML extension not installed.'); |
202
|
|
|
} |
203
|
|
|
array_walk_recursive($output, function(&$value) { |
204
|
|
|
if (is_object($value)) { |
205
|
|
|
$value = (array) $value; |
206
|
|
|
} |
207
|
|
|
}); |
208
|
|
|
$output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
209
|
|
|
break; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $output; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: