Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
vitgrams committed Nov 28, 2024
1 parent a0c818f commit 6e75585
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
19 changes: 6 additions & 13 deletions src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,17 @@ public function acquireLock($handle): void
}
}

public function callWithHandle(string $filePath, string $mode, callable $callback): void
public function callbackWithLock(string $filePath, string $mode, callable $callback): void
{
$resultDocHandle = fopen($this->driver->getTempFilePath(), 'c+');
$handle = fopen($filePath, $mode);

try {
$this->driver->acquireLock($resultDocHandle);
$this->acquireLock($handle);

$resultDocContent = json_decode(stream_get_contents($resultDocHandle), true)
?? $this->generateEmptyData();

$currentDocContent = json_decode(file_get_contents($this->driver->getTempFilePath(ParallelTesting::token())), true);

$this->mergeOpenAPIDocs($resultDocContent, $currentDocContent);

$this->driver->rewriteJsonFile($resultDocHandle, $resultDocContent);
$callback($handle);
} finally {
flock($resultDocHandle, LOCK_UN);
fclose($resultDocHandle);
flock($handle, LOCK_UN);
fclose($handle);
}
}

Expand Down
25 changes: 11 additions & 14 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,22 +1011,19 @@ public function mergeOpenAPIDocs(array &$documentation, array $additionalDocumen

public function mergeTempDocumentation(): void
{
$resultDocHandle = fopen($this->driver->getTempFilePath(), 'c+');
$this->driver->callbackWithLock(
filePath: $this->driver->getTempFilePath(),
mode: 'c+',
callback: function ($resultDocHandle) {
$resultDocContent = json_decode(stream_get_contents($resultDocHandle), true)
?? $this->generateEmptyData();

try {
$this->driver->acquireLock($resultDocHandle);
$currentDocContent = json_decode(file_get_contents($this->driver->getTempFilePath(ParallelTesting::token())), true);

$resultDocContent = json_decode(stream_get_contents($resultDocHandle), true)
?? $this->generateEmptyData();
$this->mergeOpenAPIDocs($resultDocContent, $currentDocContent);

$currentDocContent = json_decode(file_get_contents($this->driver->getTempFilePath(ParallelTesting::token())), true);

$this->mergeOpenAPIDocs($resultDocContent, $currentDocContent);

$this->driver->rewriteJsonFile($resultDocHandle, $resultDocContent);
} finally {
flock($resultDocHandle, LOCK_UN);
fclose($resultDocHandle);
}
$this->driver->rewriteJsonFile($resultDocHandle, $resultDocContent);
},
);
}
}

0 comments on commit 6e75585

Please sign in to comment.