@@ -16,162 +16,162 @@ |
||
| 16 | 16 | abstract class AbstractObject |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - private static $mime_types = array( |
|
| 20 | - 'fileform' => 'multipart/form-data', |
|
| 21 | - 'form' => 'application/x-www-form-urlencoded', |
|
| 22 | - 'json' => 'application/json', |
|
| 23 | - 'text' => 'text/plain', |
|
| 24 | - 'utf8' => 'text/plain; charset=utf-8', |
|
| 25 | - 'yml' => 'application/x-yaml', |
|
| 26 | - 'yaml' => 'application/x-yaml', |
|
| 27 | - 'php' => 'text/x-php', |
|
| 28 | - 'xml' => 'text/xml', |
|
| 29 | - ); |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var AbstractObject |
|
| 33 | - */ |
|
| 34 | - private $parent; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Map of extensions and their (trimmed) values |
|
| 38 | - * @var string[] |
|
| 39 | - */ |
|
| 40 | - private $extensions = []; |
|
| 41 | - |
|
| 42 | - public function __construct(?AbstractObject $parent = null) |
|
| 43 | - { |
|
| 44 | - $this->parent = $parent; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Trim whitespace from a multibyte string |
|
| 49 | - * @param string $string |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public static function trim($string): string |
|
| 53 | - { |
|
| 54 | - return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Filter all items from an array where the value is either null or an |
|
| 59 | - * empty array. |
|
| 60 | - * @param array $array |
|
| 61 | - * @return array |
|
| 62 | - */ |
|
| 63 | - public static function arrayFilterNull($array): array |
|
| 64 | - { |
|
| 65 | - return array_filter($array, static function ($value) { |
|
| 66 | - return $value !== null && $value !== []; |
|
| 67 | - }); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Recursively call toArray() on all objects to return an array |
|
| 72 | - * @param array $array |
|
| 73 | - * @return array |
|
| 74 | - */ |
|
| 75 | - public static function objectsToArray($array): array |
|
| 76 | - { |
|
| 77 | - return array_map(static function (AbstractObject $item) { |
|
| 78 | - return $item->toArray(); |
|
| 79 | - }, $array); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @return array |
|
| 84 | - */ |
|
| 85 | - public function toArray(): array |
|
| 86 | - { |
|
| 87 | - return $this->extensions; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Shifts the first word off a text line and returns it |
|
| 92 | - * @param string $data |
|
| 93 | - * @return string|bool Either the first word or false if no more words available |
|
| 94 | - */ |
|
| 95 | - public static function wordShift(&$data) |
|
| 96 | - { |
|
| 97 | - if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) { |
|
| 98 | - $data = $matches[2]; |
|
| 99 | - return $matches[1]; |
|
| 100 | - } |
|
| 101 | - return false; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Splits a text line in all it's words |
|
| 106 | - * @param string $data |
|
| 107 | - * @return string[] |
|
| 108 | - */ |
|
| 109 | - public static function wordSplit($data): array |
|
| 110 | - { |
|
| 111 | - return array_values(preg_grep('~\S~', preg_split('~\s+~', $data))); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Translate consumes from shortcuts |
|
| 116 | - * @param String[] $mimeTypes |
|
| 117 | - * @return String[] |
|
| 118 | - */ |
|
| 119 | - protected static function translateMimeTypes($mimeTypes): array |
|
| 120 | - { |
|
| 121 | - foreach ($mimeTypes as &$mimeType) { |
|
| 122 | - if (isset(self::$mime_types[strtolower($mimeType)])) { |
|
| 123 | - $mimeType = self::$mime_types[strtolower($mimeType)]; |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return $mimeTypes; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @param string $command |
|
| 132 | - * @param string $data |
|
| 133 | - * @return AbstractObject|boolean |
|
| 134 | - */ |
|
| 135 | - public function handleCommand($command, $data = null) |
|
| 136 | - { |
|
| 137 | - if (stripos($command, 'x-') === 0) { |
|
| 138 | - $this->extensions[$command] = empty($data) ? $data : trim($data); |
|
| 139 | - return $this; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return AbstractObject|null |
|
| 147 | - */ |
|
| 148 | - protected function getParent(): ?AbstractObject |
|
| 149 | - { |
|
| 150 | - return $this->parent; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - protected function getParentClass($classname): AbstractObject |
|
| 154 | - { |
|
| 155 | - if (is_a($this, $classname)) { |
|
| 156 | - return $this; |
|
| 157 | - } |
|
| 158 | - return $this->parent->getParentClass($classname); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @return Swagger |
|
| 163 | - */ |
|
| 164 | - protected function getSwagger(): Swagger |
|
| 165 | - { |
|
| 166 | - return $this->parent->getSwagger(); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @return TypeRegistry |
|
| 171 | - */ |
|
| 172 | - protected function getTypeRegistry(): TypeRegistry |
|
| 173 | - { |
|
| 174 | - return $this->parent->getTypeRegistry(); |
|
| 175 | - } |
|
| 19 | + private static $mime_types = array( |
|
| 20 | + 'fileform' => 'multipart/form-data', |
|
| 21 | + 'form' => 'application/x-www-form-urlencoded', |
|
| 22 | + 'json' => 'application/json', |
|
| 23 | + 'text' => 'text/plain', |
|
| 24 | + 'utf8' => 'text/plain; charset=utf-8', |
|
| 25 | + 'yml' => 'application/x-yaml', |
|
| 26 | + 'yaml' => 'application/x-yaml', |
|
| 27 | + 'php' => 'text/x-php', |
|
| 28 | + 'xml' => 'text/xml', |
|
| 29 | + ); |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var AbstractObject |
|
| 33 | + */ |
|
| 34 | + private $parent; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Map of extensions and their (trimmed) values |
|
| 38 | + * @var string[] |
|
| 39 | + */ |
|
| 40 | + private $extensions = []; |
|
| 41 | + |
|
| 42 | + public function __construct(?AbstractObject $parent = null) |
|
| 43 | + { |
|
| 44 | + $this->parent = $parent; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Trim whitespace from a multibyte string |
|
| 49 | + * @param string $string |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public static function trim($string): string |
|
| 53 | + { |
|
| 54 | + return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Filter all items from an array where the value is either null or an |
|
| 59 | + * empty array. |
|
| 60 | + * @param array $array |
|
| 61 | + * @return array |
|
| 62 | + */ |
|
| 63 | + public static function arrayFilterNull($array): array |
|
| 64 | + { |
|
| 65 | + return array_filter($array, static function ($value) { |
|
| 66 | + return $value !== null && $value !== []; |
|
| 67 | + }); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Recursively call toArray() on all objects to return an array |
|
| 72 | + * @param array $array |
|
| 73 | + * @return array |
|
| 74 | + */ |
|
| 75 | + public static function objectsToArray($array): array |
|
| 76 | + { |
|
| 77 | + return array_map(static function (AbstractObject $item) { |
|
| 78 | + return $item->toArray(); |
|
| 79 | + }, $array); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @return array |
|
| 84 | + */ |
|
| 85 | + public function toArray(): array |
|
| 86 | + { |
|
| 87 | + return $this->extensions; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Shifts the first word off a text line and returns it |
|
| 92 | + * @param string $data |
|
| 93 | + * @return string|bool Either the first word or false if no more words available |
|
| 94 | + */ |
|
| 95 | + public static function wordShift(&$data) |
|
| 96 | + { |
|
| 97 | + if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) { |
|
| 98 | + $data = $matches[2]; |
|
| 99 | + return $matches[1]; |
|
| 100 | + } |
|
| 101 | + return false; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Splits a text line in all it's words |
|
| 106 | + * @param string $data |
|
| 107 | + * @return string[] |
|
| 108 | + */ |
|
| 109 | + public static function wordSplit($data): array |
|
| 110 | + { |
|
| 111 | + return array_values(preg_grep('~\S~', preg_split('~\s+~', $data))); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Translate consumes from shortcuts |
|
| 116 | + * @param String[] $mimeTypes |
|
| 117 | + * @return String[] |
|
| 118 | + */ |
|
| 119 | + protected static function translateMimeTypes($mimeTypes): array |
|
| 120 | + { |
|
| 121 | + foreach ($mimeTypes as &$mimeType) { |
|
| 122 | + if (isset(self::$mime_types[strtolower($mimeType)])) { |
|
| 123 | + $mimeType = self::$mime_types[strtolower($mimeType)]; |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return $mimeTypes; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @param string $command |
|
| 132 | + * @param string $data |
|
| 133 | + * @return AbstractObject|boolean |
|
| 134 | + */ |
|
| 135 | + public function handleCommand($command, $data = null) |
|
| 136 | + { |
|
| 137 | + if (stripos($command, 'x-') === 0) { |
|
| 138 | + $this->extensions[$command] = empty($data) ? $data : trim($data); |
|
| 139 | + return $this; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return AbstractObject|null |
|
| 147 | + */ |
|
| 148 | + protected function getParent(): ?AbstractObject |
|
| 149 | + { |
|
| 150 | + return $this->parent; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + protected function getParentClass($classname): AbstractObject |
|
| 154 | + { |
|
| 155 | + if (is_a($this, $classname)) { |
|
| 156 | + return $this; |
|
| 157 | + } |
|
| 158 | + return $this->parent->getParentClass($classname); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @return Swagger |
|
| 163 | + */ |
|
| 164 | + protected function getSwagger(): Swagger |
|
| 165 | + { |
|
| 166 | + return $this->parent->getSwagger(); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @return TypeRegistry |
|
| 171 | + */ |
|
| 172 | + protected function getTypeRegistry(): TypeRegistry |
|
| 173 | + { |
|
| 174 | + return $this->parent->getTypeRegistry(); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | 177 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | */ |
| 63 | 63 | public static function arrayFilterNull($array): array |
| 64 | 64 | { |
| 65 | - return array_filter($array, static function ($value) { |
|
| 65 | + return array_filter($array, static function($value) { |
|
| 66 | 66 | return $value !== null && $value !== []; |
| 67 | 67 | }); |
| 68 | 68 | } |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | */ |
| 75 | 75 | public static function objectsToArray($array): array |
| 76 | 76 | { |
| 77 | - return array_map(static function (AbstractObject $item) { |
|
| 77 | + return array_map(static function(AbstractObject $item) { |
|
| 78 | 78 | return $item->toArray(); |
| 79 | 79 | }, $array); |
| 80 | 80 | } |
@@ -38,215 +38,215 @@ |
||
| 38 | 38 | class SwaggerGen |
| 39 | 39 | { |
| 40 | 40 | |
| 41 | - public const FORMAT_ARRAY = ''; |
|
| 42 | - public const FORMAT_JSON = 'json'; |
|
| 43 | - public const FORMAT_JSON_PRETTY = 'json+'; |
|
| 44 | - public const FORMAT_YAML = 'yaml'; |
|
| 45 | - |
|
| 46 | - private string $host; |
|
| 47 | - private string $basePath; |
|
| 48 | - private array $dirs = []; |
|
| 49 | - private array $defines = []; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var TypeRegistry |
|
| 53 | - */ |
|
| 54 | - private $typeRegistry; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Create a new SwaggerGen instance |
|
| 58 | - * |
|
| 59 | - * @param string $host |
|
| 60 | - * @param string $basePath |
|
| 61 | - * @param string[] $dirs |
|
| 62 | - * @param TypeRegistry $typeRegistry |
|
| 63 | - */ |
|
| 64 | - public function __construct($host = '', $basePath = '', $dirs = [], $typeRegistry = null) |
|
| 65 | - { |
|
| 66 | - $this->host = $host; |
|
| 67 | - $this->basePath = $basePath; |
|
| 68 | - $this->dirs = $dirs; |
|
| 69 | - $this->typeRegistry = $typeRegistry; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Set a new type registry |
|
| 74 | - * |
|
| 75 | - * @param TypeRegistry $typeRegistry |
|
| 76 | - */ |
|
| 77 | - public function setTypeRegistry($typeRegistry = null) |
|
| 78 | - { |
|
| 79 | - $this->typeRegistry = $typeRegistry; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param string $name |
|
| 84 | - */ |
|
| 85 | - public function define($name, $value = 1) |
|
| 86 | - { |
|
| 87 | - $this->defines[$name] = $value; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $name |
|
| 92 | - */ |
|
| 93 | - public function undefine($name) |
|
| 94 | - { |
|
| 95 | - unset($this->defines[$name]); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get Swagger 2.x output |
|
| 100 | - * |
|
| 101 | - * @param string[] $files |
|
| 102 | - * @param string[] $dirs |
|
| 103 | - * @param string $format |
|
| 104 | - * @return array|false|string |
|
| 105 | - * @throws Exception |
|
| 106 | - * @throws StatementException |
|
| 107 | - */ |
|
| 108 | - public function getSwagger(array $files, array $dirs = [], string $format = self::FORMAT_ARRAY) |
|
| 109 | - { |
|
| 110 | - $dirs = array_merge($this->dirs, $dirs); |
|
| 111 | - |
|
| 112 | - $statements = []; |
|
| 113 | - foreach ($files as $file) { |
|
| 114 | - switch (pathinfo($file, PATHINFO_EXTENSION)) { |
|
| 115 | - case 'php': |
|
| 116 | - $fileStatements = $this->parsePhpFile($file, $dirs); |
|
| 117 | - break; |
|
| 118 | - |
|
| 119 | - case 'txt': |
|
| 120 | - $fileStatements = $this->parseTextFile($file, $dirs); |
|
| 121 | - break; |
|
| 122 | - |
|
| 123 | - default: |
|
| 124 | - $fileStatements = $this->parseText($file, $dirs); |
|
| 125 | - break; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $statements[] = $fileStatements; |
|
| 129 | - } |
|
| 130 | - $statements = array_merge(...$statements); |
|
| 131 | - |
|
| 132 | - $output = $this->parseStatements($this->host, $this->basePath, $statements)->toArray(); |
|
| 133 | - |
|
| 134 | - switch ($format) { |
|
| 135 | - case self::FORMAT_JSON: |
|
| 136 | - $output = json_encode($output); |
|
| 137 | - break; |
|
| 138 | - |
|
| 139 | - case self::FORMAT_JSON_PRETTY: |
|
| 140 | - $flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0 |
|
| 141 | - $output = json_encode($output, $flags); |
|
| 142 | - break; |
|
| 143 | - |
|
| 144 | - case self::FORMAT_YAML: |
|
| 145 | - if (!function_exists('yaml_emit')) { |
|
| 146 | - throw new Exception('YAML extension not installed.'); |
|
| 147 | - } |
|
| 148 | - array_walk_recursive($output, static function (&$value) { |
|
| 149 | - if (is_object($value)) { |
|
| 150 | - $value = (array)$value; |
|
| 151 | - } |
|
| 152 | - }); |
|
| 153 | - $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
|
| 154 | - break; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $output; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @param string $file |
|
| 162 | - * @param string[] $dirs |
|
| 163 | - * @return Statement[] |
|
| 164 | - * @throws Exception |
|
| 165 | - */ |
|
| 166 | - private function parsePhpFile(string $file, array $dirs): array |
|
| 167 | - { |
|
| 168 | - return (new Parser())->parse($file, $dirs, $this->defines); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @param string $file |
|
| 173 | - * @param string[] $dirs |
|
| 174 | - * @return Statement[] |
|
| 175 | - */ |
|
| 176 | - private function parseTextFile($file, $dirs) |
|
| 177 | - { |
|
| 178 | - return (new TextParser())->parse($file, $dirs, $this->defines); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @param string $text |
|
| 183 | - * @param string[] $dirs |
|
| 184 | - * @return Statement[] |
|
| 185 | - */ |
|
| 186 | - private function parseText(string $text, array $dirs): array |
|
| 187 | - { |
|
| 188 | - return (new TextParser())->parseText($text, $dirs, $this->defines); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Creates Swagger\Swagger object and populates it with statements |
|
| 193 | - * |
|
| 194 | - * This effectively converts the linear list of statements into parse-tree |
|
| 195 | - * like structure, performing some checks (like rejecting unknown |
|
| 196 | - * subcommands) during the process. Returned Swagger\Swagger object is |
|
| 197 | - * ready for serialization with {@see Swagger\Swagger::toArray} |
|
| 198 | - * |
|
| 199 | - * @param string $host |
|
| 200 | - * @param string $basePath |
|
| 201 | - * @param Statement[] $statements |
|
| 202 | - * @return Swagger |
|
| 203 | - * @throws StatementException |
|
| 204 | - */ |
|
| 205 | - public function parseStatements($host, $basePath, $statements) |
|
| 206 | - { |
|
| 207 | - $swagger = new Swagger($host, $basePath, $this->typeRegistry); |
|
| 208 | - |
|
| 209 | - $stack = array($swagger); |
|
| 210 | - /* @var AbstractObject[] $stack */ |
|
| 211 | - foreach ($statements as $statement) { |
|
| 212 | - try { |
|
| 213 | - $top = end($stack); |
|
| 214 | - |
|
| 215 | - do { |
|
| 216 | - $result = $top->handleCommand($statement->getCommand(), $statement->getData()); |
|
| 217 | - |
|
| 218 | - if ($result) { |
|
| 219 | - if ($result !== $top) { |
|
| 220 | - // Remove all similar classes from array first! |
|
| 221 | - $classname = get_class($result); |
|
| 222 | - $stack = array_filter($stack, static function ($class) use ($classname) { |
|
| 223 | - return !(is_a($class, $classname)); |
|
| 224 | - }); |
|
| 225 | - |
|
| 226 | - $stack[] = $result; |
|
| 227 | - } |
|
| 228 | - } else { |
|
| 229 | - $top = prev($stack); |
|
| 230 | - } |
|
| 231 | - } while (!$result && $top); |
|
| 232 | - } catch (Exception $e) { |
|
| 233 | - throw new StatementException($e->getMessage(), $e->getCode(), $e, $statement); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - if (!$result && !$top) { |
|
| 237 | - $messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}"); |
|
| 238 | - |
|
| 239 | - $stacktrace = []; |
|
| 240 | - foreach ($stack as $object) { |
|
| 241 | - $stacktrace[] = (string)$object; |
|
| 242 | - } |
|
| 243 | - $messages[] = implode(', ' . PHP_EOL, $stacktrace); |
|
| 244 | - |
|
| 245 | - throw new StatementException(implode('. ', $messages), 0, null, $statement); |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - return $swagger; |
|
| 250 | - } |
|
| 41 | + public const FORMAT_ARRAY = ''; |
|
| 42 | + public const FORMAT_JSON = 'json'; |
|
| 43 | + public const FORMAT_JSON_PRETTY = 'json+'; |
|
| 44 | + public const FORMAT_YAML = 'yaml'; |
|
| 45 | + |
|
| 46 | + private string $host; |
|
| 47 | + private string $basePath; |
|
| 48 | + private array $dirs = []; |
|
| 49 | + private array $defines = []; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var TypeRegistry |
|
| 53 | + */ |
|
| 54 | + private $typeRegistry; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Create a new SwaggerGen instance |
|
| 58 | + * |
|
| 59 | + * @param string $host |
|
| 60 | + * @param string $basePath |
|
| 61 | + * @param string[] $dirs |
|
| 62 | + * @param TypeRegistry $typeRegistry |
|
| 63 | + */ |
|
| 64 | + public function __construct($host = '', $basePath = '', $dirs = [], $typeRegistry = null) |
|
| 65 | + { |
|
| 66 | + $this->host = $host; |
|
| 67 | + $this->basePath = $basePath; |
|
| 68 | + $this->dirs = $dirs; |
|
| 69 | + $this->typeRegistry = $typeRegistry; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Set a new type registry |
|
| 74 | + * |
|
| 75 | + * @param TypeRegistry $typeRegistry |
|
| 76 | + */ |
|
| 77 | + public function setTypeRegistry($typeRegistry = null) |
|
| 78 | + { |
|
| 79 | + $this->typeRegistry = $typeRegistry; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param string $name |
|
| 84 | + */ |
|
| 85 | + public function define($name, $value = 1) |
|
| 86 | + { |
|
| 87 | + $this->defines[$name] = $value; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $name |
|
| 92 | + */ |
|
| 93 | + public function undefine($name) |
|
| 94 | + { |
|
| 95 | + unset($this->defines[$name]); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get Swagger 2.x output |
|
| 100 | + * |
|
| 101 | + * @param string[] $files |
|
| 102 | + * @param string[] $dirs |
|
| 103 | + * @param string $format |
|
| 104 | + * @return array|false|string |
|
| 105 | + * @throws Exception |
|
| 106 | + * @throws StatementException |
|
| 107 | + */ |
|
| 108 | + public function getSwagger(array $files, array $dirs = [], string $format = self::FORMAT_ARRAY) |
|
| 109 | + { |
|
| 110 | + $dirs = array_merge($this->dirs, $dirs); |
|
| 111 | + |
|
| 112 | + $statements = []; |
|
| 113 | + foreach ($files as $file) { |
|
| 114 | + switch (pathinfo($file, PATHINFO_EXTENSION)) { |
|
| 115 | + case 'php': |
|
| 116 | + $fileStatements = $this->parsePhpFile($file, $dirs); |
|
| 117 | + break; |
|
| 118 | + |
|
| 119 | + case 'txt': |
|
| 120 | + $fileStatements = $this->parseTextFile($file, $dirs); |
|
| 121 | + break; |
|
| 122 | + |
|
| 123 | + default: |
|
| 124 | + $fileStatements = $this->parseText($file, $dirs); |
|
| 125 | + break; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $statements[] = $fileStatements; |
|
| 129 | + } |
|
| 130 | + $statements = array_merge(...$statements); |
|
| 131 | + |
|
| 132 | + $output = $this->parseStatements($this->host, $this->basePath, $statements)->toArray(); |
|
| 133 | + |
|
| 134 | + switch ($format) { |
|
| 135 | + case self::FORMAT_JSON: |
|
| 136 | + $output = json_encode($output); |
|
| 137 | + break; |
|
| 138 | + |
|
| 139 | + case self::FORMAT_JSON_PRETTY: |
|
| 140 | + $flags = (defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); // Since PHP 5.4.0 |
|
| 141 | + $output = json_encode($output, $flags); |
|
| 142 | + break; |
|
| 143 | + |
|
| 144 | + case self::FORMAT_YAML: |
|
| 145 | + if (!function_exists('yaml_emit')) { |
|
| 146 | + throw new Exception('YAML extension not installed.'); |
|
| 147 | + } |
|
| 148 | + array_walk_recursive($output, static function (&$value) { |
|
| 149 | + if (is_object($value)) { |
|
| 150 | + $value = (array)$value; |
|
| 151 | + } |
|
| 152 | + }); |
|
| 153 | + $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
|
| 154 | + break; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $output; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @param string $file |
|
| 162 | + * @param string[] $dirs |
|
| 163 | + * @return Statement[] |
|
| 164 | + * @throws Exception |
|
| 165 | + */ |
|
| 166 | + private function parsePhpFile(string $file, array $dirs): array |
|
| 167 | + { |
|
| 168 | + return (new Parser())->parse($file, $dirs, $this->defines); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @param string $file |
|
| 173 | + * @param string[] $dirs |
|
| 174 | + * @return Statement[] |
|
| 175 | + */ |
|
| 176 | + private function parseTextFile($file, $dirs) |
|
| 177 | + { |
|
| 178 | + return (new TextParser())->parse($file, $dirs, $this->defines); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @param string $text |
|
| 183 | + * @param string[] $dirs |
|
| 184 | + * @return Statement[] |
|
| 185 | + */ |
|
| 186 | + private function parseText(string $text, array $dirs): array |
|
| 187 | + { |
|
| 188 | + return (new TextParser())->parseText($text, $dirs, $this->defines); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Creates Swagger\Swagger object and populates it with statements |
|
| 193 | + * |
|
| 194 | + * This effectively converts the linear list of statements into parse-tree |
|
| 195 | + * like structure, performing some checks (like rejecting unknown |
|
| 196 | + * subcommands) during the process. Returned Swagger\Swagger object is |
|
| 197 | + * ready for serialization with {@see Swagger\Swagger::toArray} |
|
| 198 | + * |
|
| 199 | + * @param string $host |
|
| 200 | + * @param string $basePath |
|
| 201 | + * @param Statement[] $statements |
|
| 202 | + * @return Swagger |
|
| 203 | + * @throws StatementException |
|
| 204 | + */ |
|
| 205 | + public function parseStatements($host, $basePath, $statements) |
|
| 206 | + { |
|
| 207 | + $swagger = new Swagger($host, $basePath, $this->typeRegistry); |
|
| 208 | + |
|
| 209 | + $stack = array($swagger); |
|
| 210 | + /* @var AbstractObject[] $stack */ |
|
| 211 | + foreach ($statements as $statement) { |
|
| 212 | + try { |
|
| 213 | + $top = end($stack); |
|
| 214 | + |
|
| 215 | + do { |
|
| 216 | + $result = $top->handleCommand($statement->getCommand(), $statement->getData()); |
|
| 217 | + |
|
| 218 | + if ($result) { |
|
| 219 | + if ($result !== $top) { |
|
| 220 | + // Remove all similar classes from array first! |
|
| 221 | + $classname = get_class($result); |
|
| 222 | + $stack = array_filter($stack, static function ($class) use ($classname) { |
|
| 223 | + return !(is_a($class, $classname)); |
|
| 224 | + }); |
|
| 225 | + |
|
| 226 | + $stack[] = $result; |
|
| 227 | + } |
|
| 228 | + } else { |
|
| 229 | + $top = prev($stack); |
|
| 230 | + } |
|
| 231 | + } while (!$result && $top); |
|
| 232 | + } catch (Exception $e) { |
|
| 233 | + throw new StatementException($e->getMessage(), $e->getCode(), $e, $statement); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + if (!$result && !$top) { |
|
| 237 | + $messages = array("Unsupported or unknown command: {$statement->getCommand()} {$statement->getData()}"); |
|
| 238 | + |
|
| 239 | + $stacktrace = []; |
|
| 240 | + foreach ($stack as $object) { |
|
| 241 | + $stacktrace[] = (string)$object; |
|
| 242 | + } |
|
| 243 | + $messages[] = implode(', ' . PHP_EOL, $stacktrace); |
|
| 244 | + |
|
| 245 | + throw new StatementException(implode('. ', $messages), 0, null, $statement); |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + return $swagger; |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | 252 | } |
@@ -145,9 +145,9 @@ discard block |
||
| 145 | 145 | if (!function_exists('yaml_emit')) { |
| 146 | 146 | throw new Exception('YAML extension not installed.'); |
| 147 | 147 | } |
| 148 | - array_walk_recursive($output, static function (&$value) { |
|
| 148 | + array_walk_recursive($output, static function(&$value) { |
|
| 149 | 149 | if (is_object($value)) { |
| 150 | - $value = (array)$value; |
|
| 150 | + $value = (array) $value; |
|
| 151 | 151 | } |
| 152 | 152 | }); |
| 153 | 153 | $output = yaml_emit($output, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | if ($result !== $top) { |
| 220 | 220 | // Remove all similar classes from array first! |
| 221 | 221 | $classname = get_class($result); |
| 222 | - $stack = array_filter($stack, static function ($class) use ($classname) { |
|
| 222 | + $stack = array_filter($stack, static function($class) use ($classname) { |
|
| 223 | 223 | return !(is_a($class, $classname)); |
| 224 | 224 | }); |
| 225 | 225 | |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | |
| 239 | 239 | $stacktrace = []; |
| 240 | 240 | foreach ($stack as $object) { |
| 241 | - $stacktrace[] = (string)$object; |
|
| 241 | + $stacktrace[] = (string) $object; |
|
| 242 | 242 | } |
| 243 | 243 | $messages[] = implode(', ' . PHP_EOL, $stacktrace); |
| 244 | 244 | |
@@ -15,57 +15,57 @@ |
||
| 15 | 15 | class TypeRegistry |
| 16 | 16 | {
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Map of format-name => class-name |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - private $formats = []; |
|
| 18 | + /** |
|
| 19 | + * Map of format-name => class-name |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + private $formats = []; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Add a type name from classname |
|
| 27 | - * |
|
| 28 | - * @param string $classname |
|
| 29 | - */ |
|
| 30 | - public function add(string $classname): void |
|
| 31 | - {
|
|
| 32 | - if (is_subclass_of($classname, ICustomType::class)) {
|
|
| 33 | - foreach ($classname::getFormats() as $format) {
|
|
| 34 | - $this->formats[$format] = $classname; |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - } |
|
| 25 | + /** |
|
| 26 | + * Add a type name from classname |
|
| 27 | + * |
|
| 28 | + * @param string $classname |
|
| 29 | + */ |
|
| 30 | + public function add(string $classname): void |
|
| 31 | + {
|
|
| 32 | + if (is_subclass_of($classname, ICustomType::class)) {
|
|
| 33 | + foreach ($classname::getFormats() as $format) {
|
|
| 34 | + $this->formats[$format] = $classname; |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Remove type format by explicitly nulling it (disables it) |
|
| 41 | - * |
|
| 42 | - * @param string $name |
|
| 43 | - */ |
|
| 44 | - public function remove($name) |
|
| 45 | - {
|
|
| 46 | - $this->formats[$name] = null; |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * Remove type format by explicitly nulling it (disables it) |
|
| 41 | + * |
|
| 42 | + * @param string $name |
|
| 43 | + */ |
|
| 44 | + public function remove($name) |
|
| 45 | + {
|
|
| 46 | + $this->formats[$name] = null; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Is a type format known? |
|
| 51 | - * |
|
| 52 | - * @return bool |
|
| 53 | - */ |
|
| 54 | - public function has($name) |
|
| 55 | - {
|
|
| 56 | - return !empty($this->formats[$name]); |
|
| 57 | - } |
|
| 49 | + /** |
|
| 50 | + * Is a type format known? |
|
| 51 | + * |
|
| 52 | + * @return bool |
|
| 53 | + */ |
|
| 54 | + public function has($name) |
|
| 55 | + {
|
|
| 56 | + return !empty($this->formats[$name]); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Get the format class name |
|
| 61 | - * |
|
| 62 | - * @return null|string |
|
| 63 | - */ |
|
| 64 | - public function get($name) |
|
| 65 | - {
|
|
| 66 | - return !empty($this->formats[$name]) |
|
| 67 | - ? $this->formats[$name] |
|
| 68 | - : null; |
|
| 69 | - } |
|
| 59 | + /** |
|
| 60 | + * Get the format class name |
|
| 61 | + * |
|
| 62 | + * @return null|string |
|
| 63 | + */ |
|
| 64 | + public function get($name) |
|
| 65 | + {
|
|
| 66 | + return !empty($this->formats[$name]) |
|
| 67 | + ? $this->formats[$name] |
|
| 68 | + : null; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | } |
@@ -12,8 +12,8 @@ discard block |
||
| 12 | 12 | * @copyright 2014-2025 Martijn van der Lee |
| 13 | 13 | * @license https://opensource.org/licenses/MIT MIT |
| 14 | 14 | */ |
| 15 | -class TypeRegistry |
|
| 16 | -{
|
|
| 15 | +class TypeRegistry |
|
| 16 | +{ |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Map of format-name => class-name |
@@ -29,8 +29,8 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | public function add(string $classname): void |
| 31 | 31 | {
|
| 32 | - if (is_subclass_of($classname, ICustomType::class)) {
|
|
| 33 | - foreach ($classname::getFormats() as $format) {
|
|
| 32 | + if (is_subclass_of($classname, ICustomType::class)) { |
|
| 33 | + foreach ($classname::getFormats() as $format) { |
|
| 34 | 34 | $this->formats[$format] = $classname; |
| 35 | 35 | } |
| 36 | 36 | } |
@@ -41,8 +41,8 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @param string $name |
| 43 | 43 | */ |
| 44 | - public function remove($name) |
|
| 45 | - {
|
|
| 44 | + public function remove($name) |
|
| 45 | + { |
|
| 46 | 46 | $this->formats[$name] = null; |
| 47 | 47 | } |
| 48 | 48 | |
@@ -51,8 +51,8 @@ discard block |
||
| 51 | 51 | * |
| 52 | 52 | * @return bool |
| 53 | 53 | */ |
| 54 | - public function has($name) |
|
| 55 | - {
|
|
| 54 | + public function has($name) |
|
| 55 | + { |
|
| 56 | 56 | return !empty($this->formats[$name]); |
| 57 | 57 | } |
| 58 | 58 | |
@@ -61,8 +61,8 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @return null|string |
| 63 | 63 | */ |
| 64 | - public function get($name) |
|
| 65 | - {
|
|
| 64 | + public function get($name) |
|
| 65 | + { |
|
| 66 | 66 | return !empty($this->formats[$name]) |
| 67 | 67 | ? $this->formats[$name] |
| 68 | 68 | : null; |