| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebPConvert\Loggers; |
| 4 |
|
| 5 |
use WebPConvert\Loggers\BaseLogger; |
| 6 |
|
| 7 |
/** |
| 8 |
* Collect the logging and retrieve it later in HTML or plain text format. |
| 9 |
* |
| 10 |
* @package WebPConvert |
| 11 |
* @author Bjørn Rosell <[email protected]> |
| 12 |
* @since Class available since Release 2.0.0 |
| 13 |
*/ |
| 14 |
class BufferLogger extends BaseLogger |
| 15 |
{ |
| 16 |
public $entries = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* Write a message to the buffer - all entries can later be retrieved with getText() or getHtlm(). |
| 20 |
* |
| 21 |
* @param string $msg message to log |
| 22 |
* @param string $style style (null | bold | italic) |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function log($msg, $style = '') |
| 26 |
{ |
| 27 |
$this->entries[] = [$msg, $style]; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Write a new line to the buffer. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function ln() |
| 36 |
{ |
| 37 |
$this->entries[] = ''; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get everything logged - as HTML. |
| 42 |
* |
| 43 |
* @return string The log, formatted as HTML. |
| 44 |
*/ |
| 45 |
public function getHtml() |
| 46 |
{ |
| 47 |
$html = ''; |
| 48 |
foreach ($this->entries as $entry) { |
| 49 |
if ($entry == '') { |
| 50 |
$html .= '<br>'; |
| 51 |
} else { |
| 52 |
list($msg, $style) = $entry; |
| 53 |
$msg = htmlspecialchars($msg); |
| 54 |
if ($style == 'bold') { |
| 55 |
$html .= '<b>' . $msg . '</b>'; |
| 56 |
} elseif ($style == 'italic') { |
| 57 |
$html .= '<i>' . $msg . '</i>'; |
| 58 |
} else { |
| 59 |
$html .= $msg; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
return $html; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get everything logged - as markdown. |
| 68 |
* |
| 69 |
* @return string The log, formatted as MarkDown. |
| 70 |
*/ |
| 71 |
public function getMarkDown($newLineChar = "\n\r") |
| 72 |
{ |
| 73 |
$md = ''; |
| 74 |
foreach ($this->entries as $entry) { |
| 75 |
if ($entry == '') { |
| 76 |
$md .= $newLineChar; |
| 77 |
} else { |
| 78 |
list($msg, $style) = $entry; |
| 79 |
if ($style == 'bold') { |
| 80 |
$md .= '**' . $msg . '** '; |
| 81 |
} elseif ($style == 'italic') { |
| 82 |
$md .= '*' . $msg . '* '; |
| 83 |
} else { |
| 84 |
$md .= $msg; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return $md; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get everything logged - as plain text. |
| 93 |
* |
| 94 |
* @param string $newLineChar. The character used for new lines. |
| 95 |
* @return string The log, formatted as plain text. |
| 96 |
*/ |
| 97 |
public function getText($newLineChar = ' ') |
| 98 |
{ |
| 99 |
$text = ''; |
| 100 |
foreach ($this->entries as $entry) { |
| 101 |
if ($entry == '') { // empty string means new line |
| 102 |
if (substr($text, -2) != '.' . $newLineChar) { |
| 103 |
$text .= '.' . $newLineChar; |
| 104 |
} |
| 105 |
} else { |
| 106 |
list($msg, $style) = $entry; |
| 107 |
$text .= $msg; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return $text; |
| 112 |
} |
| 113 |
} |
| 114 |
|