FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Html.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Log\Handler;
4 
5 class Html extends File{
6 
7  public $hashFormat = "hash-*"; //!< Asterisk is replaced by hash of prio + message. Must be regex safe (besides the asterisk).
8  public $templateIndex = "
9  <tr id='ix-[id]' class='prio[prio] [hash]'>
10  <td class='time'><a href='#[id]'>[time]</a></td>
11  <td class='prio'><a href='#[id]'>[name] ([prio])</a></td>
12  <td class='message'>[message]</td>
13  <td class='label'><label for='[hash]' title='[hash]-count'></label></td>
14  </tr>";
15  //!< Index template ([id] of item, [time], [prio], [name] of prio, [message], and hash of prio + message are replaced).
16 
17  protected $_lastInsertId = null;
18 
19  protected function value($value){
20  if($value === null) return '<i>null</i>';
21  if($value === false) return '<i>false</i>';
22  if($value === true) return '<i>true</i>';
23  if($value === '') return '<i>[empty string]</i>';
24  return
25  (is_string($value) && preg_match('/(^\\s| {2,}|\\s$)/',$value) && ($value = htmlspecialchars($value))) ||
26  (strpos($value = htmlspecialchars(trim(str_replace("Array\n(",'array(',print_r($value,true)))),"\n") !== false)
27  ? "<pre>$value</pre>"
28  : $value;
29  }
30 
31  protected function format($prio,$context){
32  $this->_lastInsertId = $id = \Rsi\Str::random();
33  $result = "<tr><td colspan='2'><a name='$id'>{$this->separator}</a></td></tr>\n";
34  foreach($context as $key => $value){
35  if($special = substr($key,0,1) == '*') $key = substr($key,1);
36  $class = ($special ? 'special ' : '') . preg_replace('/\\W+/','-',$key);
37  if(($descr = $key) == 'date/time') $descr = "<a href='#ix-$id'>$descr</a>";
38  $html = null;
39  if(is_array($value)){
40  $html = "<table>\n";
41  $keys = null;
42  foreach($value as $record) if(!is_array($record) || ($keys && (array_keys($record) !== $keys))){
43  $keys = false;
44  break;
45  }
46  else $keys = array_keys($record);
47  if($keys){
48  $html .= "\t\t<tr><th></th><th>" . implode('</th><th>',array_map('htmlspecialchars',$keys)) . "</th></tr>\n";
49  foreach($value as $index => $record){
50  $html .= "\t\t<tr><th>" . htmlspecialchars($index) . "</th>";
51  foreach($record as $data) $html .=
52  "<td" . (is_scalar($data) && preg_match('/^[\\d\\-:\\s]+$/',$data) ? " class='number'" : '') . ">" . $this->value($data) . "</td>";
53  $html .= "</tr>\n";
54  }
55  }
56  else foreach($value as $key => $data) $html .=
57  "\t\t<tr><th>" . htmlspecialchars($key) . "</th><td>" . $this->value($data) . "</td></tr>\n";
58  $html .= "\t</table>";
59  }
60  if(strlen($result .= "<tr class='$class'>\n\t<th" . ($html ? " title='$descr'" : '') . ">$descr</th>\n\t<td>" . ($html ?: $this->value($value)) . "</td>\n</tr>\n") > $this->maxSize){
61  $result .= "<tr class='more'><td colspan='2'>...</td><tr>\n";
62  break;
63  }
64  }
65  return $result;
66  }
67 
68  public function add($prio,$message,$context){
69  parent::add($prio,$message,$context);
70  if($index = $this->indexFilename) \Rsi\File::write($index,strtr($this->templateIndex,[
71  '[id]' => $this->_lastInsertId,
72  '[time]' => $this->_lastInsertTime,
73  '[prio]' => $prio,
74  '[name]' => array_search($prio,$this->_log->constants()),
75  '[message]' => htmlspecialchars($message),
76  '[hash]' => str_replace('*',md5($prio . $message),$this->hashFormat)
77  ]),0666,true);
78  }
79 
80  protected function getIndexFilename(){
81  return $this->filename . '.index';
82  }
83 
84 }
Rsi\Fred\Log\Handler\Html
Definition: Html.php:5
Rsi\Fred\Log\Handler\Html\$_lastInsertId
$_lastInsertId
Definition: Html.php:17
Rsi\Fred\Log\Handler\Html\add
add($prio, $message, $context)
Definition: Html.php:68
Rsi\Fred\Log\Handler\Html\value
value($value)
Definition: Html.php:19
Rsi\Fred\Log\Handler
Definition: Console.php:3
Rsi\Fred\Log\Handler\Html\format
format($prio, $context)
Definition: Html.php:31
Rsi\Fred\Log\Handler\Html\$hashFormat
$hashFormat
Asterisk is replaced by hash of prio + message. Must be regex safe (besides the asterisk).
Definition: Html.php:7
Rsi\Fred\Log\Handler\Html\$templateIndex
$templateIndex
Index template ([id] of item, [time], [prio], [name] of prio, [message], and hash of prio + message a...
Definition: Html.php:8
Rsi\Fred\Log\Handler\File
Definition: File.php:5
Rsi\Fred\Log\Handler\Html\getIndexFilename
getIndexFilename()
Definition: Html.php:80