Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\output\progress_trace;
18
 
19
use core\output\progress_trace;
20
 
21
/**
22
 * Special type of trace that can be used for catching of output of other traces.
23
 *
24
 * @copyright Petr Skoda {@link http://skodak.org}
25
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @package core
27
 */
28
class progress_trace_buffer extends progress_trace {
29
    /** @var string output buffer */
30
    protected string $buffer = '';
31
 
32
    /**
33
     * Constructor.
34
     *
35
     * @param progress_trace $trace
36
     * @param bool $passthrough true means output and buffer, false means just buffer and no output
37
     */
38
    public function __construct(
39
        /** @var progress_trace The progress_trace to pass content to */
40
        protected progress_trace $trace,
41
        /** @var bool Whether we pass output out */
42
        protected bool $passthrough = true,
43
    ) {
44
        $this->buffer      = '';
45
    }
46
 
47
    #[\Override]
48
    public function output(
49
        string $message,
50
        int $depth = 0,
51
    ): void {
52
        ob_start();
53
        $this->trace->output($message, $depth);
54
        $this->buffer .= ob_get_contents();
55
        if ($this->passthrough) {
56
            ob_end_flush();
57
        } else {
58
            ob_end_clean();
59
        }
60
    }
61
 
62
    #[\Override]
63
    public function finished(): void {
64
        ob_start();
65
        $this->trace->finished();
66
        $this->buffer .= ob_get_contents();
67
        if ($this->passthrough) {
68
            ob_end_flush();
69
        } else {
70
            ob_end_clean();
71
        }
72
    }
73
 
74
    /**
75
     * Reset the internal text buffer.
76
     */
77
    public function reset_buffer(): void {
78
        $this->buffer = '';
79
    }
80
 
81
    /**
82
     * Return the internal text buffer.
83
     *
84
     * @return string buffered plain text
85
     */
86
    public function get_buffer(): string {
87
        return $this->buffer;
88
    }
89
}
90
 
91
// Alias this class to the old name.
92
// This file will be autoloaded by the legacyclasses autoload system.
93
// In future all uses of this class will be corrected and the legacy references will be removed.
94
class_alias(progress_trace_buffer::class, \progress_trace_buffer::class);