Permalink
Browse files

Merge branch 'refs/heads/w02_MDL-37286_m25_tracebuffer' into w02_MDL-…

…37187_m25_flatfile
  • Loading branch information...
skodak committed Jan 12, 2013
2 parents 31a22c2 + 4fa7fde commit 0b196a09a8dc990ee352221942ad998750ed81e4
Showing with 236 additions and 3 deletions.
  1. +88 −0 lib/tests/weblib_test.php
  2. +148 −3 lib/weblib.php
View
@@ -212,4 +212,92 @@ public function test_qualified_me() {
$PAGE->set_url('/course/view.php', array('id'=>1));
$this->assertEquals($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
}
+
+ public function test_null_progres_trace() {
+ $this->resetAfterTest(false);
+
+ $trace = new null_progress_trace();
+ $trace->output('do');
+ $trace->output('re', 1);
+ $trace->output('mi', 2);
+ $trace->finished();
+ $output = ob_get_contents();
+ $this->assertSame('', $output);
+ $this->expectOutputString('');
+ }
+
+ public function test_text_progres_trace() {
+ $this->resetAfterTest(false);
+
+ $trace = new text_progress_trace();
+ $trace->output('do');
+ $trace->output('re', 1);
+ $trace->output('mi', 2);
+ $trace->finished();
+ $this->expectOutputString("do\n re\n mi\n");
+ }
+
+ public function test_html_progres_trace() {
+ $this->resetAfterTest(false);
+
+ $trace = new html_progress_trace();
+ $trace->output('do');
+ $trace->output('re', 1);
+ $trace->output('mi', 2);
+ $trace->finished();
+ $this->expectOutputString("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n");
+ }
+
+ public function test_html_list_progress_trace() {
+ $this->resetAfterTest(false);
+
+ $trace = new html_list_progress_trace();
+ $trace->output('do');
+ $trace->output('re', 1);
+ $trace->output('mi', 2);
+ $trace->finished();
+ $this->expectOutputString("<ul>\n<li>do<ul>\n<li>re<ul>\n<li>mi</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n");
+ }
+
+ public function test_progres_trace_buffer() {
+ $this->resetAfterTest(false);
+
+ $trace = new progress_trace_buffer(new html_progress_trace());
+ ob_start();
+ $trace->output('do');
+ $trace->output('re', 1);
+ $trace->output('mi', 2);
+ $trace->finished();
+ $output = ob_get_contents();
+ ob_end_clean();
+ $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $output);
+ $this->assertSame($output, $trace->get_buffer());
+
+ $trace = new progress_trace_buffer(new html_progress_trace(), false);
+ $trace->output('do');
+ $trace->output('re', 1);
+ $trace->output('mi', 2);
+ $trace->finished();
+ $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
+ $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
+ $trace->reset_buffer();
+ $this->assertSame('', $trace->get_buffer());
+ $this->expectOutputString('');
+ }
+
+ 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));
+ $trace->output('do');
+ $trace->output('re', 1);
+ $trace->output('mi', 2);
+ $trace->finished();
+ $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace1->get_buffer());
+ $this->assertSame("do\n re\n mi\n", $trace2->get_buffer());
+ $this->expectOutputString('');
+ }
}
View
@@ -3144,7 +3144,8 @@ public function restart() {
*/
abstract class progress_trace {
/**
- * Ouput an progress message in whatever format.
+ * Output an progress message in whatever format.
+ *
* @param string $message the message to output.
* @param integer $depth indent depth for this message.
*/
@@ -3183,7 +3184,7 @@ public function output($message, $depth = 0) {
*/
class text_progress_trace extends progress_trace {
/**
- * Output the trace message
+ * Output the trace message.
*
* @param string $message
* @param int $depth
@@ -3203,7 +3204,7 @@ public function output($message, $depth = 0) {
*/
class html_progress_trace extends progress_trace {
/**
- * Output the trace message
+ * Output the trace message.
*
* @param string $message
* @param int $depth
@@ -3265,6 +3266,150 @@ public function finished() {
}
}
+/**
+ * This subclass of progress_trace outputs to error log.
+ *
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package moodlecore
+ */
+class error_log_progress_trace extends progress_trace {
+ /** @var string log prefix */
+ protected $prefix;
+
+ /**
+ * Constructor.
+ * @param string $prefix optional log prefix
+ */
+ public function __construct($prefix = '') {
+ $this->prefix = $prefix;
+ }
+
+ /**
+ * Output the trace message.
+ *
+ * @param string $message
+ * @param int $depth
+ * @return void Output is sent to error log.
+ */
+ public function output($message, $depth = 0) {
+ error_log($this->prefix . str_repeat(' ', $depth) . $message);
+ }
+}
+
+/**
+ * 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
+ * @package moodlecore
+ */
+class progress_trace_buffer extends progress_trace {
+ /** @var progres_trace */
+ protected $trace;
+ /** @var bool do we pass output out */
+ protected $passthrough;
+ /** @var string output buffer */
+ protected $buffer;
+
+ /**
+ * Constructor.
+ *
+ * @param progress_trace $trace
+ * @param bool $passthrough true means output and buffer, false means just buffer and no output
+ */
+ public function __construct(progress_trace $trace, $passthrough = true) {
+ $this->trace = $trace;
+ $this->passthrough = $passthrough;
+ $this->buffer = '';
+ }
+
+ /**
+ * Output the trace message.
+ *
+ * @param string $message the message to output.
+ * @param int $depth indent depth for this message.
+ * @return void output stored in buffer
+ */
+ public function output($message, $depth = 0) {
+ ob_start();
+ $this->trace->output($message, $depth);
+ $this->buffer .= ob_get_contents();
+ if ($this->passthrough) {
+ ob_end_flush();
+ } else {
+ ob_end_clean();
+ }
+ }
+
+ /**
+ * Called when the processing is finished.
+ */
+ public function finished() {
+ ob_start();
+ $this->trace->finished();
+ $this->buffer .= ob_get_contents();
+ if ($this->passthrough) {
+ ob_end_flush();
+ } else {
+ ob_end_clean();
+ }
+ }
+
+ /**
+ * Reset internal text buffer.
+ */
+ public function reset_buffer() {
+ $this->buffer = '';
+ }
+
+ /**
+ * Return internal text buffer.
+ * @return string buffered plain text
+ */
+ public function get_buffer() {
+ return $this->buffer;
+ }
+}
+
+/**
+ * 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
*

0 comments on commit 0b196a0

Please sign in to comment.