summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Widget/IcingaConfigDiff.php
blob: 800f1d93ac5aa8f172613bba2f9919600de1b462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

namespace Icinga\Module\Director\Web\Widget;

use gipfl\Diff\HtmlRenderer\SideBySideDiff;
use gipfl\Diff\PhpDiff;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use ipl\Html\Html;
use ipl\Html\HtmlDocument;
use ipl\Html\ValidHtml;

class IcingaConfigDiff extends HtmlDocument
{
    public function __construct(IcingaConfig $left, IcingaConfig $right)
    {
        foreach (static::getDiffs($left, $right) as $filename => $diff) {
            $this->add([
                Html::tag('h3', $filename),
                $diff
            ]);
        }
    }

    /**
     * @param IcingaConfig $oldConfig
     * @param IcingaConfig $newConfig
     * @return ValidHtml[]
     */
    public static function getDiffs(IcingaConfig $oldConfig, IcingaConfig $newConfig)
    {
        $oldFileNames = $oldConfig->getFileNames();
        $newFileNames = $newConfig->getFileNames();

        $fileNames = array_merge($oldFileNames, $newFileNames);

        $diffs = [];
        foreach ($fileNames as $filename) {
            if (in_array($filename, $oldFileNames)) {
                $left = $oldConfig->getFile($filename)->getContent();
            } else {
                $left = '';
            }

            if (in_array($filename, $newFileNames)) {
                $right = $newConfig->getFile($filename)->getContent();
            } else {
                $right = '';
            }
            if ($left === $right) {
                continue;
            }

            $diffs[$filename] = new SideBySideDiff(new PhpDiff($left, $right));
        }

        return $diffs;
    }
}