Skip to content

Commit b0883b4

Browse files
committed
Improve error handling of file-system operations
1 parent 6f189fc commit b0883b4

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/Container/Builder/FileSystemContainerBuilder.php

+23-7
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ public function buildPreloadFile(): array
6060

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

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

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

8386
foreach ($compiledContainerFiles["definitions"] as $filename => $content) {
84-
file_put_contents($definitionDirectory . DIRECTORY_SEPARATOR . $filename, $content);
87+
$file = $definitionDirectory . DIRECTORY_SEPARATOR . $filename;
88+
$result = file_put_contents($file, $content);
89+
if ($result === false) {
90+
throw new ContainerException("File '$$file' cannot be written");
91+
}
8592
}
8693
}
8794

88-
file_put_contents($this->containerPath, $compiledContainerFiles["container"]);
95+
$result = file_put_contents($this->containerPath, $compiledContainerFiles["container"]);
96+
if ($result === false) {
97+
throw new ContainerException("File '$this->containerPath' cannot be written");
98+
}
8999
}
90100

91101
private function deleteDirectory(string $directory): void
@@ -100,9 +110,15 @@ private function deleteDirectory(string $directory): void
100110
foreach ($files as $file) {
101111
assert($file instanceof SplFileInfo);
102112
if ($file->isDir()) {
103-
rmdir($file->getRealPath());
113+
$result = rmdir($file->getRealPath());
114+
if ($result === false) {
115+
throw new ContainerException("Directory '" . $file->getRealPath() . "' cannot be deleted");
116+
}
104117
} else {
105-
unlink($file->getRealPath());
118+
$result = unlink($file->getRealPath());
119+
if ($result === false) {
120+
throw new ContainerException("File '" . $file->getRealPath() . "' cannot be deleted");
121+
}
106122
}
107123
}
108124

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

121137
$result = mkdir($directory);
122138
if ($result === false) {
123-
throw new ContainerException("Directory '$directory' can not be created!");
139+
throw new ContainerException("Directory '$directory' cannot be created");
124140
}
125141
}
126142

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

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

136152
return $basePath . DIRECTORY_SEPARATOR . $relativeDirectory;

0 commit comments

Comments
 (0)