Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce render method on Files #22

Merged
merged 1 commit into from
Aug 28, 2024
Merged
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
14 changes: 14 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace WyriHaximus\SubSplitTools;

final readonly class File
{
public function __construct(
public string $fileName,
public string $contents,
) {
}
}
42 changes: 31 additions & 11 deletions src/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function Safe\file_get_contents;
use function Safe\file_put_contents;
use function Safe\mkdir;
use function str_replace;
use function strlen;
use function substr;
use function WyriHaximus\Twig\render;
Expand All @@ -28,11 +29,33 @@
* @param array<mixed> $templateVariables
*/
public static function setUp(string $templates, string $destination, array $templateVariables): void
{
foreach (self::render($templates, $destination, $templateVariables) as $file) {
try {
/** @phpstan-ignore-next-line */
@mkdir(dirname($file->fileName), 0744, true);

Check warning on line 36 in src/Files.php

View workflow job for this annotation

GitHub Actions / qa (8.2, locked, mutation-testing)

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ foreach (self::render($templates, $destination, $templateVariables) as $file) { try { /** @phpstan-ignore-next-line */ - @mkdir(dirname($file->fileName), 0744, true); + @mkdir(dirname($file->fileName), 483, true); } catch (FilesystemException) { // void @IgnoreException }

Check warning on line 36 in src/Files.php

View workflow job for this annotation

GitHub Actions / qa (8.2, locked, mutation-testing)

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ foreach (self::render($templates, $destination, $templateVariables) as $file) { try { /** @phpstan-ignore-next-line */ - @mkdir(dirname($file->fileName), 0744, true); + @mkdir(dirname($file->fileName), 485, true); } catch (FilesystemException) { // void @IgnoreException }

Check warning on line 36 in src/Files.php

View workflow job for this annotation

GitHub Actions / qa (8.3, locked, mutation-testing)

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ foreach (self::render($templates, $destination, $templateVariables) as $file) { try { /** @phpstan-ignore-next-line */ - @mkdir(dirname($file->fileName), 0744, true); + @mkdir(dirname($file->fileName), 483, true); } catch (FilesystemException) { // void @IgnoreException }

Check warning on line 36 in src/Files.php

View workflow job for this annotation

GitHub Actions / qa (8.3, locked, mutation-testing)

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ foreach (self::render($templates, $destination, $templateVariables) as $file) { try { /** @phpstan-ignore-next-line */ - @mkdir(dirname($file->fileName), 0744, true); + @mkdir(dirname($file->fileName), 485, true); } catch (FilesystemException) { // void @IgnoreException }
} catch (FilesystemException) {
// void @ignoreException
}

file_put_contents(
$file->fileName,
$file->contents,
);
}
}

/**
* @param array<mixed> $templateVariables
*
* @return iterable<File>
*/
public static function render(string $templates, string $destination, array $templateVariables): iterable
{
foreach (
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
rtrim($templates, '/'),

Check warning on line 58 in src/Files.php

View workflow job for this annotation

GitHub Actions / qa (8.2, locked, mutation-testing)

Escaped Mutant for Mutator "UnwrapRtrim": --- Original +++ New @@ @@ */ public static function render(string $templates, string $destination, array $templateVariables) : iterable { - foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(rtrim($templates, '/'), FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_PATHNAME)) as $fileName) { + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($templates, FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_PATHNAME)) as $fileName) { if (!is_string($fileName)) { continue; }

Check warning on line 58 in src/Files.php

View workflow job for this annotation

GitHub Actions / qa (8.3, locked, mutation-testing)

Escaped Mutant for Mutator "UnwrapRtrim": --- Original +++ New @@ @@ */ public static function render(string $templates, string $destination, array $templateVariables) : iterable { - foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(rtrim($templates, '/'), FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_PATHNAME)) as $fileName) { + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($templates, FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_PATHNAME)) as $fileName) { if (!is_string($fileName)) { continue; }
FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_PATHNAME,
),
) as $fileName
Expand All @@ -45,20 +68,17 @@
continue;
}

$newFilename = render(
$destination . DIRECTORY_SEPARATOR . substr($fileName, strlen($templates)),
$renderedFileName = render(
rtrim($destination, '/') . DIRECTORY_SEPARATOR . substr($fileName, strlen($templates)),

Check warning on line 72 in src/Files.php

View workflow job for this annotation

GitHub Actions / qa (8.2, locked, mutation-testing)

Escaped Mutant for Mutator "UnwrapRtrim": --- Original +++ New @@ @@ if (!is_file($fileName)) { continue; } - $renderedFileName = render(rtrim($destination, '/') . DIRECTORY_SEPARATOR . substr($fileName, strlen($templates)), $templateVariables); + $renderedFileName = render($destination . DIRECTORY_SEPARATOR . substr($fileName, strlen($templates)), $templateVariables); do { $previousRenderedFileName = $renderedFileName; $renderedFileName = str_replace(['//', '\\\\'], DIRECTORY_SEPARATOR, $renderedFileName);

Check warning on line 72 in src/Files.php

View workflow job for this annotation

GitHub Actions / qa (8.3, locked, mutation-testing)

Escaped Mutant for Mutator "UnwrapRtrim": --- Original +++ New @@ @@ if (!is_file($fileName)) { continue; } - $renderedFileName = render(rtrim($destination, '/') . DIRECTORY_SEPARATOR . substr($fileName, strlen($templates)), $templateVariables); + $renderedFileName = render($destination . DIRECTORY_SEPARATOR . substr($fileName, strlen($templates)), $templateVariables); do { $previousRenderedFileName = $renderedFileName; $renderedFileName = str_replace(['//', '\\\\'], DIRECTORY_SEPARATOR, $renderedFileName);
$templateVariables,
);
do {
$previousRenderedFileName = $renderedFileName;
$renderedFileName = str_replace(['//', '\\\\'], DIRECTORY_SEPARATOR, $renderedFileName);
} while ($previousRenderedFileName !== $renderedFileName);

try {
/** @phpstan-ignore-next-line */
@mkdir(dirname($newFilename), 0744, true);
} catch (FilesystemException) {
// void @ignoreException
}

file_put_contents(
$newFilename,
yield new File(
$renderedFileName,
render(
file_get_contents($fileName),
$templateVariables,
Expand Down
30 changes: 28 additions & 2 deletions tests/FilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use WyriHaximus\SubSplitTools\Files;
use WyriHaximus\TestUtilities\TestCase;

use function time;

use const DIRECTORY_SEPARATOR;

final class FilesTest extends TestCase
Expand All @@ -17,8 +19,8 @@ final class FilesTest extends TestCase
public function allFilesGotMovedOver(): void
{
Files::setUp(
__DIR__ . DIRECTORY_SEPARATOR . 'templates',
$this->getTmpDir(),
__DIR__ . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR,
$this->getTmpDir() . DIRECTORY_SEPARATOR,
[],
);

Expand All @@ -27,4 +29,28 @@ public function allFilesGotMovedOver(): void
self::assertFileExists($this->getTmpDir() . 'a/b/second.level.txt');
self::assertFileExists($this->getTmpDir() . 'a/b/c/third.level.txt');
}

/**
* @test
*/
public function renderTemplatedPath(): void
{
$time = time();
$files = [];

foreach (
Files::render(
__DIR__ . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR,
$this->getTmpDir() . DIRECTORY_SEPARATOR . '//{{ time }}\\\\' . DIRECTORY_SEPARATOR,
['time' => $time],
) as $file
) {
$files[$file->fileName] = $file;
}

self::assertArrayHasKey($this->getTmpDir() . $time . '/root.txt', $files);
self::assertArrayHasKey($this->getTmpDir() . $time . '/a/first.level.txt', $files);
self::assertArrayHasKey($this->getTmpDir() . $time . '/a/b/second.level.txt', $files);
self::assertArrayHasKey($this->getTmpDir() . $time . '/a/b/c/third.level.txt', $files);
}
}
Loading