|
| 1 | +<?php /* |
| 2 | ++----------------------------------------------------------------------+ |
| 3 | +| Copyright (c) 1997-2025 The PHP Group | |
| 4 | ++----------------------------------------------------------------------+ |
| 5 | +| This source file is subject to version 3.01 of the PHP license, | |
| 6 | +| that is bundled with this package in the file LICENSE, and is | |
| 7 | +| available through the world-wide-web at the following url: | |
| 8 | +| https://www.php.net/license/3_01.txt. | |
| 9 | +| If you did not receive a copy of the PHP license and are unable to | |
| 10 | +| obtain it through the world-wide-web, please send a note to | |
| 11 | +| [email protected], so we can mail you a copy immediately. | |
| 12 | ++----------------------------------------------------------------------+ |
| 13 | +| Authors: André L F S Bacci <ae php.net> | |
| 14 | ++----------------------------------------------------------------------+ |
| 15 | +
|
| 16 | +# Description |
| 17 | +
|
| 18 | +This class caches formatted output, and calculates if this output is not |
| 19 | +previously marked as ignored, before printing it. */ |
| 20 | + |
| 21 | +class OutputBuffer |
| 22 | +{ |
| 23 | + private string $filename = ""; |
| 24 | + private string $header = ""; |
| 25 | + private array $matter = []; |
| 26 | + private array $footer = []; |
| 27 | + |
| 28 | + private OutputIgnore $ignore; |
| 29 | + private string $options; |
| 30 | + |
| 31 | + public function __construct( string $header , string $filename , OutputIgnore $ignore ) |
| 32 | + { |
| 33 | + $filename = str_replace( "/./" , "/" , $filename ); |
| 34 | + |
| 35 | + $this->header = $header . ": " . $filename . "\n\n"; |
| 36 | + $this->filename = $filename; |
| 37 | + $this->ignore = $ignore; |
| 38 | + |
| 39 | + $copy = $ignore->residualArgv; |
| 40 | + array_shift( $copy ); |
| 41 | + $this->options = implode( " " , $copy ); |
| 42 | + } |
| 43 | + |
| 44 | + public function add( string $text ) |
| 45 | + { |
| 46 | + $this->matter[] = $text; |
| 47 | + } |
| 48 | + |
| 49 | + public function addDiff( string $text , int $sourceCount , int $targetCount ) |
| 50 | + { |
| 51 | + if ( $sourceCount == $targetCount ) |
| 52 | + return; |
| 53 | + $prefix = "* "; |
| 54 | + $suffix = " -{$targetCount} +{$sourceCount}"; |
| 55 | + if ( $sourceCount == 0 ) |
| 56 | + { |
| 57 | + $prefix = "- "; |
| 58 | + $suffix = $targetCount == 1 ? "" : " -{$targetCount}"; |
| 59 | + } |
| 60 | + if ( $targetCount == 0 ) |
| 61 | + { |
| 62 | + $prefix = "+ "; |
| 63 | + $suffix = $sourceCount == 1 ? "" : " +{$sourceCount}"; |
| 64 | + } |
| 65 | + $this->add( "{$prefix}{$text}{$suffix}\n" ); |
| 66 | + } |
| 67 | + |
| 68 | + public function addFooter( string $text ) |
| 69 | + { |
| 70 | + $this->footer[] = $text; |
| 71 | + } |
| 72 | + |
| 73 | + public function addLine() |
| 74 | + { |
| 75 | + if ( count( $this->matter ) > 0 && end( $this->matter ) != "\n" ) |
| 76 | + $this->add( "\n" ); |
| 77 | + } |
| 78 | + |
| 79 | + public function print( bool $useAlternatePrinting = false ) |
| 80 | + { |
| 81 | + if ( count( $this->matter ) == 0 && count( $this->footer ) == 0 ) |
| 82 | + return; |
| 83 | + |
| 84 | + $hashHead = $this->hash( false ); |
| 85 | + $hashFull = $this->hash( true ); |
| 86 | + |
| 87 | + if ( $this->ignore->shouldIgnore( $this , $this->filename , $hashHead , $hashFull ) ) |
| 88 | + return; |
| 89 | + |
| 90 | + print $this->header; |
| 91 | + |
| 92 | + if ( $useAlternatePrinting ) |
| 93 | + $this->printMatterAlternate(); |
| 94 | + else |
| 95 | + foreach( $this->matter as $text ) |
| 96 | + print $text; |
| 97 | + |
| 98 | + if ( count( $this->matter ) ) |
| 99 | + print "\n"; |
| 100 | + |
| 101 | + foreach( $this->footer as $text ) |
| 102 | + print $text; |
| 103 | + |
| 104 | + if ( count( $this->footer ) ) |
| 105 | + print "\n"; |
| 106 | + } |
| 107 | + |
| 108 | + private function printMatterAlternate() : void |
| 109 | + { |
| 110 | + $add = array(); |
| 111 | + $del = array(); |
| 112 | + $rst = array(); |
| 113 | + |
| 114 | + foreach( $this->matter as $text ) |
| 115 | + { |
| 116 | + if ( $text[0] == '+' ) $add[] = $text; |
| 117 | + elseif ( $text[0] == '-' ) $del[] = $text; |
| 118 | + else $rst[] = $text; |
| 119 | + } |
| 120 | + |
| 121 | + for ( $idx = 0 ; $idx < count( $this->matter ) ; $idx++ ) |
| 122 | + { |
| 123 | + if ( isset( $add[ $idx ] ) ) print $add[ $idx ]; |
| 124 | + if ( isset( $del[ $idx ] ) ) print $del[ $idx ]; |
| 125 | + } |
| 126 | + |
| 127 | + foreach( $rst as $text ) |
| 128 | + print $text; |
| 129 | + } |
| 130 | + |
| 131 | + private function hash( bool $withContents ) : string |
| 132 | + { |
| 133 | + $text = $this->header . $this->options; |
| 134 | + if ( $withContents ) |
| 135 | + $text .= implode( "" , $this->matter ); |
| 136 | + $text = str_replace( " " , "" , $text ); |
| 137 | + $text = str_replace( "\n" , "" , $text ); |
| 138 | + $text = str_replace( "\r" , "" , $text ); |
| 139 | + $text = str_replace( "\t" , "" , $text ); |
| 140 | + return hash( "crc32b" , $text ); |
| 141 | + } |
| 142 | +} |
0 commit comments