View source
<?php
namespace Drupal\Core\Diff;
use Drupal\Component\Diff\DiffFormatter as DiffFormatterBase;
use Drupal\Component\Diff\WordLevelDiff;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
class DiffFormatter extends DiffFormatterBase {
protected $rows = [];
public function __construct(ConfigFactoryInterface $config_factory) {
$config = $config_factory
->get('system.diff');
$this->leading_context_lines = $config
->get('context.lines_leading');
$this->trailing_context_lines = $config
->get('context.lines_trailing');
}
protected function _start_diff() {
$this->rows = [];
}
protected function _end_diff() {
return $this->rows;
}
protected function _block_header($xbeg, $xlen, $ybeg, $ylen) {
return [
[
'data' => $xbeg + $this->line_stats['offset']['x'],
'colspan' => 2,
],
[
'data' => $ybeg + $this->line_stats['offset']['y'],
'colspan' => 2,
],
];
}
protected function _start_block($header) {
if ($this->show_header) {
$this->rows[] = $header;
}
}
protected function _lines($lines, $prefix = ' ', $color = 'white') {
}
protected function addedLine($line) {
return [
[
'data' => '+',
'class' => 'diff-marker',
],
[
'data' => [
'#markup' => $line,
],
'class' => 'diff-context diff-addedline',
],
];
}
protected function deletedLine($line) {
return [
[
'data' => '-',
'class' => 'diff-marker',
],
[
'data' => [
'#markup' => $line,
],
'class' => 'diff-context diff-deletedline',
],
];
}
protected function contextLine($line) {
return [
' ',
[
'data' => [
'#markup' => $line,
],
'class' => 'diff-context',
],
];
}
protected function emptyLine() {
return [
' ',
' ',
];
}
protected function _added($lines) {
foreach ($lines as $line) {
$this->rows[] = array_merge($this
->emptyLine(), $this
->addedLine(Html::escape($line)));
}
}
protected function _deleted($lines) {
foreach ($lines as $line) {
$this->rows[] = array_merge($this
->deletedLine(Html::escape($line)), $this
->emptyLine());
}
}
protected function _context($lines) {
foreach ($lines as $line) {
$this->rows[] = array_merge($this
->contextLine(Html::escape($line)), $this
->contextLine(Html::escape($line)));
}
}
protected function _changed($orig, $closing) {
$orig = array_map('\\Drupal\\Component\\Utility\\Html::escape', $orig);
$closing = array_map('\\Drupal\\Component\\Utility\\Html::escape', $closing);
$diff = new WordLevelDiff($orig, $closing);
$del = $diff
->orig();
$add = $diff
->closing();
while ($line = array_shift($del)) {
$aline = array_shift($add);
$this->rows[] = array_merge($this
->deletedLine($line), isset($aline) ? $this
->addedLine($aline) : $this
->emptyLine());
}
foreach ($add as $line) {
$this->rows[] = array_merge($this
->emptyLine(), $this
->addedLine($line));
}
}
}