|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Fast Forward Development Tools for PHP projects. |
| 7 | + * |
| 8 | + * This file is part of fast-forward/dev-tools project. |
| 9 | + * |
| 10 | + * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | + * @license https://opensource.org/licenses/MIT MIT License |
| 12 | + * |
| 13 | + * @see https://github.com/php-fast-forward/ |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | + * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | + */ |
| 19 | + |
| 20 | +namespace FastForward\DevTools\Resource; |
| 21 | + |
| 22 | +use FastForward\DevTools\Filesystem\FilesystemInterface; |
| 23 | +use SebastianBergmann\Diff\Differ; |
| 24 | +use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; |
| 25 | +use Throwable; |
| 26 | + |
| 27 | +use function sprintf; |
| 28 | +use function str_contains; |
| 29 | +use function trim; |
| 30 | + |
| 31 | +/** |
| 32 | + * Renders deterministic overwrite summaries and unified diffs for copied files. |
| 33 | + */ |
| 34 | +final readonly class OverwriteDiffRenderer |
| 35 | +{ |
| 36 | + /** |
| 37 | + * Creates a new overwrite diff renderer. |
| 38 | + * |
| 39 | + * @param FilesystemInterface $filesystem the filesystem used to read compared file contents |
| 40 | + */ |
| 41 | + public function __construct(private FilesystemInterface $filesystem) |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Compares a source file against the target file that would be overwritten. |
| 47 | + * |
| 48 | + * @param string $sourcePath the source file path that would replace the target |
| 49 | + * @param string $targetPath the existing target file path |
| 50 | + * |
| 51 | + * @return OverwriteDiffResult the rendered comparison result |
| 52 | + */ |
| 53 | + public function render(string $sourcePath, string $targetPath): OverwriteDiffResult |
| 54 | + { |
| 55 | + try { |
| 56 | + $sourceContent = $this->filesystem->readFile($sourcePath); |
| 57 | + $targetContent = $this->filesystem->readFile($targetPath); |
| 58 | + } catch (Throwable) { |
| 59 | + return new OverwriteDiffResult( |
| 60 | + OverwriteDiffResult::STATUS_UNREADABLE, |
| 61 | + sprintf( |
| 62 | + 'Target %s will be overwritten from %s, but the existing or source content could not be read.', |
| 63 | + $targetPath, |
| 64 | + $sourcePath, |
| 65 | + ), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + if ($sourceContent === $targetContent) { |
| 70 | + return new OverwriteDiffResult( |
| 71 | + OverwriteDiffResult::STATUS_UNCHANGED, |
| 72 | + sprintf('Target %s already matches source %s; overwrite skipped.', $targetPath, $sourcePath), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + if ($this->isBinary($sourceContent) || $this->isBinary($targetContent)) { |
| 77 | + return new OverwriteDiffResult( |
| 78 | + OverwriteDiffResult::STATUS_BINARY, |
| 79 | + sprintf( |
| 80 | + 'Target %s will be overwritten from %s, but a text diff is unavailable for binary content.', |
| 81 | + $targetPath, |
| 82 | + $sourcePath, |
| 83 | + ), |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + $header = sprintf("--- Current: %s\n+++ Source: %s\n", $targetPath, $sourcePath); |
| 88 | + $differ = new Differ(new UnifiedDiffOutputBuilder($header)); |
| 89 | + |
| 90 | + return new OverwriteDiffResult( |
| 91 | + OverwriteDiffResult::STATUS_CHANGED, |
| 92 | + sprintf('Overwriting resource %s from %s.', $targetPath, $sourcePath), |
| 93 | + trim($differ->diff($targetContent, $sourceContent)), |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Reports whether the given content should be treated as binary. |
| 99 | + * |
| 100 | + * @param string $content the content to inspect |
| 101 | + * |
| 102 | + * @return bool true when the content should not receive a text diff |
| 103 | + */ |
| 104 | + private function isBinary(string $content): bool |
| 105 | + { |
| 106 | + return str_contains($content, "\0"); |
| 107 | + } |
| 108 | +} |
0 commit comments