IcingaConfigDiff: replace duplicate code

This commit is contained in:
Thomas Gelf 2021-10-05 19:07:06 +02:00
parent 5aba966a4b
commit c2d7b235a5
3 changed files with 64 additions and 77 deletions

View File

@ -6,6 +6,7 @@ use gipfl\Diff\HtmlRenderer\SideBySideDiff;
use gipfl\Diff\PhpDiff; use gipfl\Diff\PhpDiff;
use gipfl\IcingaWeb2\Link; use gipfl\IcingaWeb2\Link;
use gipfl\Web\Widget\Hint; use gipfl\Web\Widget\Hint;
use Icinga\Module\Director\Web\Widget\IcingaConfigDiff;
use Icinga\Module\Director\Web\Widget\UnorderedList; use Icinga\Module\Director\Web\Widget\UnorderedList;
use Icinga\Module\Director\Db\Cache\PrefetchCache; use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface; use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
@ -309,7 +310,7 @@ class SyncruleController extends ActionController
$cfgOld = new IcingaConfig($this->db()); $cfgOld = new IcingaConfig($this->db());
$oldObject->renderToConfig($cfgOld); $oldObject->renderToConfig($cfgOld);
$object->renderToConfig($cfgNew); $object->renderToConfig($cfgNew);
foreach ($this->getConfigDiffs($cfgOld, $cfgNew) as $file => $diff) { foreach (IcingaConfigDiff::getDiffs($cfgOld, $cfgNew) as $file => $diff) {
$names[$name . '___PRETITLE___' . $file] = Html::tag('h3', $file); $names[$name . '___PRETITLE___' . $file] = Html::tag('h3', $file);
$names[$name . '___PREVIEW___' . $file] = $diff; $names[$name . '___PREVIEW___' . $file] = $diff;
} }
@ -334,7 +335,7 @@ class SyncruleController extends ActionController
$cfgOld = new IcingaConfig($this->db()); $cfgOld = new IcingaConfig($this->db());
$oldObject->renderToConfig($cfgOld); $oldObject->renderToConfig($cfgOld);
$object->renderToConfig($cfgNew); $object->renderToConfig($cfgNew);
foreach ($this->getConfigDiffs($cfgOld, $cfgNew) as $file => $diff) { foreach (IcingaConfigDiff::getDiffs($cfgOld, $cfgNew) as $file => $diff) {
$names[$name . '___PRETITLE___' . $file] = Html::tag('h3', $file); $names[$name . '___PRETITLE___' . $file] = Html::tag('h3', $file);
$names[$name . '___PREVIEW___' . $file] = $diff; $names[$name . '___PREVIEW___' . $file] = $diff;
} }
@ -364,43 +365,6 @@ class SyncruleController extends ActionController
return $list; return $list;
} }
/**
* Stolen from elsewhere, should be de-duplicated
*
* @param IcingaConfig $oldConfig
* @param IcingaConfig $newConfig
* @return ValidHtml[]
*/
protected function getConfigDiffs(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;
}
protected function listModifiedProperties($properties) protected function listModifiedProperties($properties)
{ {
$list = new UnorderedList(); $list = new UnorderedList();

View File

@ -127,12 +127,12 @@ class ActivityLogInfo extends HtmlDocument
$this->add($this->getInfoTable()); $this->add($this->getInfoTable());
if ($tabName === 'old') { if ($tabName === 'old') {
// $title = sprintf('%s former config', $this->entry->object_name); // $title = sprintf('%s former config', $this->entry->object_name);
$diffs = $this->getConfigDiffs($this->oldConfig(), $this->emptyConfig()); $diffs = IcingaConfigDiff::getDiffs($this->oldConfig(), $this->emptyConfig());
} elseif ($tabName === 'new') { } elseif ($tabName === 'new') {
// $title = sprintf('%s new config', $this->entry->object_name); // $title = sprintf('%s new config', $this->entry->object_name);
$diffs = $this->getConfigDiffs($this->emptyConfig(), $this->newConfig()); $diffs = IcingaConfigDiff::getDiffs($this->emptyConfig(), $this->newConfig());
} else { } else {
$diffs = $this->getConfigDiffs($this->oldConfig(), $this->newConfig()); $diffs = IcingaConfigDiff::getDiffs($this->oldConfig(), $this->newConfig());
} }
$this->addDiffs($diffs); $this->addDiffs($diffs);
@ -399,41 +399,6 @@ class ActivityLogInfo extends HtmlDocument
return $tabs; return $tabs;
} }
/**
* @param IcingaConfig $oldConfig
* @param IcingaConfig $newConfig
* @return ValidHtml[]
*/
protected function getConfigDiffs(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;
}
/** /**
* @return IcingaObject * @return IcingaObject
* @throws \Icinga\Exception\IcingaException * @throws \Icinga\Exception\IcingaException

View File

@ -0,0 +1,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;
}
}