ConfigDiff: allow Inline diff, cleanup, css

This commit is contained in:
Thomas Gelf 2018-10-04 06:46:32 +02:00
parent a9b2910657
commit 0b341c6db1
2 changed files with 51 additions and 32 deletions

View File

@ -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");
}
public function renderHtmlInline()
{
require_once dirname(__DIR__) . '/vendor/php-diff/lib/Diff/Renderer/Html/Inline.php';
$renderer = new Diff_Renderer_Html_Inline;
return $this->diff->Render($renderer);
return $this;
}
public function renderTextContext()
protected function getHtmlRenderer()
{
require_once dirname(__DIR__) . '/vendor/php-diff/lib/Diff/Renderer/Text/Context.php';
$renderer = new Diff_Renderer_Text_Context;
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 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);
$class = 'Diff_Renderer_Html_' . $this->htmlRenderer;
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);
}
}

View File

@ -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;
}
}