PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/lib/Drupal/Component/Diff/DiffFormatter.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 197 lines | 131 code | 27 blank | 39 comment | 23 complexity | 401df3814297c38cafabb0b4f3444548 MD5 | raw file
  1. <?php
  2. namespace Drupal\Component\Diff;
  3. use Drupal\Component\Diff\Engine\DiffOpCopy;
  4. /**
  5. * A class to format Diffs
  6. *
  7. * This class formats the diff in classic diff format.
  8. * It is intended that this class be customized via inheritance,
  9. * to obtain fancier outputs.
  10. * @todo document
  11. * @private
  12. * @subpackage DifferenceEngine
  13. */
  14. class DiffFormatter {
  15. /**
  16. * Should a block header be shown?
  17. */
  18. public $show_header = TRUE;
  19. /**
  20. * Number of leading context "lines" to preserve.
  21. *
  22. * This should be left at zero for this class, but subclasses
  23. * may want to set this to other values.
  24. */
  25. public $leading_context_lines = 0;
  26. /**
  27. * Number of trailing context "lines" to preserve.
  28. *
  29. * This should be left at zero for this class, but subclasses
  30. * may want to set this to other values.
  31. */
  32. public $trailing_context_lines = 0;
  33. /**
  34. * The line stats.
  35. *
  36. * @var array
  37. */
  38. protected $line_stats = [
  39. 'counter' => ['x' => 0, 'y' => 0],
  40. 'offset' => ['x' => 0, 'y' => 0],
  41. ];
  42. /**
  43. * Format a diff.
  44. *
  45. * @param \Drupal\Component\Diff\Diff $diff
  46. * A Diff object.
  47. *
  48. * @return string
  49. * The formatted output.
  50. */
  51. public function format(Diff $diff) {
  52. $xi = $yi = 1;
  53. $block = FALSE;
  54. $context = [];
  55. $nlead = $this->leading_context_lines;
  56. $ntrail = $this->trailing_context_lines;
  57. $this->_start_diff();
  58. foreach ($diff->getEdits() as $edit) {
  59. if ($edit->type == 'copy') {
  60. if (is_array($block)) {
  61. if (sizeof($edit->orig) <= $nlead + $ntrail) {
  62. $block[] = $edit;
  63. }
  64. else {
  65. if ($ntrail) {
  66. $context = array_slice($edit->orig, 0, $ntrail);
  67. $block[] = new DiffOpCopy($context);
  68. }
  69. $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
  70. $block = FALSE;
  71. }
  72. }
  73. $context = $edit->orig;
  74. }
  75. else {
  76. if (!is_array($block)) {
  77. $context = array_slice($context, sizeof($context) - $nlead);
  78. $x0 = $xi - sizeof($context);
  79. $y0 = $yi - sizeof($context);
  80. $block = [];
  81. if ($context) {
  82. $block[] = new DiffOpCopy($context);
  83. }
  84. }
  85. $block[] = $edit;
  86. }
  87. if ($edit->orig) {
  88. $xi += sizeof($edit->orig);
  89. }
  90. if ($edit->closing) {
  91. $yi += sizeof($edit->closing);
  92. }
  93. }
  94. if (is_array($block)) {
  95. $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
  96. }
  97. $end = $this->_end_diff();
  98. if (!empty($xi)) {
  99. $this->line_stats['counter']['x'] += $xi;
  100. }
  101. if (!empty($yi)) {
  102. $this->line_stats['counter']['y'] += $yi;
  103. }
  104. return $end;
  105. }
  106. protected function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
  107. $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
  108. foreach ($edits as $edit) {
  109. if ($edit->type == 'copy') {
  110. $this->_context($edit->orig);
  111. }
  112. elseif ($edit->type == 'add') {
  113. $this->_added($edit->closing);
  114. }
  115. elseif ($edit->type == 'delete') {
  116. $this->_deleted($edit->orig);
  117. }
  118. elseif ($edit->type == 'change') {
  119. $this->_changed($edit->orig, $edit->closing);
  120. }
  121. else {
  122. trigger_error('Unknown edit type', E_USER_ERROR);
  123. }
  124. }
  125. $this->_end_block();
  126. }
  127. protected function _start_diff() {
  128. ob_start();
  129. }
  130. protected function _end_diff() {
  131. $val = ob_get_contents();
  132. ob_end_clean();
  133. return $val;
  134. }
  135. protected function _block_header($xbeg, $xlen, $ybeg, $ylen) {
  136. if ($xlen > 1) {
  137. $xbeg .= "," . ($xbeg + $xlen - 1);
  138. }
  139. if ($ylen > 1) {
  140. $ybeg .= "," . ($ybeg + $ylen - 1);
  141. }
  142. return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
  143. }
  144. protected function _start_block($header) {
  145. if ($this->show_header) {
  146. echo $header . "\n";
  147. }
  148. }
  149. protected function _end_block() {
  150. }
  151. protected function _lines($lines, $prefix = ' ') {
  152. foreach ($lines as $line) {
  153. echo "$prefix $line\n";
  154. }
  155. }
  156. protected function _context($lines) {
  157. $this->_lines($lines);
  158. }
  159. protected function _added($lines) {
  160. $this->_lines($lines, '>');
  161. }
  162. protected function _deleted($lines) {
  163. $this->_lines($lines, '<');
  164. }
  165. protected function _changed($orig, $closing) {
  166. $this->_deleted($orig);
  167. echo "---\n";
  168. $this->_added($closing);
  169. }
  170. }