Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkaOnLine committed Jun 21, 2018
1 parent 06e3b54 commit 41282b2
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 34 deletions.
182 changes: 148 additions & 34 deletions src/Generator.php
Expand Up @@ -3,69 +3,183 @@
namespace L5Swagger;

use File;
use L5Swagger\Exceptions\L5SwaggerException;
use Symfony\Component\Yaml\Dumper as YamlDumper;

class Generator
{
/**
* @var string
*/
protected $appDir;

/**
* @var string
*/
protected $docDir;

/**
* @var string
*/
protected $docsFile;

/**
* @var string
*/
protected $yamlDocsFile;

/**
* @var array
*/
protected $excludedDirs;

/**
* @var array
*/
protected $constants;

/**
* @var \Swagger\Annotations\Swagger
*/
protected $swagger;

/**
* @var bool
*/
protected $yamlCopyRequired;

public function __construct()
{
$this->appDir = config('l5-swagger.paths.annotations');
$this->docDir = config('l5-swagger.paths.docs');
$this->docsFile = $this->docDir.'/'.config('l5-swagger.paths.docs_json', 'api-docs.json');
$this->yamlDocsFile = $this->docDir.'/'.config('l5-swagger.paths.docs_yaml', 'api-docs.yaml');
$this->excludedDirs = config('l5-swagger.paths.excludes');
$this->constants = config('l5-swagger.constants') ?: [];
$this->yamlCopyRequired = config('l5-swagger.generate_yaml_copy', false);
}

public static function generateDocs()
{
$appDir = config('l5-swagger.paths.annotations');
$docDir = config('l5-swagger.paths.docs');
if (! File::exists($docDir) || is_writable($docDir)) {
// delete all existing documentation
if (File::exists($docDir)) {
File::deleteDirectory($docDir);
}
(new static)->prepareDirectory()
->defineConstants()
->scanFilesForDocumentation()
->populateServers()
->saveJson()
->makeYamlCopy();
}

self::defineConstants(config('l5-swagger.constants') ?: []);
/**
* Check directory structure and permissions.
*
* @return Generator
*/
protected function prepareDirectory()
{
if (File::exists($this->docDir) && ! is_writable($this->docDir)) {
throw new L5SwaggerException('Documentation storage directory is not writable');
}

File::makeDirectory($docDir);
$excludeDirs = config('l5-swagger.paths.excludes');
$swagger = \Swagger\scan($appDir, ['exclude' => $excludeDirs]);
// delete all existing documentation
if (File::exists($this->docDir)) {
File::deleteDirectory($this->docDir);
}

self::generateServers($swagger);
File::makeDirectory($this->docDir);

$docsJsonFile = $docDir.'/'.config('l5-swagger.paths.docs_json', 'api-docs.json');
$swagger->saveAs($docsJsonFile);
return $this;
}

if (config('l5-swagger.generate_yaml_copy', false)) {
file_put_contents(
$docDir.'/'.config('l5-swagger.paths.docs_yaml', 'api-docs.yaml'),
(new YamlDumper(2))->dump(json_decode(file_get_contents($docsJsonFile), true), 20)
);
/**
* Define constant which will be replaced.
*
* @return Generator
*/
protected function defineConstants()
{
if (! empty($this->constants)) {
foreach ($this->constants as $key => $value) {
defined($key) || define($key, $value);
}

$security = new SecurityDefinitions();
$security->generate($docsJsonFile);
}

return $this;
}

/**
* Scan directory and create Swagger.
*
* @return Generator
*/
protected function scanFilesForDocumentation()
{
$this->swagger = \Swagger\scan(
$this->appDir,
['exclude' => $this->excludedDirs]
);

return $this;
}

/**
* Generate servers section or basePath depending on Swagger version.
*
* @return Generator
*/
protected static function generateServers($swagger)
protected function populateServers()
{
if (config('l5-swagger.paths.base') !== null) {
$isVersion3 = version_compare(config('l5-swagger.swagger_version'), '3.0', '>=');

if ($isVersion3) {
$swagger->servers = [
if ($this->isOpenApi()) {
$this->swagger->servers = [
new \Swagger\Annotations\Server(['url' => config('l5-swagger.paths.base')]),
];
}

if (! $isVersion3) {
$swagger->basePath = config('l5-swagger.paths.base');
if (! $this->isOpenApi()) {
$this->swagger->basePath = config('l5-swagger.paths.base');
}
}

return $this;
}

protected static function defineConstants(array $constants)
/**
* Save documentation as json file.
*
* @return Generator
*/
protected function saveJson()
{
if (! empty($constants)) {
foreach ($constants as $key => $value) {
defined($key) || define($key, $value);
}
$this->swagger->saveAs($this->docsFile);

$security = new SecurityDefinitions();
$security->generate($this->docsFile);

return $this;
}

/**
* Save documentation as yaml file.
*
* @return Generator
*/
protected function makeYamlCopy()
{
if ($this->yamlCopyRequired) {
file_put_contents(
$this->yamlDocsFile,
(new YamlDumper(2))->dump(json_decode(file_get_contents($this->docsFile), true), 20)
);
}
}

/**
* Check which documentation version is used.
*
* @return bool
*/
protected function isOpenApi()
{
return version_compare(config('l5-swagger.swagger_version'), '3.0', '>=');
}
}
16 changes: 16 additions & 0 deletions tests/GeneratorTest.php
Expand Up @@ -3,9 +3,25 @@
namespace Tests;

use L5Swagger\Generator;
use L5Swagger\Exceptions\L5SwaggerException;

class GeneratorTest extends TestCase
{
/** @test **/
public function itThrowsExceptionIfDocumatationDirIsNotWritable()
{
$this->setAnnotationsPath();

mkdir(config('l5-swagger.paths.docs'), 0555);
chmod(config('l5-swagger.paths.docs'), 0555);

$this->expectException(L5SwaggerException::class);

Generator::generateDocs();

chmod(config('l5-swagger.paths.docs'), 0777);
}

/** @test */
public function canGenerateApiJsonFile()
{
Expand Down

0 comments on commit 41282b2

Please sign in to comment.