Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 $mainFilePath;
private ?array $config;

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

$this->prodFilePath = config('auto-doc.drivers.local.production_path');
$this->config = config('auto-doc.drivers.local');

if (empty($this->prodFilePath)) {
$directory = str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
$directory = str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)
$directory = (str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made this change but I can't understand the difference.

? $this->config['directory']
: $this->config['directory'] . DIRECTORY_SEPARATOR;

$this->mainFilePath = storage_path("$directory{$this->config['base_file_name']}.json");

if (empty($this->config['base_file_name']) || !str_ends_with($this->mainFilePath, '.json')) {
throw new MissedProductionFilePathException();
}
}

public function saveData(): void
{
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
$documentationDirectory = storage_path($this->config['directory']);
Copy link

Copilot AI Aug 28, 2025

Choose a reason for hiding this comment

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

The directory path construction is inconsistent with the mainFilePath construction. When config['directory'] already ends with DIRECTORY_SEPARATOR, this will create the wrong directory path. Should use the same normalized $directory variable from lines 19-21.

Copilot uses AI. Check for mistakes.
if (!is_dir($documentationDirectory)) {
mkdir($documentationDirectory);
}

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

$this->clearTmpData();
}

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

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

return json_decode($fileContent, true);
}
Expand Down
21 changes: 14 additions & 7 deletions src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,41 @@
class StorageDriver extends BaseDriver
{
protected Filesystem $disk;
protected ?string $prodFilePath;
protected ?string $mainFilePath;
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']);

if (empty($this->prodFilePath)) {
$directory = str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)
Copy link
Collaborator

Choose a reason for hiding this comment

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

as you already did the same in the local driver, let's move this logic to the BaseDriver class

Suggested change
$directory = str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)
$directory = (str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR))

? $this->config['directory']
: $this->config['directory'] . DIRECTORY_SEPARATOR;

$this->mainFilePath = "$directory{$this->config['base_file_name']}.json";

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

public function saveData(): void
{
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));
$this->disk->put($this->mainFilePath, json_encode($this->getTmpData()));

$this->clearTmpData();
}

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

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

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

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

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

self::$localDriverFilePath ??= __DIR__ . '/../storage/documentation.json';
$documentationDirectory = config('auto-doc.drivers.local.directory');

self::$baseFile ??= $documentationDirectory.'/documentation.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' => 'documentation']);
}

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