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