Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Store documentation file on a configurable directory #152

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
8 changes: 5 additions & 3 deletions config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@
'drivers' => [
'local' => [
'class' => LocalDriver::class,
'production_path' => storage_path('documentation.json'),
'directory' => 'documentations',
'base_file_name' => 'documentation',
],
'remote' => [
'class' => RemoteDriver::class,
Expand All @@ -146,7 +147,8 @@
| One of the filesystems.disks config value
*/
'disk' => env('SWAGGER_STORAGE_DRIVER_DISK', 'public'),
'production_path' => 'documentation.json',
'directory' => 'documentations',
'base_file_name' => 'documentation',
],
],

Expand Down Expand Up @@ -184,5 +186,5 @@
'development',
],

'config_version' => '2.8',
'config_version' => '2.9',
];
24 changes: 18 additions & 6 deletions src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,45 @@

class LocalDriver extends BaseDriver
{
protected ?string $prodFilePath;
protected ?string $baseFileName;
private ?array $config;

public function __construct()
{
parent::__construct();
$this->config = config('auto-doc.drivers.local');

$this->prodFilePath = config('auto-doc.drivers.local.production_path');
$directory = $this->config['directory'];
if (!str_ends_with($directory, DIRECTORY_SEPARATOR)) {
$directory .= DIRECTORY_SEPARATOR;
}

$this->baseFileName = storage_path($directory.$this->config['base_file_name'].'.json');

if (empty($this->prodFilePath)) {
if (!preg_match('/\/[\w]+\.json/ms', $this->baseFileName)) {
throw new MissedProductionFilePathException();
}
}

public function saveData(): void
{
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
$prodDir = storage_path($this->config['directory']);
if (!is_dir($prodDir)) {
mkdir($prodDir);
}

file_put_contents($this->baseFileName, json_encode($this->getTmpData()));

$this->clearTmpData();
}

public function getDocumentation(): array
{
if (!file_exists($this->prodFilePath)) {
if (!file_exists($this->baseFileName)) {
throw new FileNotFoundException();
}

$fileContent = file_get_contents($this->prodFilePath);
$fileContent = file_get_contents($this->baseFileName);

return json_decode($fileContent, true);
}
Expand Down
20 changes: 13 additions & 7 deletions src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,40 @@
class StorageDriver extends BaseDriver
{
protected Filesystem $disk;
protected ?string $prodFilePath;
protected ?string $baseFileName;
protected array $config;

public function __construct()
{
parent::__construct();

$this->disk = Storage::disk(config('auto-doc.drivers.storage.disk'));
$this->prodFilePath = config('auto-doc.drivers.storage.production_path');
$this->config = config('auto-doc.drivers.storage');
$this->disk = Storage::disk($this->config['disk']);
$directory = $this->config['directory'];
if (!str_ends_with($directory, DIRECTORY_SEPARATOR)) {
$directory .= DIRECTORY_SEPARATOR;
}
$this->baseFileName = $directory.$this->config['base_file_name'].'.json';

if (empty($this->prodFilePath)) {
if (!preg_match('/\/[\w]+\.json/ms', $this->baseFileName)) {
throw new MissedProductionFilePathException();
}
}

public function saveData(): void
{
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));
$this->disk->put($this->baseFileName, json_encode($this->getTmpData()));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Storage driver should create storage directory only for non Cloud drivers (on documentation saving)

I believe the Filesystem class will take care of it.


$this->clearTmpData();
}

public function getDocumentation(): array
{
if (!$this->disk->exists($this->prodFilePath)) {
if (!$this->disk->exists($this->baseFileName)) {
throw new FileNotFoundException();
}

$fileContent = $this->disk->get($this->prodFilePath);
$fileContent = $this->disk->get($this->baseFileName);

return json_decode($fileContent, true);
}
Expand Down
18 changes: 14 additions & 4 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ class AutoDocControllerTest extends TestCase
use PHPMock;

protected static array $documentation;
protected static string $localDriverFilePath;
protected static string $baseFileName;
protected static string $baseFile;

public function setUp(): void
{
parent::setUp();

self::$localDriverFilePath ??= __DIR__ . '/../storage/documentation.json';
$documentationDirectory = config('auto-doc.drivers.local.directory');
if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) {
$documentationDirectory .= DIRECTORY_SEPARATOR;
}

self::$baseFileName ??= 'documentation';
self::$baseFile ??= $documentationDirectory.self::$baseFileName.'.json';
self::$documentation ??= $this->getJsonFixture('tmp_data');

file_put_contents(self::$localDriverFilePath, json_encode(self::$documentation));
if (!is_dir(storage_path($documentationDirectory))) {
mkdir(storage_path($documentationDirectory));
}
file_put_contents(storage_path(self::$baseFile), json_encode(self::$documentation));

config(['auto-doc.drivers.local.production_path' => self::$localDriverFilePath]);
config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]);
}

public function tearDown(): void
Expand Down
25 changes: 16 additions & 9 deletions tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@
class LocalDriverTest extends TestCase
{
protected static LocalDriver $localDriverClass;
protected static string $productionFilePath;
protected static string $baseFileName;
protected static string $baseFile;
protected static string $tmpDocumentationFilePath;
protected static array $tmpData;

public function setUp(): void
{
parent::setUp();

self::$productionFilePath ??= __DIR__ . '/../storage/documentation.json';
self::$tmpDocumentationFilePath ??= __DIR__ . '/../storage/temp_documentation.json';
$documentationDirectory = config('auto-doc.drivers.local.directory');
if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) {
$documentationDirectory .= DIRECTORY_SEPARATOR;
}

self::$baseFileName ??= 'documentation';
self::$baseFile ??= storage_path($documentationDirectory.self::$baseFileName.'.json');
self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json');

self::$tmpData ??= $this->getJsonFixture('tmp_data');

config(['auto-doc.drivers.local.production_path' => self::$productionFilePath]);
config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]);

