diff --git a/application/controllers/SyncruleController.php b/application/controllers/SyncruleController.php index a907aaa9..727e5cbf 100644 --- a/application/controllers/SyncruleController.php +++ b/application/controllers/SyncruleController.php @@ -6,6 +6,7 @@ use gipfl\Diff\HtmlRenderer\SideBySideDiff; use gipfl\Diff\PhpDiff; use gipfl\IcingaWeb2\Link; use gipfl\Web\Widget\Hint; +use Icinga\Module\Director\Web\Widget\IcingaConfigDiff; use Icinga\Module\Director\Web\Widget\UnorderedList; use Icinga\Module\Director\Db\Cache\PrefetchCache; use Icinga\Module\Director\DirectorObject\Automation\ExportInterface; @@ -309,7 +310,7 @@ class SyncruleController extends ActionController $cfgOld = new IcingaConfig($this->db()); $oldObject->renderToConfig($cfgOld); $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 . '___PREVIEW___' . $file] = $diff; } @@ -334,7 +335,7 @@ class SyncruleController extends ActionController $cfgOld = new IcingaConfig($this->db()); $oldObject->renderToConfig($cfgOld); $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 . '___PREVIEW___' . $file] = $diff; } @@ -364,43 +365,6 @@ class SyncruleController extends ActionController 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) { $list = new UnorderedList(); diff --git a/library/Director/Web/Widget/ActivityLogInfo.php b/library/Director/Web/Widget/ActivityLogInfo.php index f2f4c889..f1a1956e 100644 --- a/library/Director/Web/Widget/ActivityLogInfo.php +++ b/library/Director/Web/Widget/ActivityLogInfo.php @@ -127,12 +127,12 @@ class ActivityLogInfo extends HtmlDocument $this->add($this->getInfoTable()); if ($tabName === 'old') { // $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') { // $title = sprintf('%s new config', $this->entry->object_name); - $diffs = $this->getConfigDiffs($this->emptyConfig(), $this->newConfig()); + $diffs = IcingaConfigDiff::getDiffs($this->emptyConfig(), $this->newConfig()); } else { - $diffs = $this->getConfigDiffs($this->oldConfig(), $this->newConfig()); + $diffs = IcingaConfigDiff::getDiffs($this->oldConfig(), $this->newConfig()); } $this->addDiffs($diffs); @@ -399,41 +399,6 @@ class ActivityLogInfo extends HtmlDocument 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 * @throws \Icinga\Exception\IcingaException diff --git a/library/Director/Web/Widget/IcingaConfigDiff.php b/library/Director/Web/Widget/IcingaConfigDiff.php new file mode 100644 index 00000000..800f1d93 --- /dev/null +++ b/library/Director/Web/Widget/IcingaConfigDiff.php @@ -0,0 +1,58 @@ + $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; + } +}