Skip to content

Commit

Permalink
feat: imp v2;
Browse files Browse the repository at this point in the history
  • Loading branch information
vitgrams committed Oct 25, 2024
1 parent c64db73 commit e99c3a3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function getTmpData(): ?array

protected function clearTmpDir(): void
{
File::deleteDirectory(config('auto-doc.tmp_dir'));
if (file_exists($this->tempFilePath)) {
unlink($this->tempFilePath);
}
}
}
4 changes: 2 additions & 2 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,9 @@ public function saveProductionData()
$this->driver->saveData();
}

public function saveTmpData(array $data): void
public function getDocumentation(): array
{
$this->driver->saveTmpData($data);
return $this->driver->getDocumentation();
}

public function getDocFileContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public function notify(Finished $event): void
{
$this->createApplication();

if ($this->isReadyToSaveProductionData()) {
if ($token = ParallelTesting::token()) {
$this->mergeTempDocumentation($token);
} else {
app(SwaggerService::class)->saveProductionData();
}
}
Expand All @@ -28,41 +30,24 @@ protected function createApplication(): void
$app->make(Kernel::class)->bootstrap();
}

protected function isReadyToSaveProductionData(): bool
protected function mergeTempDocumentation(string $token): void
{
if ($token = ParallelTesting::token()) {
unlink(config('auto-doc.tmp_dir') . "/worker_{$token}_in_progress.flag");
$swaggerService = app(SwaggerService::class);

if (empty(glob(config('auto-doc.tmp_dir') . '/worker_*_in_progress.flag'))) {
$this->mergeTempDocumentation();
$handle = fopen(config('auto-doc.drivers.local.production_path'), 'c+');

return true;
}
} else {
return true;
while (!flock($handle, LOCK_EX | LOCK_NB)) {
usleep(10);
}

return false;
}
$tmpDoc = file_get_contents(config('auto-doc.tmp_dir') . "/temp_documentation_{$token}.json");

protected function mergeTempDocumentation(): void
{
$swaggerService = app(SwaggerService::class);

$resultDoc = [];
$prodDoc = json_decode(stream_get_contents($handle), true);

$tmpPaths = glob(config('auto-doc.tmp_dir') . '/temp_documentation_*.json');
$swaggerService->mergeOpenAPIDocs($prodDoc, json_decode($tmpDoc, true));

foreach ($tmpPaths as $tmpDocFilePath) {
$tmpDoc = json_decode(file_get_contents($tmpDocFilePath), true);

if (empty($resultDoc)) {
$resultDoc = $tmpDoc;
} else {
$swaggerService->mergeOpenAPIDocs($resultDoc, $tmpDoc);
}
}
$swaggerService->saveProductionData();

$swaggerService->saveTmpData($resultDoc);
fclose($handle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ final class ApplicationStartedSubscriber implements StartedSubscriber
{
public function notify(Started $event): void
{
$this->createApplication();

if ($token = ParallelTesting::token()) {
touch(config('auto-doc.tmp_dir') . "/worker_{$token}_in_progress.flag");
}
// $this->createApplication();
//
// if ($token = ParallelTesting::token()) {
// touch(config('auto-doc.tmp_dir') . "/worker_{$token}_in_progress.flag");
// }
}

protected function createApplication(): void
Expand Down

0 comments on commit e99c3a3

Please sign in to comment.