Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Build/AuthorPageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/Build/BuildCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions src/Build/BuildManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Build/CollectionListingWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Build/DateArchiveWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
17 changes: 17 additions & 0 deletions src/Build/FileWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace YiiPress\Build;

use RuntimeException;

final class FileWriter
{
public static function write(string $filePath, string $contents): void
{
if (@file_put_contents($filePath, $contents) === false) {
throw new RuntimeException(sprintf('Unable to write file "%s".', $filePath));
}
}
}
2 changes: 1 addition & 1 deletion src/Build/NotFoundPageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public function write(SiteConfig $siteConfig, string $outputDir, ?Navigation $na
throw new RuntimeException(sprintf('Directory "%s" was not created', $outputDir));
}

file_put_contents($outputDir . '/404.html', $html);
FileWriter::write($outputDir . '/404.html', $html);
}
}
4 changes: 2 additions & 2 deletions src/Build/ParallelEntryWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private function writeEntries(SiteConfig $siteConfig, array $tasks, string $cont
foreach ($tasks 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);
}
}
}
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Build/ParallelTaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines 64 to 66
}

Expand Down
2 changes: 1 addition & 1 deletion src/Build/RedirectPageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Build/SearchIndexGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Comment on lines 66 to 68
}
}
4 changes: 2 additions & 2 deletions src/Build/TaxonomyPageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
13 changes: 7 additions & 6 deletions src/Console/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
}
Comment on lines +776 to 782
$output->writeln(' robots.txt generated.');
}

$notFoundWriter = new NotFoundPageWriter($this->templateResolver, $assetManifest);
$notFoundWriter->write($siteConfig, $outputDir, $navigation, $noWrite);
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Build/FileWriterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace YiiPress\Tests\Unit\Build;

use YiiPress\Build\FileWriter;
use PHPUnit\Framework\TestCase;
use RuntimeException;

use function PHPUnit\Framework\assertSame;

final class FileWriterTest extends TestCase
{
public function testWriteCreatesFileWithContents(): void
{
$file = sys_get_temp_dir() . '/yiipress-file-writer-' . uniqid() . '.txt';

try {
FileWriter::write($file, 'contents');

assertSame('contents', file_get_contents($file));
} finally {
if (is_file($file)) {
unlink($file);
}
}
}

public function testWriteThrowsWhenTargetCannotBeWritten(): void
{
$directory = sys_get_temp_dir() . '/yiipress-file-writer-' . uniqid();
mkdir($directory, 0o755);

try {
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unable to write file');

FileWriter::write($directory, 'contents');
} finally {
rmdir($directory);
}
}
}