123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?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;
- /**
- * Diff formatter which uses returns output that can be rendered to a table.
- */
- class DiffFormatter extends DiffFormatterBase {
- /**
- * The diff represented as an array of rows.
- *
- * @var array
- */
- protected $rows = [];
- /**
- * Creates a DiffFormatter to render diffs in a table.
- *
- * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
- * The config factory.
- */
- 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');
- }
- /**
- * {@inheritdoc}
- */
- protected function _start_diff() {
- $this->rows = [];
- }
- /**
- * {@inheritdoc}
- */
- protected function _end_diff() {
- return $this->rows;
- }
- /**
- * {@inheritdoc}
- */
- 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,
- ]
- ];
- }
- /**
- * {@inheritdoc}
- */
- protected function _start_block($header) {
- if ($this->show_header) {
- $this->rows[] = $header;
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function _lines($lines, $prefix = ' ', $color = 'white') {
- }
- /**
- * Creates an added line.
- *
- * @param string $line
- * An HTML-escaped line.
- *
- * @return array
- * An array representing a table row.
- */
- protected function addedLine($line) {
- return [
- [
- 'data' => '+',
- 'class' => 'diff-marker',
- ],
- [
- 'data' => ['#markup' => $line],
- 'class' => 'diff-context diff-addedline',
- ]
- ];
- }
- /**
- * Creates a deleted line.
- *
- * @param string $line
- * An HTML-escaped line.
- *
- * @return array
- * An array representing a table row.
- */
- protected function deletedLine($line) {
- return [
- [
- 'data' => '-',
- 'class' => 'diff-marker',
- ],
- [
- 'data' => ['#markup' => $line],
- 'class' => 'diff-context diff-deletedline',
- ]
- ];
- }
- /**
- * Creates a context line.
- *
- * @param string $line
- * An HTML-escaped line.
- *
- * @return array
- * An array representing a table row.
- */
- protected function contextLine($line) {
- return [
- ' ',
- [
- 'data' => ['#markup' => $line],
- 'class' => 'diff-context',
- ]
- ];
- }
- /**
- * Creates an empty line.
- *
- * @return array
- * An array representing a table row.
- */
- protected function emptyLine() {
- return [
- ' ',
- ' ',
- ];
- }
- /**
- * {@inheritdoc}
- */
- protected function _added($lines) {
- foreach ($lines as $line) {
- $this->rows[] = array_merge($this->emptyLine(), $this->addedLine(Html::escape($line)));
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function _deleted($lines) {
- foreach ($lines as $line) {
- $this->rows[] = array_merge($this->deletedLine(Html::escape($line)), $this->emptyLine());
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function _context($lines) {
- foreach ($lines as $line) {
- $this->rows[] = array_merge($this->contextLine(Html::escape($line)), $this->contextLine(Html::escape($line)));
- }
- }
- /**
- * {@inheritdoc}
- */
- 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();
- // Notice that WordLevelDiff returns HTML-escaped output. Hence, we will be
- // calling addedLine/deletedLine without HTML-escaping.
- while ($line = array_shift($del)) {
- $aline = array_shift($add);
- $this->rows[] = array_merge($this->deletedLine($line), isset($aline) ? $this->addedLine($aline) : $this->emptyLine());
- }
- // If any leftovers.
- foreach ($add as $line) {
- $this->rows[] = array_merge($this->emptyLine(), $this->addedLine($line));
- }
- }
- }
|