From 0b341c6db1021e2a5bac2b32332fb603f39233b8 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 4 Oct 2018 06:46:32 +0200 Subject: [PATCH] ConfigDiff: allow Inline diff, cleanup, css --- library/Director/ConfigDiff.php | 68 +++++++++++++++++---------------- public/css/module.less | 15 ++++++++ 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/library/Director/ConfigDiff.php b/library/Director/ConfigDiff.php index 3f5c1a7a..d439a2da 100644 --- a/library/Director/ConfigDiff.php +++ b/library/Director/ConfigDiff.php @@ -3,11 +3,8 @@ namespace Icinga\Module\Director; use Diff; -use Diff_Renderer_Html_Inline; -use Diff_Renderer_Html_SideBySide; -use Diff_Renderer_Text_Context; -use Diff_Renderer_Text_Unified; use dipl\Html\ValidHtml; +use InvalidArgumentException; class ConfigDiff implements ValidHtml { @@ -16,29 +13,40 @@ class ConfigDiff implements ValidHtml protected $b; protected $diff; - protected $opcodes; + + protected $htmlRenderer = 'SideBySide'; + + protected $knownHtmlRenderers = [ + 'SideBySide', + 'Inline', + ]; + + protected $knownTextRenderers = [ + 'Context', + 'Unified', + ]; protected function __construct($a, $b) { require_once dirname(__DIR__) . '/vendor/php-diff/lib/Diff.php'; if (empty($a)) { - $this->a = array(); + $this->a = []; } else { $this->a = explode("\n", (string) $a); } if (empty($b)) { - $this->b = array(); + $this->b = []; } else { $this->b = explode("\n", (string) $b); } - $options = array( + $options = [ 'context' => 5, // 'ignoreWhitespace' => true, // 'ignoreCase' => true, - ); + ]; $this->diff = new Diff($this->a, $this->b, $options); } @@ -52,35 +60,32 @@ class ConfigDiff implements ValidHtml */ public function renderHtml() { - return $this->renderHtmlSideBySide(); + return $this->diff->Render($this->getHtmlRenderer()); } - public function renderHtmlSideBySide() + public function setHtmlRenderer($name) { - require_once dirname(__DIR__) . '/vendor/php-diff/lib/Diff/Renderer/Html/SideBySide.php'; - $renderer = new Diff_Renderer_Html_SideBySide; - return $this->diff->Render($renderer); + if (in_array($name, $this->knownHtmlRenderers)) { + $this->htmlRenderer = $name; + } else { + throw new InvalidArgumentException("There is no known '$name' renderer"); + } + + return $this; } - public function renderHtmlInline() + protected function getHtmlRenderer() { - require_once dirname(__DIR__) . '/vendor/php-diff/lib/Diff/Renderer/Html/Inline.php'; - $renderer = new Diff_Renderer_Html_Inline; - return $this->diff->Render($renderer); - } + $filename = sprintf( + '%s/vendor/php-diff/lib/Diff/Renderer/Html/%s.php', + dirname(__DIR__), + $this->htmlRenderer + ); + require_once($filename); - public function renderTextContext() - { - require_once dirname(__DIR__) . '/vendor/php-diff/lib/Diff/Renderer/Text/Context.php'; - $renderer = new Diff_Renderer_Text_Context; - return $this->diff->Render($renderer); - } + $class = 'Diff_Renderer_Html_' . $this->htmlRenderer; - public function renderTextUnified() - { - require_once dirname(__DIR__) . '/vendor/php-diff/lib/Diff/Renderer/Text/Context.php'; - $renderer = new Diff_Renderer_Text_Unified; - return $this->diff->Render($renderer); + return new $class; } public function __toString() @@ -90,7 +95,6 @@ class ConfigDiff implements ValidHtml public static function create($a, $b) { - $diff = new static($a, $b); - return $diff; + return new static($a, $b); } } diff --git a/public/css/module.less b/public/css/module.less index f81a682e..cdd216f4 100644 --- a/public/css/module.less +++ b/public/css/module.less @@ -1694,3 +1694,18 @@ div.exception { background: #e99; } /** END of php-diff **/ + +.DifferencesInline { + tr td:last-child { + width: 90%; + } + tr th:first-child { + width: 5%; + } + tr th:nth-child(2) { + width: 5%; + } + ins, del { + text-decoration: none; + } +}