Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 28999 2010-04-06 17:42:14Z Kris.Wallsmith $
20
 */
21
class sfWebDebugPanelDoctrine extends sfWebDebugPanel
22
{
23
  /**
24
   * Get the title/icon for the panel
25
   *
26
   * @return string $html
27
   */
28
  public function getTitle()
29
  {
30
    if ($events = $this->getDoctrineEvents())
31
    {
32
      return '<img src="'.$this->webDebug->getOption('image_root_path').'/database.png" alt="SQL queries" /> '.count($events);
33
    }
34
  }
35
 
36
  /**
37
   * Get the verbal title of the panel
38
   *
39
   * @return string $title
40
   */
41
  public function getPanelTitle()
42
  {
43
    return 'SQL queries';
44
  }
45
 
46
  /**
47
   * Get the html content of the panel
48
   *
49
   * @return string $html
50
   */
51
  public function getPanelContent()
52
  {
53
    return '
54
      <div id="sfWebDebugDatabaseLogs">
55
        <h3>Doctrine Version: '.Doctrine_Core::VERSION.'</h3>
56
        <ol>'.implode("\n", $this->getSqlLogs()).'</ol>
57
      </div>
58
    ';
59
  }
60
 
61
  /**
62
   * Listens to debug.web.load_panels and adds this panel.
63
   */
64
  static public function listenToAddPanelEvent(sfEvent $event)
65
  {
66
    $event->getSubject()->setPanel('db', new self($event->getSubject()));
67
  }
68
 
69
  /**
70
   * Returns an array of Doctrine query events.
71
   *
72
   * @return array
73
   */
74
  protected function getDoctrineEvents()
75
  {
76
    $databaseManager = sfContext::getInstance()->getDatabaseManager();
77
 
78
    $events = array();
79
    if ($databaseManager)
80
    {
81
      foreach ($databaseManager->getNames() as $name)
82
      {
83
        $database = $databaseManager->getDatabase($name);
84
        if ($database instanceof sfDoctrineDatabase && $profiler = $database->getProfiler())
85
        {
86
          foreach ($profiler->getQueryExecutionEvents() as $event)
87
          {
88
            $events[$event->getSequence()] = $event;
89
          }
90
        }
91
      }
92
    }
93
 
94
    // sequence events
95
    ksort($events);
96
 
97
    return $events;
98
  }
99
 
100
  /**
101
   * Builds the sql logs and returns them as an array.
102
   *
103
   * @return array
104
   */
105
  protected function getSqlLogs()
106
  {
107
    $logs = $this->webDebug->getLogger()->getLogs();
108
 
109
    $html = array();
110
    foreach ($this->getDoctrineEvents() as $i => $event)
111
    {
112
      $conn = $event->getInvoker() instanceof Doctrine_Connection ? $event->getInvoker() : $event->getInvoker()->getConnection();
113
      $params = sfDoctrineConnectionProfiler::fixParams($event->getParams());
114
      $query = $this->formatSql(htmlspecialchars($event->getQuery(), ENT_QUOTES, sfConfig::get('sf_charset')));
115
 
116
      // interpolate parameters
117
      foreach ($params as $param)
118
      {
119
        $param = htmlspecialchars($param, ENT_QUOTES, sfConfig::get('sf_charset'));
120
        $query = join(var_export(is_scalar($param) ? $param : (string) $param, true), explode('?', $query, 2));
121
      }
122
 
123
      // slow query
124
      if ($event->slowQuery && $this->getStatus() > sfLogger::NOTICE)
125
      {
126
        $this->setStatus(sfLogger::NOTICE);
127
      }
128
 
129
      // backtrace
130
      $backtrace = null;
131
      foreach ($logs as $i => $log)
132
      {
133
        if (!isset($log['debug_backtrace']) || !count($log['debug_backtrace']))
134
        {
135
          // backtrace disabled
136
          break;
137
        }
138
 
139
        if (false !== strpos($log['message'], $event->getQuery()))
140
        {
141
          // assume queries are being requested in order
142
          unset($logs[$i]);
143
          $backtrace = '&nbsp;'.$this->getToggleableDebugStack($log['debug_backtrace']);
144
          break;
145
        }
146
      }
147
 
148
      $html[] = sprintf('
149
        <li%s>
150
          <p class="sfWebDebugDatabaseQuery">%s</p>
151
          <div class="sfWebDebugDatabaseLogInfo">%ss, "%s" connection%s</div>
152
        </li>',
153
        $event->slowQuery ? ' class="sfWebDebugWarning"' : '',
154
        $query,
155
        number_format($event->getElapsedSecs(), 2),
156
        $conn->getName(),
157
        $backtrace
158
      );
159
    }
160
 
161
    return $html;
162
  }
163
}