Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of sebastian/diff.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianBergmann\Diff\Output;
11
 
12
use function fclose;
13
use function fopen;
14
use function fwrite;
15
use function stream_get_contents;
16
use function substr;
17
use SebastianBergmann\Diff\Differ;
18
 
19
/**
20
 * Builds a diff string representation in a loose unified diff format
21
 * listing only changes lines. Does not include line numbers.
22
 */
23
final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $header;
29
 
30
    public function __construct(string $header = "--- Original\n+++ New\n")
31
    {
32
        $this->header = $header;
33
    }
34
 
35
    public function getDiff(array $diff): string
36
    {
37
        $buffer = fopen('php://memory', 'r+b');
38
 
39
        if ('' !== $this->header) {
40
            fwrite($buffer, $this->header);
41
 
42
            if ("\n" !== substr($this->header, -1, 1)) {
43
                fwrite($buffer, "\n");
44
            }
45
        }
46
 
47
        foreach ($diff as $diffEntry) {
48
            if ($diffEntry[1] === Differ::ADDED) {
49
                fwrite($buffer, '+' . $diffEntry[0]);
50
            } elseif ($diffEntry[1] === Differ::REMOVED) {
51
                fwrite($buffer, '-' . $diffEntry[0]);
52
            } elseif ($diffEntry[1] === Differ::DIFF_LINE_END_WARNING) {
53
                fwrite($buffer, ' ' . $diffEntry[0]);
54
 
55
                continue; // Warnings should not be tested for line break, it will always be there
56
            } else { /* Not changed (old) 0 */
57
                continue; // we didn't write the non changs line, so do not add a line break either
58
            }
59
 
60
            $lc = substr($diffEntry[0], -1);
61
 
62
            if ($lc !== "\n" && $lc !== "\r") {
63
                fwrite($buffer, "\n"); // \No newline at end of file
64
            }
65
        }
66
 
67
        $diff = stream_get_contents($buffer, -1, 0);
68
        fclose($buffer);
69
 
70
        return $diff;
71
    }
72
}