Skip to content

Commit

Permalink
Feat: Back up swagger documentation, and create new
Browse files Browse the repository at this point in the history
  • Loading branch information
shahinyanmronas committed May 15, 2024
1 parent e375695 commit 01fe0d8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
'drivers' => [
'local' => [
'class' => LocalDriver::class,
'production_path' => storage_path(env('SWAGGER_DOCUMENTATION_PATH', '/swagger').DIRECTORY_SEPARATOR.'documentation.json'),
'production_path' => storage_path(env('SWAGGER_DOCUMENTATION_PATH', '/documentation').DIRECTORY_SEPARATOR.'documentation.json'),
],
'remote' => [
'class' => RemoteDriver::class,
Expand Down
25 changes: 24 additions & 1 deletion src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ public function __construct()

public function saveData(): void
{
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
$currentDocumentation = [];
$newData = $this->getTmpData();

if (file_exists($this->prodFilePath)) {
$currentDocumentation = $this->getDocumentation();
}

if (!empty($currentDocumentation) && $currentDocumentation !== $newData) {
$version = $this->getNextVersion();
$newFileName = str_replace('documentation.json', "documentation_$version.json", $this->prodFilePath);

copy($this->prodFilePath, $newFileName);
}

file_put_contents($this->prodFilePath, json_encode($newData));

$this->clearTmpData();
}
Expand All @@ -41,4 +55,13 @@ public function getDocumentation(): array

return json_decode($fileContent, true);
}

protected function getNextVersion(): string
{
$currentVersion = config('auto-doc.config_version', '0.1');

[$major, $minor] = explode('.', $currentVersion);

return "{$major}_{$minor}";
}
}
12 changes: 7 additions & 5 deletions tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use RonasIT\Support\AutoDoc\Drivers\LocalDriver;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;
use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException;

class LocalDriverTest extends TestCase
Expand Down Expand Up @@ -62,14 +63,15 @@ public function testCreateClassConfigEmpty()

public function testCreateDirectoryIfNotExists()
{
$productionPath = __DIR__ . '/../storage/non_existent_directory/documentation.json';
config(['auto-doc.drivers.local.production_path' => $productionPath]);
$productionPath = 'non_existent_directory/documentation.json';

Storage::makeDirectory($productionPath);

mkdir(dirname($productionPath), 0777, true);
config(['auto-doc.drivers.local.production_path' => $productionPath]);

$this->assertTrue(is_dir(dirname($productionPath)));
$this->assertTrue(Storage::exists($productionPath));

rmdir(dirname($productionPath));
Storage::delete($productionPath);
}

public function testGetAndSaveTmpData()
Expand Down

0 comments on commit 01fe0d8

Please sign in to comment.