UnifiedDiffFormatter.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Portions taken from phpwiki-1.3.3.
  4. *
  5. * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
  6. * You may copy this code freely under the conditions of the GPL.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup DifferenceEngine
  25. */
  26. /**
  27. * A formatter that outputs unified diffs
  28. * @ingroup DifferenceEngine
  29. */
  30. class UnifiedDiffFormatter extends DiffFormatter {
  31. /** @var int */
  32. protected $leadingContextLines = 2;
  33. /** @var int */
  34. protected $trailingContextLines = 2;
  35. /**
  36. * @param string[] $lines
  37. * @param string $prefix
  38. */
  39. protected function lines( $lines, $prefix = ' ' ) {
  40. foreach ( $lines as $line ) {
  41. $this->writeOutput( "{$prefix}{$line}\n" );
  42. }
  43. }
  44. /**
  45. * @param string[] $lines
  46. */
  47. protected function added( $lines ) {
  48. $this->lines( $lines, '+' );
  49. }
  50. /**
  51. * @param string[] $lines
  52. */
  53. protected function deleted( $lines ) {
  54. $this->lines( $lines, '-' );
  55. }
  56. /**
  57. * @param string[] $orig
  58. * @param string[] $closing
  59. */
  60. protected function changed( $orig, $closing ) {
  61. $this->deleted( $orig );
  62. $this->added( $closing );
  63. }
  64. /**
  65. * @param int $xbeg
  66. * @param int $xlen
  67. * @param int $ybeg
  68. * @param int $ylen
  69. *
  70. * @return string
  71. */
  72. protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
  73. return "@@ -$xbeg,$xlen +$ybeg,$ylen @@";
  74. }
  75. }