Skip to content

Commit db649e9

Browse files
committed
Add .json extension to base_file_name attr and check target dir existance;
1 parent 8d25c0f commit db649e9

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/Drivers/BaseDriver.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ abstract class BaseDriver implements SwaggerDriverContract
1010

1111
public function __construct()
1212
{
13-
$prodDir = config('auto-doc.documentation_directory');
14-
if (!is_dir($prodDir)) {
15-
mkdir($prodDir);
16-
}
17-
$this->tempFilePath = $prodDir.DIRECTORY_SEPARATOR.'temp_documentation.json';
13+
$this->tempFilePath = storage_path('temp_documentation.json');
1814
}
1915

2016
public function saveTmpData($data): void

src/Drivers/LocalDriver.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,45 @@
77

88
class LocalDriver extends BaseDriver
99
{
10-
protected ?string $prodFilePath;
10+
protected ?string $baseFileName;
11+
private ?array $config;
1112

1213
public function __construct()
1314
{
1415
parent::__construct();
15-
$this->prodFilePath = config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR.config('auto-doc.drivers.local.production_path');
16+
$this->config = config('auto-doc.drivers.local');
1617

17-
if (!preg_match('/\/[\w]+\.json/ms',$this->prodFilePath)) {
18+
$directory = $this->config['directory'];
19+
if (!str_ends_with($directory, DIRECTORY_SEPARATOR)) {
20+
$directory .= DIRECTORY_SEPARATOR;
21+
}
22+
23+
$this->baseFileName = storage_path($directory.$this->config['base_file_name'].'.json');
24+
25+
if (!preg_match('/\/[\w]+\.json/ms', $this->baseFileName)) {
1826
throw new MissedProductionFilePathException();
1927
}
2028
}
2129

2230
public function saveData(): void
2331
{
24-
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
32+
$prodDir = storage_path($this->config['directory']);
33+
if (!is_dir($prodDir)) {
34+
mkdir($prodDir);
35+
}
36+
37+
file_put_contents($this->baseFileName, json_encode($this->getTmpData()));
2538

2639
$this->clearTmpData();
2740
}
2841

2942
public function getDocumentation(): array
3043
{
31-
if (!file_exists($this->prodFilePath)) {
44+
if (!file_exists($this->baseFileName)) {
3245
throw new FileNotFoundException();
3346
}
3447

35-
$fileContent = file_get_contents($this->prodFilePath);
48+
$fileContent = file_get_contents($this->baseFileName);
3649

3750
return json_decode($fileContent, true);
3851
}

src/Drivers/StorageDriver.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,40 @@
1010
class StorageDriver extends BaseDriver
1111
{
1212
protected Filesystem $disk;
13-
protected ?string $prodFilePath;
13+
protected ?string $baseFileName;
14+
protected array $config;
1415

1516
public function __construct()
1617
{
1718
parent::__construct();
1819

19-
$this->disk = Storage::disk(config('auto-doc.drivers.storage.disk'));
20-
$this->prodFilePath = config('auto-doc.drivers.storage.production_path');
20+
$this->config = config('auto-doc.drivers.storage');
21+
$this->disk = Storage::disk($this->config['disk']);
22+
$directory = $this->config['directory'];
23+
if (!str_ends_with($directory, DIRECTORY_SEPARATOR)) {
24+
$directory .= DIRECTORY_SEPARATOR;
25+
}
26+
$this->baseFileName = $directory.$this->config['base_file_name'].'.json';
2127

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

2733
public function saveData(): void
2834
{
29-
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));
35+
$this->disk->put($this->baseFileName, json_encode($this->getTmpData()));
3036

3137
$this->clearTmpData();
3238
}
3339

3440
public function getDocumentation(): array
3541
{
36-
if (!$this->disk->exists($this->prodFilePath)) {
42+
if (!$this->disk->exists($this->baseFileName)) {
3743
throw new FileNotFoundException();
3844
}
3945

40-
$fileContent = $this->disk->get($this->prodFilePath);
46+
$fileContent = $this->disk->get($this->baseFileName);
4147

4248
return json_decode($fileContent, true);
4349
}

0 commit comments

Comments
 (0)