From 652e29aef45bd6a7190a72e133e1745ec80fe853 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 13 Jun 2026 11:52:28 +0300 Subject: [PATCH] Add throwing file writer for build output --- src/Build/AuthorPageWriter.php | 4 +-- src/Build/BuildCache.php | 2 +- src/Build/BuildManifest.php | 13 +++----- src/Build/CollectionListingWriter.php | 2 +- src/Build/DateArchiveWriter.php | 6 ++-- src/Build/FileWriter.php | 17 +++++++++++ src/Build/NotFoundPageWriter.php | 2 +- src/Build/ParallelEntryWriter.php | 4 +-- src/Build/ParallelTaskRunner.php | 2 +- src/Build/RedirectPageWriter.php | 2 +- src/Build/SearchIndexGenerator.php | 2 +- src/Build/TaxonomyPageWriter.php | 4 +-- src/Console/BuildCommand.php | 13 ++++---- tests/Unit/Build/FileWriterTest.php | 44 +++++++++++++++++++++++++++ 14 files changed, 88 insertions(+), 29 deletions(-) create mode 100644 src/Build/FileWriter.php create mode 100644 tests/Unit/Build/FileWriterTest.php diff --git a/src/Build/AuthorPageWriter.php b/src/Build/AuthorPageWriter.php index fa9645b..9a0480d 100644 --- a/src/Build/AuthorPageWriter.php +++ b/src/Build/AuthorPageWriter.php @@ -150,7 +150,7 @@ private function writeIndexPage( throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); } - file_put_contents($dir . '/index.html', $html); + FileWriter::write($dir . '/index.html', $html); } } @@ -218,7 +218,7 @@ private function writeAuthorPage( throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); } - file_put_contents($dir . '/index.html', $html); + FileWriter::write($dir . '/index.html', $html); } } } diff --git a/src/Build/BuildCache.php b/src/Build/BuildCache.php index 32bb2bf..381a5d7 100644 --- a/src/Build/BuildCache.php +++ b/src/Build/BuildCache.php @@ -52,7 +52,7 @@ public function get(string $sourceFilePath, string $context = ''): ?string public function set(string $sourceFilePath, string $html, string $context = ''): void { $key = $this->buildKey($sourceFilePath, $context); - file_put_contents($this->cacheDir . '/' . $key, $html); + FileWriter::write($this->cacheDir . '/' . $key, $html); } public function clear(): void diff --git a/src/Build/BuildManifest.php b/src/Build/BuildManifest.php index 3622a67..d7cde44 100644 --- a/src/Build/BuildManifest.php +++ b/src/Build/BuildManifest.php @@ -62,14 +62,11 @@ public function save(): void throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); } - file_put_contents( - $this->manifestPath, - json_encode([ - 'entries' => $this->entries, - 'configFiles' => $this->configFiles, - 'trackedDirectories' => $this->trackedDirectories, - ], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), - ); + FileWriter::write($this->manifestPath, json_encode([ + 'entries' => $this->entries, + 'configFiles' => $this->configFiles, + 'trackedDirectories' => $this->trackedDirectories, + ], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); } public function isChanged(string $sourceFile): bool diff --git a/src/Build/CollectionListingWriter.php b/src/Build/CollectionListingWriter.php index 0a04610..ca1223d 100644 --- a/src/Build/CollectionListingWriter.php +++ b/src/Build/CollectionListingWriter.php @@ -90,7 +90,7 @@ public function write( throw new RuntimeException(sprintf('Directory "%s" was not created', $task['dir'])); } - file_put_contents($task['dir'] . '/index.html', $html); + FileWriter::write($task['dir'] . '/index.html', $html); } return 1; diff --git a/src/Build/DateArchiveWriter.php b/src/Build/DateArchiveWriter.php index 0ee0623..c43c08b 100644 --- a/src/Build/DateArchiveWriter.php +++ b/src/Build/DateArchiveWriter.php @@ -160,7 +160,7 @@ private function writeArchiveIndexPage( throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); } - file_put_contents($dir . '/index.html', $html); + FileWriter::write($dir . '/index.html', $html); } } @@ -216,7 +216,7 @@ private function writeYearlyPage( throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); } - file_put_contents($dir . '/index.html', $html); + FileWriter::write($dir . '/index.html', $html); } } @@ -271,7 +271,7 @@ private function writeMonthlyPage( throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); } - file_put_contents($dir . '/index.html', $html); + FileWriter::write($dir . '/index.html', $html); } } } diff --git a/src/Build/FileWriter.php b/src/Build/FileWriter.php new file mode 100644 index 0000000..5c7a05f --- /dev/null +++ b/src/Build/FileWriter.php @@ -0,0 +1,17 @@ +render($siteConfig, $task['entry'], $task['permalink'], $navigation, $crossRefResolver, $task['navigationPager'] ?? null); if (!$noWrite) { - file_put_contents($task['filePath'], $html); + FileWriter::write($task['filePath'], $html); } } } @@ -115,7 +115,7 @@ private function writeParallel(SiteConfig $siteConfig, array $tasks, string $con foreach ($chunk as $task) { $html = $renderer->render($siteConfig, $task['entry'], $task['permalink'], $navigation, $crossRefResolver, $task['navigationPager'] ?? null); if (!$noWrite) { - file_put_contents($task['filePath'], $html); + FileWriter::write($task['filePath'], $html); } } diff --git a/src/Build/ParallelTaskRunner.php b/src/Build/ParallelTaskRunner.php index 6997f36..6a355b2 100644 --- a/src/Build/ParallelTaskRunner.php +++ b/src/Build/ParallelTaskRunner.php @@ -62,7 +62,7 @@ public function run(array $tasks, int $workerCount, callable $taskRunner, int $m if ($pid === 0) { $count = $this->runSequential($chunk, $taskRunner); - file_put_contents($resultFile, (string) $count); + FileWriter::write($resultFile, (string) $count); exit(0); } diff --git a/src/Build/RedirectPageWriter.php b/src/Build/RedirectPageWriter.php index f0a954c..156e5dc 100644 --- a/src/Build/RedirectPageWriter.php +++ b/src/Build/RedirectPageWriter.php @@ -71,6 +71,6 @@ public function write( throw new RuntimeException(sprintf('Directory "%s" was not created', $dirPath)); } - file_put_contents($filePath, $html); + FileWriter::write($filePath, $html); } } diff --git a/src/Build/SearchIndexGenerator.php b/src/Build/SearchIndexGenerator.php index 1522779..1d33a7a 100644 --- a/src/Build/SearchIndexGenerator.php +++ b/src/Build/SearchIndexGenerator.php @@ -64,7 +64,7 @@ public function generate( $json = json_encode($items, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if (!$noWrite) { - file_put_contents($outputDir . '/search-index.json', $json); + FileWriter::write($outputDir . '/search-index.json', $json); } } } diff --git a/src/Build/TaxonomyPageWriter.php b/src/Build/TaxonomyPageWriter.php index 55a82e3..4d4bf25 100644 --- a/src/Build/TaxonomyPageWriter.php +++ b/src/Build/TaxonomyPageWriter.php @@ -85,7 +85,7 @@ private function writeIndexPage( throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); } - file_put_contents($dir . '/index.html', $html); + FileWriter::write($dir . '/index.html', $html); } } @@ -143,7 +143,7 @@ private function writeTermPage( throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); } - file_put_contents($dir . '/index.html', $html); + FileWriter::write($dir . '/index.html', $html); } } } diff --git a/src/Console/BuildCommand.php b/src/Console/BuildCommand.php index 4e1e959..4941a62 100644 --- a/src/Console/BuildCommand.php +++ b/src/Console/BuildCommand.php @@ -17,6 +17,7 @@ use YiiPress\Build\NavigationPager; use YiiPress\Build\RedirectPageWriter; use YiiPress\Build\RobotsTxtGenerator; +use YiiPress\Build\FileWriter; use YiiPress\Build\SearchIndexGenerator; use YiiPress\Build\ThemeAssetCopier; use YiiPress\Build\FeedGenerator; @@ -772,13 +773,13 @@ function (array $feedTask) use ($siteConfig, $outputDir, $authors, $noWrite): in $profile->switchTo('write support files'); $robotsGenerator = new RobotsTxtGenerator(); - $robots = $robotsGenerator->generate($siteConfig); - if ($robots !== '') { - if (!$noWrite) { - file_put_contents($outputDir . '/robots.txt', $robots); + $robots = $robotsGenerator->generate($siteConfig); + if ($robots !== '') { + if (!$noWrite) { + FileWriter::write($outputDir . '/robots.txt', $robots); + } + $output->writeln(' robots.txt generated.'); } - $output->writeln(' robots.txt generated.'); - } $notFoundWriter = new NotFoundPageWriter($this->templateResolver, $assetManifest); $notFoundWriter->write($siteConfig, $outputDir, $navigation, $noWrite); diff --git a/tests/Unit/Build/FileWriterTest.php b/tests/Unit/Build/FileWriterTest.php new file mode 100644 index 0000000..f3bf121 --- /dev/null +++ b/tests/Unit/Build/FileWriterTest.php @@ -0,0 +1,44 @@ +expectException(RuntimeException::class); + $this->expectExceptionMessage('Unable to write file'); + + FileWriter::write($directory, 'contents'); + } finally { + rmdir($directory); + } + } +}