Added another front page space for Yaffs info. Added roave security for composer.
[yaffs-website] / web / core / lib / Drupal / Core / Diff / DiffFormatter.php
1 <?php
2
3 namespace Drupal\Core\Diff;
4
5 use Drupal\Component\Diff\DiffFormatter as DiffFormatterBase;
6 use Drupal\Component\Diff\WordLevelDiff;
7 use Drupal\Component\Utility\Html;
8 use Drupal\Core\Config\ConfigFactoryInterface;
9
10 /**
11  * Diff formatter which uses returns output that can be rendered to a table.
12  */
13 class DiffFormatter extends DiffFormatterBase {
14
15   /**
16    * The diff represented as an array of rows.
17    *
18    * @var array
19    */
20   protected $rows = [];
21
22   /**
23    * Creates a DiffFormatter to render diffs in a table.
24    *
25    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
26    *   The config factory.
27    */
28   public function __construct(ConfigFactoryInterface $config_factory) {
29     $config = $config_factory->get('system.diff');
30     $this->leading_context_lines = $config->get('context.lines_leading');
31     $this->trailing_context_lines = $config->get('context.lines_trailing');
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function _start_diff() {
38     $this->rows = [];
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   protected function _end_diff() {
45     return $this->rows;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   protected function _block_header($xbeg, $xlen, $ybeg, $ylen) {
52     return [
53       [
54         'data' => $xbeg + $this->line_stats['offset']['x'],
55         'colspan' => 2,
56       ],
57       [
58         'data' => $ybeg + $this->line_stats['offset']['y'],
59         'colspan' => 2,
60       ]
61     ];
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   protected function _start_block($header) {
68     if ($this->show_header) {
69       $this->rows[] = $header;
70     }
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   protected function _lines($lines, $prefix=' ', $color='white') {
77   }
78
79   /**
80    * Creates an added line.
81    *
82    * @param string $line
83    *   An HTML-escaped line.
84    *
85    * @return array
86    *   An array representing a table row.
87    */
88   protected function addedLine($line) {
89     return [
90       [
91         'data' => '+',
92         'class' => 'diff-marker',
93       ],
94       [
95         'data' => ['#markup' => $line],
96         'class' => 'diff-context diff-addedline',
97       ]
98     ];
99   }
100
101   /**
102    * Creates a deleted line.
103    *
104    * @param string $line
105    *   An HTML-escaped line.
106    *
107    * @return array
108    *   An array representing a table row.
109    */
110   protected function deletedLine($line) {
111     return [
112       [
113         'data' => '-',
114         'class' => 'diff-marker',
115       ],
116       [
117         'data' => ['#markup' => $line],
118         'class' => 'diff-context diff-deletedline',
119       ]
120     ];
121   }
122
123   /**
124    * Creates a context line.
125    *
126    * @param string $line
127    *   An HTML-escaped line.
128    *
129    * @return array
130    *   An array representing a table row.
131    */
132   protected function contextLine($line) {
133     return [
134       ' ',
135       [
136         'data' => ['#markup' => $line],
137         'class' => 'diff-context',
138       ]
139     ];
140   }
141
142   /**
143    * Creates an empty line.
144    *
145    * @return array
146    *   An array representing a table row.
147    */
148   protected function emptyLine() {
149     return [
150       ' ',
151       ' ',
152     ];
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   protected function _added($lines) {
159     foreach ($lines as $line) {
160       $this->rows[] = array_merge($this->emptyLine(), $this->addedLine(Html::escape($line)));
161     }
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   protected function _deleted($lines) {
168     foreach ($lines as $line) {
169       $this->rows[] = array_merge($this->deletedLine(Html::escape($line)), $this->emptyLine());
170     }
171   }
172
173   /**
174    * {@inheritdoc}
175    */
176   protected function _context($lines) {
177     foreach ($lines as $line) {
178       $this->rows[] = array_merge($this->contextLine(Html::escape($line)), $this->contextLine(Html::escape($line)));
179     }
180   }
181
182   /**
183    * {@inheritdoc}
184    */
185   protected function _changed($orig, $closing) {
186     $orig = array_map('\Drupal\Component\Utility\Html::escape', $orig);
187     $closing = array_map('\Drupal\Component\Utility\Html::escape', $closing);
188     $diff = new WordLevelDiff($orig, $closing);
189     $del = $diff->orig();
190     $add = $diff->closing();
191
192     // Notice that WordLevelDiff returns HTML-escaped output. Hence, we will be
193     // calling addedLine/deletedLine without HTML-escaping.
194     while ($line = array_shift($del)) {
195       $aline = array_shift( $add );
196       $this->rows[] = array_merge($this->deletedLine($line), isset($aline) ? $this->addedLine($aline) : $this->emptyLine());
197     }
198
199     // If any leftovers.
200     foreach ($add as $line) {
201       $this->rows[] = array_merge($this->emptyLine(), $this->addedLine($line));
202     }
203   }
204
205 }