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 assertOutputEqualsJson($outputFile, $json, $message = '') { if (file_exists($this->outputDir($outputFile))) { $outputFile = $this->outputDir($outputFile); } $expected = json_decode(file_get_contents($outputFile)); $error = json_last_error(); if ($error !== JSON_ERROR_NONE) { $this->fail('File: "' . $outputFile . '" doesn\'t contain valid json, error ' . $error); } if (is_string($json) === false) { $this->fail('Not a (json) string'); } $actual = json_decode($json); $error = json_last_error(); if ($error !== JSON_ERROR_NONE) { $this->fail('invalid json, error ' . $error); } $expectedJson = Swagger::jsonEncode($this->sort($expected, '"' . $outputFile . '" '), true); $actualJson = Swagger::jsonEncode($this->sort($actual, 'generated json '), true); return $this->assertEquals($expectedJson, $actualJson, $message); }
/** * Test the examples against the json files in ExamplesOutput. * * @group examples * @dataProvider getExampleDirs * @param string $exampleDir */ public function testExample($exampleDir) { $swagger = new Swagger($this->examplesDir($exampleDir)); $dir = new \DirectoryIterator($this->outputDir($exampleDir)); $options = array('output' => 'json'); foreach ($dir as $entry) { if ($entry->getExtension() === 'json') { $name = $entry->getBasename('.json'); if (isset($swagger->registry['/' . $name])) { // Resource? $this->assertOutputEqualsJson($entry->getPathname(), $swagger->getResource('/' . $name, $options), 'Resource "/' . $name . '" doesn\'t match expected output in "' . $entry->getPathname() . '"'); } elseif ($name === 'api-docs') { // Listing? $this->assertOutputEqualsJson($entry->getPathname(), $swagger->getResourceList($options), 'Resource listing doesn\'t match expected output in "' . $entry->getPathname() . '"'); } elseif (isset($swagger->models[$name])) { // Model $this->assertOutputEqualsJson($entry->getPathname(), Swagger::jsonEncode($swagger->models[$name]), 'Model "' . $name . '" doesn\'t match expected output in "' . $entry->getPathname() . '"'); } else { $this->fail('Resource or model "' . $name . '" not detected in "' . $this->examplesDir($exampleDir) . '"'); } } } }
File::makeDirectory($docDir); $defaultBasePath = Config::get('swagger.default-base-path'); $defaultApiVersion = Config::get('swagger.default-api-version'); $defaultSwaggerVersion = Config::get('swagger.default-swagger-version'); $excludeDirs = Config::get('swagger.excludes'); $swagger = new Swagger($appDir, $excludeDirs); $resourceList = $swagger->getResourceList(array('output' => 'array', 'apiVersion' => $defaultApiVersion, 'swaggerVersion' => $defaultSwaggerVersion)); $resourceOptions = array('output' => 'json', 'defaultSwaggerVersion' => $resourceList['swaggerVersion'], 'defaultBasePath' => $defaultBasePath); $output = array(); foreach ($swagger->getResourceNames() as $resourceName) { $json = $swagger->getResource($resourceName, $resourceOptions); $resourceName = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($resourceName, DIRECTORY_SEPARATOR)); $output[$resourceName] = $json; } $filename = $docDir . '/api-docs.json'; file_put_contents($filename, Swagger::jsonEncode($resourceList, true)); foreach ($output as $name => $json) { $name = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($name, DIRECTORY_SEPARATOR)); $filename = $docDir . '/' . $name . '.json'; file_put_contents($filename, $json); } } } if (Config::get('swagger.behind-reverse-proxy')) { $proxy = Request::server('REMOTE_ADDR'); Request::setTrustedProxies(array($proxy)); } Blade::setEscapedContentTags('{{{', '}}}'); Blade::setContentTags('{{', '}}'); //need the / at the end to avoid CORS errors on Homestead systems. $response = response()->view('swagger::index', array('secure' => Request::secure(), 'urlToDocs' => url(Config::get('swagger.doc-route')), 'requestHeaders' => Config::get('swagger.requestHeaders')));