From e7259ec998cd1c3bba28c187ea977c97971aa281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 29 Dec 2012 18:55:44 +0100 Subject: [PATCH] MDL-37286 add new combined_progress_trace This type of trace should be useful when multiple trace outputs requested. --- lib/tests/weblib_test.php | 20 +++++++++++++++++++ lib/weblib.php | 41 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php index 30be47fc951..c9586f5c4b6 100644 --- a/lib/tests/weblib_test.php +++ b/lib/tests/weblib_test.php @@ -297,4 +297,24 @@ class web_testcase extends advanced_testcase { $trace->reset_buffer(); $this->assertSame('', $trace->get_buffer()); } + + public function test_combined_progres_trace() { + $this->resetAfterTest(false); + + $trace1 = new progress_trace_buffer(new html_progress_trace(), false); + $trace2 = new progress_trace_buffer(new text_progress_trace(), false); + + $trace = new combined_progress_trace(array($trace1, $trace2)); + + ob_start(); + $trace->output('do'); + $trace->output('re', 1); + $trace->output('mi', 2); + $trace->finished(); + $output = ob_get_contents(); + ob_end_clean(); + $this->assertSame('', $output); + $this->assertSame("

do

\n

  re

\n

    mi

\n", $trace1->get_buffer()); + $this->assertSame("do\n re\n mi\n", $trace2->get_buffer()); + } } diff --git a/lib/weblib.php b/lib/weblib.php index 938bd6f43bb..0b2caeaa932 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -3297,7 +3297,7 @@ class error_log_progress_trace extends progress_trace { } /** - * Special type of traced that can be used for catching of + * Special type of trace that can be used for catching of * output of other traces. * * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -3371,6 +3371,45 @@ class progress_trace_buffer extends progress_trace { } } +/** + * Special type of trace that can be used for redirecting to multiple + * other traces. + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package moodlecore + */ +class combined_progress_trace extends progress_trace { + protected $traces; + + /** + * @param array $traces multiple traces + */ + public function __construct(array $traces) { + $this->traces = $traces; + } + + /** + * Output an progress message in whatever format. + * + * @param string $message the message to output. + * @param integer $depth indent depth for this message. + */ + public function output($message, $depth = 0) { + foreach($this->traces as $trace) { + $trace->output($message, $depth); + } + } + + /** + * Called when the processing is finished. + */ + public function finished() { + foreach($this->traces as $trace) { + $trace->finished(); + } + } +} + /** * Returns a localized sentence in the current language summarizing the current password policy *