PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/service_container/lib/Drupal/Component/Diff/DiffFormatter.php

https://gitlab.com/leoplanxxi/dr7-web-buap-2016
PHP | 187 lines | 127 code | 26 blank | 34 comment | 23 complexity | 5bef0c322967c15a4f2b1604ca8b2779 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. var $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. var $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. var $trailing_context_lines = 0;
  33. /**
  34. * Format a diff.
  35. *
  36. * @param \Drupal\Component\Diff\Diff $diff
  37. * A Diff object.
  38. *
  39. * @return string
  40. * The formatted output.
  41. */
  42. public function format(Diff $diff) {
  43. $xi = $yi = 1;
  44. $block = FALSE;
  45. $context = array();
  46. $nlead = $this->leading_context_lines;
  47. $ntrail = $this->trailing_context_lines;
  48. $this->_start_diff();
  49. foreach ($diff->getEdits() as $edit) {
  50. if ($edit->type == 'copy') {
  51. if (is_array($block)) {
  52. if (sizeof($edit->orig) <= $nlead + $ntrail) {
  53. $block[] = $edit;
  54. }
  55. else {
  56. if ($ntrail) {
  57. $context = array_slice($edit->orig, 0, $ntrail);
  58. $block[] = new DiffOpCopy($context);
  59. }
  60. $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
  61. $block = FALSE;
  62. }
  63. }
  64. $context = $edit->orig;
  65. }
  66. else {
  67. if (! is_array($block)) {
  68. $context = array_slice($context, sizeof($context) - $nlead);
  69. $x0 = $xi - sizeof($context);
  70. $y0 = $yi - sizeof($context);
  71. $block = array();
  72. if ($context) {
  73. $block[] = new DiffOpCopy($context);
  74. }
  75. }
  76. $block[] = $edit;
  77. }
  78. if ($edit->orig) {
  79. $xi += sizeof($edit->orig);
  80. }
  81. if ($edit->closing) {
  82. $yi += sizeof($edit->closing);
  83. }
  84. }
  85. if (is_array($block)) {
  86. $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
  87. }
  88. $end = $this->_end_diff();
  89. if (!empty($xi)) {
  90. $this->line_stats['counter']['x'] += $xi;
  91. }
  92. if (!empty($yi)) {
  93. $this->line_stats['counter']['y'] += $yi;
  94. }
  95. return $end;
  96. }
  97. protected function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
  98. $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
  99. foreach ($edits as $edit) {
  100. if ($edit->type == 'copy') {
  101. $this->_context($edit->orig);
  102. }
  103. elseif ($edit->type == 'add') {
  104. $this->_added($edit->closing);
  105. }
  106. elseif ($edit->type == 'delete') {
  107. $this->_deleted($edit->orig);
  108. }
  109. elseif ($edit->type == 'change') {
  110. $this->_changed($edit->orig, $edit->closing);
  111. }
  112. else {
  113. trigger_error('Unknown edit type', E_USER_ERROR);
  114. }
  115. }
  116. $this->_end_block();
  117. }
  118. protected function _start_diff() {
  119. ob_start();
  120. }
  121. protected function _end_diff() {
  122. $val = ob_get_contents();
  123. ob_end_clean();
  124. return $val;
  125. }
  126. protected function _block_header($xbeg, $xlen, $ybeg, $ylen) {
  127. if ($xlen > 1) {
  128. $xbeg .= "," . ($xbeg + $xlen - 1);
  129. }
  130. if ($ylen > 1) {
  131. $ybeg .= "," . ($ybeg + $ylen - 1);
  132. }
  133. return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
  134. }
  135. protected function _start_block($header) {
  136. if ($this->show_header) {
  137. echo $header . "\n";
  138. }
  139. }
  140. protected function _end_block() {
  141. }
  142. protected function _lines($lines, $prefix = ' ') {
  143. foreach ($lines as $line) {
  144. echo "$prefix $line\n";
  145. }
  146. }
  147. protected function _context($lines) {
  148. $this->_lines($lines);
  149. }
  150. protected function _added($lines) {
  151. $this->_lines($lines, '>');
  152. }
  153. protected function _deleted($lines) {
  154. $this->_lines($lines, '<');
  155. }
  156. protected function _changed($orig, $closing) {
  157. $this->_deleted($orig);
  158. echo "---\n";
  159. $this->_added($closing);
  160. }
  161. }