1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12:
13:
14: 15: 16: 17: 18: 19:
20: class DibiFirePhpLogger extends DibiObject
21: {
22:
23: static public $maxQueries = 30;
24:
25:
26: static public $maxLength = 1000;
27:
28:
29: public $filter;
30:
31:
32: private static $fireTable = array(array('Time', 'SQL Statement', 'Rows', 'Connection'));
33:
34:
35:
36: 37: 38:
39: public static function isAvailable()
40: {
41: return isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP/');
42: }
43:
44:
45:
46: public function __construct($filter = NULL)
47: {
48: $this->filter = $filter ? (int) $filter : DibiEvent::QUERY;
49: }
50:
51:
52:
53: 54: 55: 56:
57: public function logEvent(DibiEvent $event)
58: {
59: if (headers_sent() || ($event->type & $this->filter) === 0 || count(self::$fireTable) > self::$maxQueries) {
60: return;
61: }
62:
63: self::$fireTable[] = array(
64: sprintf('%0.3f', $event->time * 1000),
65: strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql,
66: $event->result instanceof Exception ? 'ERROR' : $event->count,
67: $event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name')
68: );
69:
70: header('X-Wf-Protocol-dibi: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
71: header('X-Wf-dibi-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.2.0');
72: header('X-Wf-dibi-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
73:
74: $payload = json_encode(array(
75: array(
76: 'Type' => 'TABLE',
77: 'Label' => 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)',
78: ),
79: self::$fireTable,
80: ));
81: foreach (str_split($payload, 4990) as $num => $s) {
82: $num++;
83: header("X-Wf-dibi-1-1-d$num: |$s|\\");
84: }
85: header("X-Wf-dibi-1-1-d$num: |$s|");
86: }
87:
88: }
89: