| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfWebDebugPanelDoctrine extends sfWebDebugPanel |
|---|
| 22 |
{ |
|---|
| 23 |
|
|---|
| 24 |
* Constructor. |
|---|
| 25 |
* |
|---|
| 26 |
* @param sfWebDebug $webDebug The web debut toolbar instance |
|---|
| 27 |
*/ |
|---|
| 28 |
public function __construct(sfWebDebug $webDebug) |
|---|
| 29 |
{ |
|---|
| 30 |
parent::__construct($webDebug); |
|---|
| 31 |
|
|---|
| 32 |
$this->webDebug->getEventDispatcher()->connect('debug.web.filter_logs', array($this, 'filterLogs')); |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
* Get the title/icon for the panel |
|---|
| 37 |
* |
|---|
| 38 |
* @return string $html |
|---|
| 39 |
*/ |
|---|
| 40 |
public function getTitle() |
|---|
| 41 |
{ |
|---|
| 42 |
if ($sqlLogs = $this->getSqlLogs()) |
|---|
| 43 |
{ |
|---|
| 44 |
return '<img src="'.$this->webDebug->getOption('image_root_path').'/database.png" alt="SQL queries" /> '.count($sqlLogs); |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* Get the verbal title of the panel |
|---|
| 50 |
* |
|---|
| 51 |
* @return string $title |
|---|
| 52 |
*/ |
|---|
| 53 |
public function getPanelTitle() |
|---|
| 54 |
{ |
|---|
| 55 |
return 'SQL queries'; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
* Get the html content of the panel |
|---|
| 60 |
* |
|---|
| 61 |
* @return string $html |
|---|
| 62 |
*/ |
|---|
| 63 |
public function getPanelContent() |
|---|
| 64 |
{ |
|---|
| 65 |
return ' |
|---|
| 66 |
<div id="sfWebDebugDatabaseLogs"> |
|---|
| 67 |
<ol><li>'.implode("</li>\n<li>", $this->getSqlLogs()).'</li></ol> |
|---|
| 68 |
</div> |
|---|
| 69 |
'; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
* Filter the logs to only include the entries from sfDoctrineLogger |
|---|
| 74 |
* |
|---|
| 75 |
* @param sfEvent $event |
|---|
| 76 |
* @param array $Logs |
|---|
| 77 |
* @return array $newLogs |
|---|
| 78 |
*/ |
|---|
| 79 |
public function filterLogs(sfEvent $event, $newSqlogs) |
|---|
| 80 |
{ |
|---|
| 81 |
$newLogs = array(); |
|---|
| 82 |
foreach ($newSqlogs as $newSqlog) |
|---|
| 83 |
{ |
|---|
| 84 |
if ('sfDoctrineLogger' != $newSqlog['type']) |
|---|
| 85 |
{ |
|---|
| 86 |
$newLogs[] = $newSqlog; |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
return $newLogs; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
* Hook to allow the loading of the Doctrine webdebug toolbar with the rest of the panels |
|---|
| 95 |
* |
|---|
| 96 |
* @param sfEvent $event |
|---|
| 97 |
* @return void |
|---|
| 98 |
*/ |
|---|
| 99 |
static public function listenToAddPanelEvent(sfEvent $event) |
|---|
| 100 |
{ |
|---|
| 101 |
$event->getSubject()->setPanel('db', new self($event->getSubject())); |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
* Build the sql logs and return them as an array |
|---|
| 106 |
* |
|---|
| 107 |
* @return array $newSqlogs |
|---|
| 108 |
*/ |
|---|
| 109 |
protected function getSqlLogs() |
|---|
| 110 |
{ |
|---|
| 111 |
$logs = array(); |
|---|
| 112 |
$bindings = array(); |
|---|
| 113 |
$i = 0; |
|---|
| 114 |
foreach ($this->webDebug->getLogger()->getLogs() as $log) |
|---|
| 115 |
{ |
|---|
| 116 |
if ('sfDoctrineLogger' != $log['type']) |
|---|
| 117 |
{ |
|---|
| 118 |
continue; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
if (preg_match('/^.*?(\b(?:SELECT|INSERT|UPDATE|DELETE)\b.*)$/', $log['message'], $match)) |
|---|
| 122 |
{ |
|---|
| 123 |
$logs[$i++] = self::formatSql($match[1]); |
|---|
| 124 |
$bindings[$i - 1] = array(); |
|---|
| 125 |
} |
|---|
| 126 |
else if (preg_match('/Binding (.*) at position (.+?) w\//', $log['message'], $match)) |
|---|
| 127 |
{ |
|---|
| 128 |
$bindings[$i - 1][] = $match[2].' = '.$match[1]; |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
foreach ($logs as $i => $log) |
|---|
| 133 |
{ |
|---|
| 134 |
if (count($bindings[$i])) |
|---|
| 135 |
{ |
|---|
| 136 |
$logs[$i] .= sprintf(' (%s)', implode(', ', $bindings[$i])); |
|---|
| 137 |
} |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
return $logs; |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
* Format a SQL with some colors on SQL keywords to make it more readable |
|---|
| 145 |
* |
|---|
| 146 |
* @param string $sql SQL string to format |
|---|
| 147 |
* @return string $newSql The new formatted SQL string |
|---|
| 148 |
*/ |
|---|
| 149 |
static protected function formatSql($sql) |
|---|
| 150 |
{ |
|---|
| 151 |
$color = "#990099"; |
|---|
| 152 |
$newSql = htmlspecialchars($sql, ENT_QUOTES, sfConfig::get('sf_charset')); |
|---|
| 153 |
$newSql = str_replace("SELECT ", "<span style=\"color: $color;\"><b>SELECT </b></span> ",$newSql); |
|---|
| 154 |
$newSql = str_replace("FROM ", "<span style=\"color: $color;\"><b>FROM </b></span>",$newSql); |
|---|
| 155 |
$newSql = str_replace(" LEFT JOIN ", "<span style=\"color: $color;\"><b> LEFT JOIN </b></span>",$newSql); |
|---|
| 156 |
$newSql = str_replace(" INNER JOIN ", "<span style=\"color: $color;\"><b> INNER JOIN </b></span>",$newSql); |
|---|
| 157 |
$newSql = str_replace(" WHERE ", "<span style=\"color: $color;\"><b> WHERE </b></span>",$newSql); |
|---|
| 158 |
$newSql = str_replace(" GROUP BY ", "<span style=\"color: $color;\"><b> GROUP BY </b></span>",$newSql); |
|---|
| 159 |
$newSql = str_replace(" HAVING ", "<span style=\"color: $color;\"><b> HAVING </b></span>",$newSql); |
|---|
| 160 |
$newSql = str_replace(" AS ", "<span style=\"color: $color;\"><b> AS </b></span> ",$newSql); |
|---|
| 161 |
$newSql = str_replace(" ON ", "<span style=\"color: $color;\"><b> ON </b></span>",$newSql); |
|---|
| 162 |
$newSql = str_replace(" ORDER BY ", "<span style=\"color: $color;\"><b> ORDER BY </b></span>",$newSql); |
|---|
| 163 |
$newSql = str_replace(" LIMIT ", "<span style=\"color: $color;\"><b> LIMIT </b></span>",$newSql); |
|---|
| 164 |
$newSql = str_replace(" OFFSET ", "<span style=\"color: $color;\"><b> OFFSET </b></span>",$newSql); |
|---|
| 165 |
|
|---|
| 166 |
return $newSql; |
|---|
| 167 |
} |
|---|
| 168 |
} |
|---|