function api($name) { $swagger = new \Swagger\Swagger(dirname(__DIR__)); header("Content-Type: application/json"); $result = $swagger->getResource('/' . $name, array('output' => 'json')); $result = get_object_vars(json_decode($result)); $result = str_replace('{baseUrl}', "http" . (!empty($_SERVER['HTTPS']) ? "s" : "") . "://" . $_SERVER['SERVER_NAME'] . $this->getBasePath(), $result); echo json_encode($result); }
public function fire() { $projectPaths = $this->realpaths($this->paths); $excludePaths = $this->realpaths($this->exclude); $outputPath = head((array) $this->output) . DIRECTORY_SEPARATOR; $swagger = new \Swagger\Swagger($projectPaths, $excludePaths); $resourceList = $swagger->getResourceList(['output' => 'array', 'suffix' => $this->suffix, 'apiVersion' => $this->default_api_version, 'swaggerVersion' => $this->default_swagger_version, 'template' => $this->api_doc_template]); $resourceOptions = ['output' => 'json', 'defaultSwaggerVersion' => $resourceList['swaggerVersion'], 'defaultBasePath' => $this->default_base_path]; if (isset($resourceList['apiVersion'])) { $resourceOptions['defaultApiVersion'] = $resourceList['apiVersion']; } $resourceName = false; $output = []; foreach ($swagger->getResourceNames() as $resourceName) { $json = $swagger->getResource($resourceName, $resourceOptions); $resourceName = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($resourceName, DIRECTORY_SEPARATOR)); $output[$resourceName] = $json; } if (!$output) { throw new SwaggerException('no valid resources found'); } if (file_exists($outputPath) && !is_dir($outputPath)) { throw new SwaggerException(sprintf('[%s] is not a directory', $outputPath)); } elseif (!file_exists($outputPath) && !mkdir($outputPath, 0755, true)) { throw new SwaggerException(sprintf('[%s] is not writeable', $outputPath)); } if (!file_exists($outputPath . '/.gitignore')) { file_put_contents($outputPath . '/.gitignore', "*\n!.gitignore"); } $filename = $outputPath . 'api-docs.json'; if (file_put_contents($filename, \Swagger\Swagger::jsonEncode($resourceList, true))) { $this->logger('Created ' . $filename); } foreach ($output as $name => $json) { $name = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($name, DIRECTORY_SEPARATOR)); $filename = $outputPath . $name . '.json'; $this->logger('Created ' . $filename); file_put_contents($filename, $json); } $this->logger(''); }
public function call() { //The Slim application $app = $this->app; // Swager Documentation $app->group($this->path, function () use($app) { $app->get('/', function () use($app) { $swagger = new \Swagger\Swagger($this->scanPath); $resourceList = $swagger->getResourceList(); echo json_encode($resourceList); }); $app->get('/:path', function ($path) use($app) { $swagger = new \Swagger\Swagger($this->scanPath); $resourceList = $swagger->getResourceList(); if (!empty($path)) { echo $swagger->getResource('/' . $path, $this->options); } else { echo json_encode($resourceList); } }); }); // Call next middleware $this->next->call(); }
// Possible options and their default values. $options = array('url' => null, 'default-base-path' => null, 'default-api-version' => null, 'default-swagger-version' => '1.2', 'version' => false, 'suffix' => '.{format}', 'help' => false, 'debug' => false); \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); $resourceListOptions = array('output' => 'json', 'suffix' => $options['suffix'], 'basePath' => $options['url'], 'apiVersion' => $options['default-api-version'], 'swaggerVersion' => $options['default-swagger-version']); $resourceOptions = array('output' => 'json', 'defaultBasePath' => $options['default-base-path'], 'defaultApiVersion' => $options['default-api-version'], 'defaultSwaggerVersion' => $options['default-swagger-version']); $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->getResourceList($resourceListOptions))) { echo 'Created ', $filename, PHP_EOL; }
public function resourceAction() { $swagger = new \Swagger\Swagger(__DIR__); $resource = $swagger->getResource('/' . $this->dispatcher->getParam('id')); return $this->response->setJsonContent($resource); }