#!/usr/bin/env php getcwd(), 'exclude' => null, 'bootstrap' => false, 'url' => null, 'default-base-path' => null, 'default-api-version' => null, 'default-swagger-version' => '1.2', 'api-doc-template' => null, 'version' => false, 'suffix' => '.{format}', 'help' => false, 'debug' => false, ); $aliases = array( 'o' => 'output', 'u' => 'url', 'e' => 'exclude', 'b' => 'bootstrap', 'v' => 'version', 'h' => 'help', ); $needsArgument = array( 'output', 'exclude', 'bootstrap', 'url', 'suffix', 'default-base-path', 'default-api-version', 'default-swagger-version', 'api-doc-template', ); $paths = array(); // Parse cli arguments for ($i = 1; $i < $argc; $i++) { $arg = $argv[$i]; if (substr($arg, 0, 2) === '--') { // longopt $option = substr($arg, 2); } elseif ($arg[0] === '-') { // shortopt if (array_key_exists(substr($arg, 1), $aliases)) { $option = $aliases[$arg[1]]; } else { throw new RuntimeException('Unknown option: "'.$arg.'"'); } } else { $paths[] = $arg; continue; } if (array_key_exists($option, $options) === false) { throw new RuntimeException('Unknown option: "'.$arg.'"'); } if (in_array($option, $needsArgument)) { if (empty($argv[$i + 1]) || $argv[$i + 1][0] === '-') { throw new RuntimeException('Missing argument for "'.$arg.'"'); } $options[$option] = $argv[$i + 1]; $i++; } else { $options[$option] = true; } } $version = trim(file_get_contents(__DIR__.'/../VERSION')); if ($options['version']) { echo $version, PHP_EOL; exit; } echo 'Swagger-PHP ', $version, PHP_EOL; echo '-----------------', PHP_EOL; if ($argc === 1 || $options['help']) { echo << $excludePath) { if (DIRECTORY_SEPARATOR != substr($excludePath, 0, 1)) { $excludePaths[$index] = getcwd().DIRECTORY_SEPARATOR.$excludePath; } } } else { $excludePaths = array(); } if ($options['bootstrap']) { /* @var \Composer\Autoload\ClassLoader $loader */ foreach (explode(',', $options['bootstrap']) as $incPath) { @list($namespace, $inclusionPath) = explode(':', $incPath); $loader->add($namespace, array($inclusionPath ? : '.')); if (!$inclusionPath && is_file($incPath)) { require_once($incPath); } } } if (count($paths) === 0) { throw new RuntimeException('A path must be provided'); } $projectPaths = array(); foreach ($paths as $i => $path) { $projectPaths[$i] = realpath($path); if ($projectPaths[$i] === false) { throw new RuntimeException('Path "'.$path.'" not found'); } } $outputPath = rtrim($options['output'], '\\//').DIRECTORY_SEPARATOR; // Force a trailing slash \Swagger\Logger::getInstance()->log = function ($entry, $type) { $type = $type === E_USER_NOTICE ? 'INFO' : 'WARN'; if ($entry instanceof Exception) { $entry = $entry->getMessage(); } echo '[', $type, '] ', $entry, PHP_EOL; }; $swagger = new \Swagger\Swagger($projectPaths, $excludePaths); $resourceList = $swagger->getResourceList(array( 'output' => 'array', 'suffix' => $options['suffix'], 'basePath' => $options['url'], 'apiVersion' => $options['default-api-version'], 'swaggerVersion' => $options['default-swagger-version'], 'template' => $options['api-doc-template'] )); // Use the imported values from the `api-doc-template` or else use the given defaults. $resourceOptions = array( 'output' => 'json', 'defaultSwaggerVersion' => $resourceList['swaggerVersion'], 'defaultBasePath' => $options['default-base-path'] ); if (isset($resourceList['apiVersion'])) { $resourceOptions['defaultApiVersion'] = $resourceList['apiVersion']; } $resourceName = false; $output = array(); foreach ($swagger->getResourceNames() as $resourceName) { $json = $swagger->getResource($resourceName, $resourceOptions); $resourceName = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($resourceName, DIRECTORY_SEPARATOR)); $output[$resourceName] = $json; } if ($output) { if (file_exists($outputPath) && !is_dir($outputPath)) { throw new RuntimeException( sprintf('[%s] is not a directory', $outputPath) ); } else { if (!file_exists($outputPath) && !mkdir($outputPath, 0755, true)) { throw new RuntimeException( sprintf('[%s] is not writeable', $outputPath) ); } } $filename = $outputPath.'api-docs.json'; if (file_put_contents($filename, Swagger\Swagger::jsonEncode($resourceList, true))) { echo 'Created ', $filename, PHP_EOL; } if ($options['url'] == false) { $filename = $outputPath.'index.php'; if (file_exists($filename)) { echo 'Skipped ', $filename, PHP_EOL; } else { file_put_contents($filename, " $json) { $name = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($name, DIRECTORY_SEPARATOR)); $filename = $outputPath.$name.'.json'; echo 'Created ', $filename, PHP_EOL; file_put_contents($filename, $json); } echo PHP_EOL; } else { throw new RuntimeException('no valid resources found'); } } catch (Exception $e) { echo '[ERROR] ', $e->getMessage(); if ($options['debug']) { echo ' in ', $e->getFile(), ' on line ', $e->getLine(); } echo PHP_EOL, PHP_EOL; exit(1); }