Skip to content

Commit

Permalink
Improve error handling of file-system operations
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Aug 3, 2024
1 parent 6f189fc commit b0883b4
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/Container/Builder/FileSystemContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public function buildPreloadFile(): array

$compiledPreloadFile = $compiler->compile($this->compilerConfig, $classes);

file_put_contents($this->preloadFilePath, $compiledPreloadFile);
$result = file_put_contents($this->preloadFilePath, $compiledPreloadFile);
if ($result === false) {
throw new ContainerException("File '$this->preloadFilePath' cannot be written");
}

return $classes;
}
Expand All @@ -81,11 +84,18 @@ public function buildContainer(array $preloadedClasses): void
$this->createDirectory($definitionDirectory);

foreach ($compiledContainerFiles["definitions"] as $filename => $content) {
file_put_contents($definitionDirectory . DIRECTORY_SEPARATOR . $filename, $content);
$file = $definitionDirectory . DIRECTORY_SEPARATOR . $filename;
$result = file_put_contents($file, $content);
if ($result === false) {
throw new ContainerException("File '$$file' cannot be written");
}
}
}

file_put_contents($this->containerPath, $compiledContainerFiles["container"]);
$result = file_put_contents($this->containerPath, $compiledContainerFiles["container"]);
if ($result === false) {
throw new ContainerException("File '$this->containerPath' cannot be written");
}
}

private function deleteDirectory(string $directory): void
Expand All @@ -100,9 +110,15 @@ private function deleteDirectory(string $directory): void
foreach ($files as $file) {
assert($file instanceof SplFileInfo);
if ($file->isDir()) {
rmdir($file->getRealPath());
$result = rmdir($file->getRealPath());
if ($result === false) {
throw new ContainerException("Directory '" . $file->getRealPath() . "' cannot be deleted");
}
} else {
unlink($file->getRealPath());
$result = unlink($file->getRealPath());
if ($result === false) {
throw new ContainerException("File '" . $file->getRealPath() . "' cannot be deleted");
}
}
}

Expand All @@ -120,7 +136,7 @@ private function createDirectory(string $directory): void

$result = mkdir($directory);
if ($result === false) {
throw new ContainerException("Directory '$directory' can not be created!");
throw new ContainerException("Directory '$directory' cannot be created");
}
}

Expand All @@ -130,7 +146,7 @@ private function getDefinitionDirectory(): string
$relativeDirectory = $this->compilerConfig->getFileBasedDefinitionConfig()->getRelativeDefinitionDirectory();

if ($relativeDirectory === "") {
throw new ContainerException("Relative directory of file-based definitions can not be empty!");
throw new ContainerException("Relative directory of file-based definitions cannot be empty");
}

return $basePath . DIRECTORY_SEPARATOR . $relativeDirectory;
Expand Down

0 comments on commit b0883b4

Please sign in to comment.