Skip to content

Commit

Permalink
Merge pull request #30 from ernix/cleanup-dirs
Browse files Browse the repository at this point in the history
Fix cleanup code in `src/Workspaces/Concerns/CompilesWorkspace.php`
  • Loading branch information
JohnathonKoster authored Mar 4, 2024
2 parents fc42f29 + ef947cd commit 4cb4888
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Workspaces/Concerns/CompilesWorkspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ trait CompilesWorkspace
*/
protected array $compiledFiles = [];

/**
* A list of all directories created by the workspace compiler.
*
* @var string[]
*/
protected array $createdDirs = [];

/**
* The path to the last temporary directory used.
*
Expand Down Expand Up @@ -81,6 +88,10 @@ public function removeCompiledFiles(): Workspace
@unlink($compiledPath);
}

foreach ($this->createdDirs as $dir) {
@rmdir($dir);
}

return $this;
}

Expand Down Expand Up @@ -137,7 +148,6 @@ public function compile(string $outputDirectory): void
{
$outputDirectory = Paths::normalizePathWithTrailingSlash($outputDirectory);
$this->lastTempDirectory = $outputDirectory;
$createdDirs = [];

try {
/** @var Document $doc */
Expand All @@ -153,21 +163,18 @@ public function compile(string $outputDirectory): void
};
$result = $doc->compile($options);

$dir = Paths::normalizePathWithTrailingSlash(Str::afterLast(Paths::normalizePath($compilePath), '/'));
$createdDirs[] = $dir;
$dir = Paths::normalizePathWithTrailingSlash(dirname(Paths::normalizePath($compilePath)));

if (! file_exists($dir)) {
@mkdir($dir, 0755, true);
$this->createdDirs[] = $dir;
}

file_put_contents($compilePath, $result);
$this->compiledFiles[$compilePath] = $doc->getFilePath();
$this->compiledDocuments[$compilePath] = $doc;
}
} catch (Exception $e) {
foreach ($createdDirs as $dir) {
@rmdir($dir);
}
$this->removeCompiledFiles();

throw $e;
Expand Down

0 comments on commit 4cb4888

Please sign in to comment.