Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/debug/sfWebDebugPanelDoctrine.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/debug/sfWebDebugPanelDoctrine.class.php

Revision 26982, 4.9 kB (checked in by FabianLange, 4 years ago)

[1.2] backported r21879 fix for doctrine debug panel not html-escaping (fixes #7972)

Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) Jonathan H. Wage <jonwage@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfWebDebugPanelDoctrine adds a panel to the web debug toolbar with Doctrine information.
14  *
15  * @package    symfony
16  * @subpackage debug
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Jonathan H. Wage <jonwage@gmail.com>
19  * @version    SVN: $Id: sfWebDebugPanelDoctrine.class.php 11205 2008-08-27 16:24:17Z fabien $
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 }
Note: See TracBrowser for help on using the browser.