File: OutputIgnoreBuffer.php

package info (click to toggle)
php-doc 20250827~git.abe740d%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 71,968 kB
  • sloc: xml: 985,760; php: 25,504; javascript: 671; sh: 177; makefile: 37
file content (167 lines) | stat: -rw-r--r-- 5,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
 *  +----------------------------------------------------------------------+
 *  | Copyright (c) 1997-2023 The PHP Group                                |
 *  +----------------------------------------------------------------------+
 *  | This source file is subject to version 3.01 of the PHP license,      |
 *  | that is bundled with this package in the file LICENSE, and is        |
 *  | available through the world-wide-web at the following url:           |
 *  | https://www.php.net/license/3_01.txt.                                |
 *  | If you did not receive a copy of the PHP license and are unable to   |
 *  | obtain it through the world-wide-web, please send a note to          |
 *  | license@php.net, so we can mail you a copy immediately.              |
 *  +----------------------------------------------------------------------+
 *  | Authors:     André L F S Bacci <ae php.net>                          |
 *  +----------------------------------------------------------------------+
 *  | Description: Cache the output and shows if if not marked to ignore.  |
 *  +----------------------------------------------------------------------+
 */

require_once __DIR__ . '/all.php';

class OutputIgnoreBuffer
{
    public int $printCount = 0;

    private string $filename = "";
    private string $header = "";
    private array  $matter = [];
    private array  $footer = [];

    private OutputIgnoreArgv $args;

    function __construct( OutputIgnoreArgv $args , string $header , string $filename )
    {
        $this->args = $args;
        $this->header = $header;
        $this->filename = $filename;
    }

    function add( string $text )
    {
        $this->matter[] = $text;
    }

    function addDiff( string $text , int $sourceCount , int $targetCount )
    {
        if ( $sourceCount == $targetCount )
            return;
        $prefix = "* ";
        $suffix = " -{$targetCount} +{$sourceCount}";
        if ( $sourceCount == 0 )
        {
            $prefix = "- ";
            $suffix = $targetCount == 1 ? "" : " -{$targetCount}";
        }
        if ( $targetCount == 0 )
        {
            $prefix = "+ ";
            $suffix = $sourceCount == 1 ? "" : " +{$sourceCount}";
        }
        $this->add( "{$prefix}{$text}{$suffix}\n" );
    }

    function addFooter( string $text )
    {
        $this->footer[] = $text;
    }

    function addLine()
    {
        if ( count( $this->matter ) > 0 && end( $this->matter ) != "\n" )
            $this->add( "\n" );
    }

    function print( bool $useAlternatePrinting = false )
    {
        if ( count( $this->matter ) == 0 )
            return;
        $this->printCount++;

        // footer

        $markhead = $this->filename . ':' . $this->hash( false ) . ':';
        $markfull = $markhead . $this->hash( true );
        $marks = OutputIgnoreArgv::cacheFile()->load( [] );

        if ( $this->args->showIgnore )
        {
            // --add-ignore

            if ( in_array( $markfull , $marks ) )
                $this->matter = [];
            else
                $this->args->pushAddIgnore( $this , $markfull );

            // Avoid dupliocates on output

            while ( in_array( $markfull , $marks ) )
            {
                $key = array_search( $markfull , $marks );
                unset( $marks[$key] );
            }

            // --del-ignore

            foreach ( $marks as $mark )
                if ( $mark != null )
                    if ( str_starts_with( $mark , $markhead ) )
                        $this->args->pushDelIgnore( $this , $mark );
        }

        if ( count( $this->matter ) == 0 && count( $this->footer ) == 0 )
            return;

        print $this->header;

        if ( $useAlternatePrinting )
            $this->printMatterAlternate();
        else
            foreach( $this->matter as $text )
                print $text;

        if ( count( $this->matter ) )
            print "\n";

        foreach( $this->footer as $text )
            print $text;

        if ( count( $this->footer ) )
            print "\n";
    }

    private function printMatterAlternate() : void
    {
        $add = [];
        $del = [];
        $rst = [];

        foreach( $this->matter as $text )
        {
            if     ( $text[0] == '+' ) $add[] = $text;
            elseif ( $text[0] == '-' ) $del[] = $text;
            else                       $rst[] = $text;
        }

        for ( $idx = 0 ; $idx < count( $this->matter ) ; $idx++ )
        {
            if ( isset( $add[ $idx ] ) ) print $add[ $idx ];
            if ( isset( $del[ $idx ] ) ) print $del[ $idx ];
        }

        foreach( $rst as $text )
            print $text;
    }

    private function hash( bool $withContents ) : string
    {
        $text = $this->header . $this->args->options;
        if ( $withContents )
            $text .= implode( "" , $this->matter );
        $text = str_replace( " " , "" , $text );
        $text = str_replace( "\n" , "" , $text );
        $text = str_replace( "\r" , "" , $text );
        $text = str_replace( "\t" , "" , $text );
        return hash( "crc32b" , $text );
    }
}