self::$localDriverClass ??= new LocalDriver();
}
Expand Down Expand Up @@ -55,7 +62,7 @@ public function testCreateClassConfigEmpty()
{
$this->expectException(MissedProductionFilePathException::class);

config(['auto-doc.drivers.local.production_path' => null]);
config(['auto-doc.drivers.local.base_file_name' => null]);

new LocalDriver();
}
Expand All @@ -73,15 +80,15 @@ public function testSaveData()

self::$localDriverClass->saveData();

$this->assertFileExists(self::$productionFilePath);
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$productionFilePath);
$this->assertFileExists(self::$baseFile);
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$baseFile);

$this->assertFileDoesNotExist(self::$tmpDocumentationFilePath);
}

public function testGetDocumentation()
{
file_put_contents(self::$productionFilePath, json_encode(self::$tmpData));
file_put_contents(self::$baseFile, json_encode(self::$tmpData));

$documentation = self::$localDriverClass->getDocumentation();

Expand All @@ -92,7 +99,7 @@ public function testGetDocumentationFileNotExists()
{
$this->expectException(FileNotFoundException::class);

config(['auto-doc.drivers.local.production_path' => 'not_exists_file']);
config(['auto-doc.drivers.local.base_file_name' => 'not_exists_file.json']);

(new LocalDriver())->getDocumentation();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/RemoteDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setUp(): void
parent::setUp();

self::$tmpData ??= $this->getJsonFixture('tmp_data');
self::$tmpDocumentationFilePath ??= __DIR__ . '/../storage/temp_documentation.json';
self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json');

self::$remoteDriverClass ??= new RemoteDriver();
}
Expand Down
23 changes: 15 additions & 8 deletions tests/StorageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class StorageDriverTest extends TestCase
{
protected static StorageDriver $storageDriverClass;
protected Filesystem $disk;
protected static string $productionFilePath;
protected static string $baseFileName;
protected static string $baseFile;
protected static string $tmpDocumentationFilePath;
protected static array $tmpData;

Expand All @@ -22,13 +23,19 @@ public function setUp(): void

$this->disk = Storage::fake('testing');

self::$productionFilePath ??= 'documentation.json';
self::$tmpDocumentationFilePath ??= __DIR__ . '/../storage/temp_documentation.json';
$documentationDirectory = config('auto-doc.drivers.storage.directory');
if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) {
$documentationDirectory .= DIRECTORY_SEPARATOR;
}

self::$baseFileName ??= 'documentation';
self::$baseFile ??= $documentationDirectory.self::$baseFileName.'.json';
self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json');

self::$tmpData ??= $this->getJsonFixture('tmp_data');

config(['auto-doc.drivers.storage.disk' => 'testing']);
config(['auto-doc.drivers.storage.production_path' => self::$productionFilePath]);
config(['auto-doc.drivers.storage.base_file_name' => self::$baseFileName]);

self::$storageDriverClass = new StorageDriver();
}
Expand Down Expand Up @@ -61,7 +68,7 @@ public function testCreateClassConfigEmpty()
{
$this->expectException(MissedProductionFilePathException::class);

config(['auto-doc.drivers.storage.production_path' => null]);
config(['auto-doc.drivers.storage.base_file_name' => null]);

new StorageDriver();
}
Expand All @@ -79,15 +86,15 @@ public function testSaveData()

self::$storageDriverClass->saveData();

$this->disk->assertExists(self::$productionFilePath);
$this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get(self::$productionFilePath));
$this->disk->assertExists(self::$baseFile);
$this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get(self::$baseFile));

$this->assertFileDoesNotExist(self::$tmpDocumentationFilePath);
}

public function testGetDocumentation()
{
$this->disk->put(self::$productionFilePath, $this->getFixture('tmp_data_non_formatted.json'));
$this->disk->put(self::$baseFile, $this->getFixture('tmp_data_non_formatted.json'));

$documentation = self::$storageDriverClass->getDocumentation();

Expand Down
Loading