From 381d0055069a71bbc9efedac4ef211c84748f6fb Mon Sep 17 00:00:00 2001 From: Shahinyanm Date: Mon, 29 Apr 2024 17:32:03 +0400 Subject: [PATCH 1/5] - add new config key -increase config version -on documentation saving directory should be created if not exists --- config/auto-doc.php | 6 +++--- src/Drivers/LocalDriver.php | 4 ++++ src/Drivers/StorageDriver.php | 4 ++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index aec6fb45..b29be2e9 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -128,7 +128,7 @@ 'drivers' => [ 'local' => [ 'class' => LocalDriver::class, - 'production_path' => storage_path('documentation.json') + 'production_path' => storage_path(env('SWAGGER_DOCUMENTATION_PATH', '/swagger/documentation.json')), ], 'remote' => [ 'class' => RemoteDriver::class, @@ -146,7 +146,7 @@ | One of the filesystems.disks config value */ 'disk' => env('SWAGGER_STORAGE_DRIVER_DISK', 'public'), - 'production_path' => 'documentation.json' + 'production_path' => env('SWAGGER_DOCUMENTATION_PATH', '/swagger/documentation.json'), ] ], @@ -184,5 +184,5 @@ 'development' ], - 'config_version' => '2.7' + 'config_version' => '2.8' ]; diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index c2801563..13f0594c 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -18,6 +18,10 @@ public function __construct() if (empty($this->prodFilePath)) { throw new MissedProductionFilePathException(); } + + if (!is_dir(dirname($this->prodFilePath))) { + mkdir(dirname($this->prodFilePath),0777, true); + } } public function saveData(): void diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index b0b142f9..a34a1b4d 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -21,6 +21,10 @@ public function __construct() if (empty($this->prodFilePath)) { throw new MissedProductionFilePathException(); } + + if (!is_dir(dirname($this->prodFilePath))) { + mkdir(dirname($this->prodFilePath),0777, true); + } } public function saveData(): void From 7eaffc0d70f9f92dfd559622d5ed18dee616edce Mon Sep 17 00:00:00 2001 From: shahinyanmronas Date: Mon, 29 Apr 2024 17:57:30 +0400 Subject: [PATCH 2/5] feat: Increase config version add new config key for documentation saving and check directory existing --- config/auto-doc.php | 6 +++--- src/Drivers/LocalDriver.php | 4 ++++ src/Drivers/StorageDriver.php | 4 ++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index aec6fb45..b29be2e9 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -128,7 +128,7 @@ 'drivers' => [ 'local' => [ 'class' => LocalDriver::class, - 'production_path' => storage_path('documentation.json') + 'production_path' => storage_path(env('SWAGGER_DOCUMENTATION_PATH', '/swagger/documentation.json')), ], 'remote' => [ 'class' => RemoteDriver::class, @@ -146,7 +146,7 @@ | One of the filesystems.disks config value */ 'disk' => env('SWAGGER_STORAGE_DRIVER_DISK', 'public'), - 'production_path' => 'documentation.json' + 'production_path' => env('SWAGGER_DOCUMENTATION_PATH', '/swagger/documentation.json'), ] ], @@ -184,5 +184,5 @@ 'development' ], - 'config_version' => '2.7' + 'config_version' => '2.8' ]; diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index c2801563..13f0594c 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -18,6 +18,10 @@ public function __construct() if (empty($this->prodFilePath)) { throw new MissedProductionFilePathException(); } + + if (!is_dir(dirname($this->prodFilePath))) { + mkdir(dirname($this->prodFilePath),0777, true); + } } public function saveData(): void diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index b0b142f9..a34a1b4d 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -21,6 +21,10 @@ public function __construct() if (empty($this->prodFilePath)) { throw new MissedProductionFilePathException(); } + + if (!is_dir(dirname($this->prodFilePath))) { + mkdir(dirname($this->prodFilePath),0777, true); + } } public function saveData(): void From d67584879b4a7336e12e2055faa67ebffb7bb42a Mon Sep 17 00:00:00 2001 From: shahinyanmronas Date: Mon, 6 May 2024 18:07:41 +0400 Subject: [PATCH 3/5] Feat: Create Test for Swagger documentation directory --- config/auto-doc.php | 4 ++-- src/Drivers/LocalDriver.php | 2 +- src/Drivers/StorageDriver.php | 4 ++-- tests/LocalDriverTest.php | 12 ++++++++++++ tests/StorageDriverTest.php | 11 +++++++++++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index b29be2e9..00bd8ed0 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -128,7 +128,7 @@ 'drivers' => [ 'local' => [ 'class' => LocalDriver::class, - 'production_path' => storage_path(env('SWAGGER_DOCUMENTATION_PATH', '/swagger/documentation.json')), + 'production_path' => storage_path(env('SWAGGER_DOCUMENTATION_PATH', '/swagger').DIRECTORY_SEPARATOR.'documentation.json'), ], 'remote' => [ 'class' => RemoteDriver::class, @@ -146,7 +146,7 @@ | One of the filesystems.disks config value */ 'disk' => env('SWAGGER_STORAGE_DRIVER_DISK', 'public'), - 'production_path' => env('SWAGGER_DOCUMENTATION_PATH', '/swagger/documentation.json'), + 'production_path' => env('SWAGGER_DOCUMENTATION_PATH', '/swagger').DIRECTORY_SEPARATOR.'documentation.json', ] ], diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 13f0594c..5bc0aa08 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -20,7 +20,7 @@ public function __construct() } if (!is_dir(dirname($this->prodFilePath))) { - mkdir(dirname($this->prodFilePath),0777, true); + mkdir(dirname($this->prodFilePath), 0777, true); } } diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index a34a1b4d..b55e967f 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -22,8 +22,8 @@ public function __construct() throw new MissedProductionFilePathException(); } - if (!is_dir(dirname($this->prodFilePath))) { - mkdir(dirname($this->prodFilePath),0777, true); + if (!Storage::exists(dirname($this->prodFilePath))) { + Storage::makeDirectory(dirname($this->prodFilePath)); } } diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index f938a09f..1e5e60c8 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -60,6 +60,18 @@ public function testCreateClassConfigEmpty() new LocalDriver(); } + public function testCreateDirectoryIfNotExists() + { + $productionPath = __DIR__ . '/../storage/non_existent_directory/documentation.json'; + config(['auto-doc.drivers.local.production_path' => $productionPath]); + + mkdir(dirname($productionPath), 0777, true); + + $this->assertTrue(is_dir(dirname($productionPath))); + + rmdir(dirname($productionPath)); + } + public function testGetAndSaveTmpData() { $this->localDriverClass->saveTmpData($this->tmpData); diff --git a/tests/StorageDriverTest.php b/tests/StorageDriverTest.php index 681f9057..53c0e6db 100755 --- a/tests/StorageDriverTest.php +++ b/tests/StorageDriverTest.php @@ -65,6 +65,17 @@ public function testCreateClassConfigEmpty() new StorageDriver(); } + public function testCreateDirectoryIfNotExists() + { + config(['auto-doc.drivers.storage.production_path' => 'non_existent_directory/documentation.json']); + + Storage::disk('testing')->makeDirectory('non_existent_directory'); + + $this->assertTrue(Storage::disk('testing')->exists('non_existent_directory')); + + Storage::disk('testing')->deleteDirectory('non_existent_directory'); + } + public function testGetAndSaveTmpData() { $this->storageDriverClass->saveTmpData($this->tmpData); From 01fe0d875e3767742510d1e7cb23227ca4bfe82b Mon Sep 17 00:00:00 2001 From: shahinyanmronas Date: Wed, 15 May 2024 10:14:29 +0400 Subject: [PATCH 4/5] Feat: Back up swagger documentation, and create new --- config/auto-doc.php | 2 +- src/Drivers/LocalDriver.php | 25 ++++++++++++++++++++++++- tests/LocalDriverTest.php | 12 +++++++----- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index 00bd8ed0..13895544 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -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, diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 5bc0aa08..ba13d90d 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -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(); } @@ -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}"; + } } diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index 1e5e60c8..c762e0cb 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -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 @@ -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() From 38d2666d7ea9254e195ad71518272e723148c653 Mon Sep 17 00:00:00 2001 From: shahinyanmronas Date: Wed, 15 May 2024 17:51:47 +0400 Subject: [PATCH 5/5] Feat: Updateing file path and file name config --- config/auto-doc.php | 6 ++++-- src/Drivers/LocalDriver.php | 11 ++++++++--- src/Drivers/StorageDriver.php | 11 ++++++++--- tests/AutoDocControllerTest.php | 12 ++++++++++-- tests/LocalDriverTest.php | 29 ++++++++++++++++++++++------- tests/StorageDriverTest.php | 25 ++++++++++++++++++------- 6 files changed, 70 insertions(+), 24 deletions(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index 13895544..67db9a61 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -128,7 +128,8 @@ 'drivers' => [ 'local' => [ 'class' => LocalDriver::class, - 'production_path' => storage_path(env('SWAGGER_DOCUMENTATION_PATH', '/documentation').DIRECTORY_SEPARATOR.'documentation.json'), + 'file_path' => env('SWAGGER_DOCUMENTATION_PATH', 'documentation'), + 'file_name' => '/documentation.json', ], 'remote' => [ 'class' => RemoteDriver::class, @@ -146,7 +147,8 @@ | One of the filesystems.disks config value */ 'disk' => env('SWAGGER_STORAGE_DRIVER_DISK', 'public'), - 'production_path' => env('SWAGGER_DOCUMENTATION_PATH', '/swagger').DIRECTORY_SEPARATOR.'documentation.json', + 'file_name' => '/documentation.json', + 'file_path' => env('SWAGGER_DOCUMENTATION_PATH', 'documentation'), ] ], diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index ba13d90d..d4d725f4 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -13,9 +13,14 @@ public function __construct() { parent::__construct(); - $this->prodFilePath = config('auto-doc.drivers.local.production_path'); - - if (empty($this->prodFilePath)) { + $this->prodFilePath = storage_path(config('auto-doc.drivers.local.file_path').config('auto-doc.drivers.local.file_name')); + + if (empty(config('auto-doc.drivers.local.file_path')) + || + empty(config('auto-doc.drivers.local.file_name')) + || + empty($this->prodFilePath) + ) { throw new MissedProductionFilePathException(); } diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index b55e967f..d6ad90d3 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -16,9 +16,14 @@ public function __construct() parent::__construct(); $this->disk = Storage::disk(config('auto-doc.drivers.storage.disk')); - $this->prodFilePath = config('auto-doc.drivers.storage.production_path'); - - if (empty($this->prodFilePath)) { + $this->prodFilePath = config('auto-doc.drivers.local.file_path').config('auto-doc.drivers.local.file_name'); + + if (empty(config('auto-doc.drivers.local.file_path')) + || + empty(config('auto-doc.drivers.local.file_name')) + || + empty($this->prodFilePath) + ) { throw new MissedProductionFilePathException(); } diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index 2148fedc..fb634bd1 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -3,6 +3,7 @@ namespace RonasIT\Support\Tests; use Illuminate\Http\Response; +use Illuminate\Support\Facades\Storage; use phpmock\phpunit\PHPMock; use RonasIT\Support\Tests\Support\Traits\MockTrait; @@ -13,17 +14,24 @@ class AutoDocControllerTest extends TestCase protected $documentation; protected $localDriverFilePath; + protected $fileName; + protected $filePath; public function setUp(): void { parent::setUp(); - $this->localDriverFilePath = __DIR__ . '/../storage/documentation.json'; + $this->filePath = Storage::path('storage'); + $this->fileName = '/documentation.json'; + $this->localDriverFilePath = storage_path($this->filePath.$this->fileName); $this->documentation = $this->getJsonFixture('tmp_data'); file_put_contents($this->localDriverFilePath, json_encode($this->documentation)); - config(['auto-doc.drivers.local.production_path' => $this->localDriverFilePath]); + config([ + 'auto-doc.drivers.local.file_name' => $this->fileName, + 'auto-doc.drivers.local.file_path' => $this->filePath, + ]); } public function tearDown(): void diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index c762e0cb..071e2648 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -11,6 +11,8 @@ class LocalDriverTest extends TestCase { protected $localDriverClass; protected $productionFilePath; + protected $filePath; + protected $fileName; protected $tmpDocumentationFilePath; protected $tmpData; @@ -18,12 +20,17 @@ public function setUp(): void { parent::setUp(); - $this->productionFilePath = __DIR__ . '/../storage/documentation.json'; - $this->tmpDocumentationFilePath = __DIR__ . '/../storage/temp_documentation.json'; + $this->filePath = Storage::path('storage'); + $this->fileName = '/documentation.json'; + $this->productionFilePath = storage_path($this->filePath.$this->fileName); + $this->tmpDocumentationFilePath = storage_path('temp_documentation.json'); $this->tmpData = $this->getJsonFixture('tmp_data'); - config(['auto-doc.drivers.local.production_path' => $this->productionFilePath]); + config([ + 'auto-doc.drivers.local.file_name' => $this->fileName, + 'auto-doc.drivers.local.file_path' => $this->filePath, + ]); $this->localDriverClass = new LocalDriver(); } @@ -56,8 +63,10 @@ public function testCreateClassConfigEmpty() { $this->expectException(MissedProductionFilePathException::class); - config(['auto-doc.drivers.local.production_path' => null]); - + config([ + 'auto-doc.drivers.local.file_name' => null, + 'auto-doc.drivers.local.file_path' => null, + ]); new LocalDriver(); } @@ -67,7 +76,10 @@ public function testCreateDirectoryIfNotExists() Storage::makeDirectory($productionPath); - config(['auto-doc.drivers.local.production_path' => $productionPath]); + config([ + 'auto-doc.drivers.local.file_name' => '/documentation.json', + 'auto-doc.drivers.local.file_path' => 'non_existent_directory', + ]); $this->assertTrue(Storage::exists($productionPath)); @@ -106,7 +118,10 @@ public function testGetDocumentationFileNotExists() { $this->expectException(FileNotFoundException::class); - config(['auto-doc.drivers.local.production_path' => 'not_exists_file']); + config([ + 'auto-doc.drivers.local.file_name' => 'not_exists_file', + 'auto-doc.drivers.local.file_path' => 'not_exists_file', + ]); (new LocalDriver())->getDocumentation(); } diff --git a/tests/StorageDriverTest.php b/tests/StorageDriverTest.php index 53c0e6db..b36f69dd 100755 --- a/tests/StorageDriverTest.php +++ b/tests/StorageDriverTest.php @@ -12,6 +12,8 @@ class StorageDriverTest extends TestCase protected $storageDriverClass; protected $disk; protected $productionFilePath; + protected $fileName; + protected $filePath; protected $tmpDocumentationFilePath; protected $tmpData; @@ -20,14 +22,18 @@ public function setUp(): void parent::setUp(); $this->disk = Storage::fake('testing'); - - $this->productionFilePath = 'documentation.json'; - $this->tmpDocumentationFilePath = __DIR__ . '/../storage/temp_documentation.json'; + $this->filePath = Storage::path('storage'); + $this->fileName = '/documentation.json'; + $this->productionFilePath = $this->filePath.$this->fileName; + $this->tmpDocumentationFilePath = storage_path('temp_documentation.json'); $this->tmpData = $this->getJsonFixture('tmp_data'); config(['auto-doc.drivers.storage.disk' => 'testing']); - config(['auto-doc.drivers.storage.production_path' => $this->productionFilePath]); + config([ + 'auto-doc.drivers.local.file_name' => $this->fileName, + 'auto-doc.drivers.local.file_path' => $this->filePath, + ]); $this->storageDriverClass = new StorageDriver(); } @@ -60,14 +66,19 @@ public function testCreateClassConfigEmpty() { $this->expectException(MissedProductionFilePathException::class); - config(['auto-doc.drivers.storage.production_path' => null]); - + config([ + 'auto-doc.drivers.local.file_name' => null, + 'auto-doc.drivers.local.file_path' => null, + ]); new StorageDriver(); } public function testCreateDirectoryIfNotExists() { - config(['auto-doc.drivers.storage.production_path' => 'non_existent_directory/documentation.json']); + config([ + 'auto-doc.drivers.local.file_name' => '/documentation.json', + 'auto-doc.drivers.local.file_path' => 'non_existent_directory', + ]); Storage::disk('testing')->makeDirectory('non_existent_directory');