class DiffFormatter

Same name in this branch
  1. 8.9.x core/lib/Drupal/Component/Diff/DiffFormatter.php \Drupal\Component\Diff\DiffFormatter
Same name in other branches
  1. 9 core/lib/Drupal/Core/Diff/DiffFormatter.php \Drupal\Core\Diff\DiffFormatter
  2. 9 core/lib/Drupal/Component/Diff/DiffFormatter.php \Drupal\Component\Diff\DiffFormatter
  3. 10 core/lib/Drupal/Core/Diff/DiffFormatter.php \Drupal\Core\Diff\DiffFormatter
  4. 10 core/lib/Drupal/Component/Diff/DiffFormatter.php \Drupal\Component\Diff\DiffFormatter
  5. 11.x core/lib/Drupal/Core/Diff/DiffFormatter.php \Drupal\Core\Diff\DiffFormatter
  6. 11.x core/lib/Drupal/Component/Diff/DiffFormatter.php \Drupal\Component\Diff\DiffFormatter

Diff formatter which uses returns output that can be rendered to a table.

Hierarchy

Expanded class hierarchy of DiffFormatter

1 file declares its use of DiffFormatter
ConfigController.php in core/modules/config/src/Controller/ConfigController.php
1 string reference to 'DiffFormatter'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses DiffFormatter
diff.formatter in core/core.services.yml
Drupal\Core\Diff\DiffFormatter

File

core/lib/Drupal/Core/Diff/DiffFormatter.php, line 13

Namespace

Drupal\Core\Diff
View source
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));
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DiffFormatter::$leading_context_lines public property Number of leading context "lines" to preserve.
DiffFormatter::$line_stats protected property The line stats.
DiffFormatter::$rows protected property The diff represented as an array of rows.
DiffFormatter::$show_header public property Should a block header be shown?
DiffFormatter::$trailing_context_lines public property Number of trailing context "lines" to preserve.
DiffFormatter::addedLine protected function Creates an added line.
DiffFormatter::contextLine protected function Creates a context line.
DiffFormatter::deletedLine protected function Creates a deleted line.
DiffFormatter::emptyLine protected function Creates an empty line.
DiffFormatter::format public function Format a diff.
DiffFormatter::_added protected function Overrides DiffFormatter::_added
DiffFormatter::_block protected function
DiffFormatter::_block_header protected function Overrides DiffFormatter::_block_header
DiffFormatter::_changed protected function Overrides DiffFormatter::_changed
DiffFormatter::_context protected function Overrides DiffFormatter::_context
DiffFormatter::_deleted protected function Overrides DiffFormatter::_deleted
DiffFormatter::_end_block protected function
DiffFormatter::_end_diff protected function Overrides DiffFormatter::_end_diff
DiffFormatter::_lines protected function Overrides DiffFormatter::_lines
DiffFormatter::_start_block protected function Overrides DiffFormatter::_start_block
DiffFormatter::_start_diff protected function Overrides DiffFormatter::_start_diff
DiffFormatter::__construct public function Creates a DiffFormatter to render diffs in a table.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.