public
Description: Doctrine ORM & Zend Framework & ZFDebug
Homepage: http://www.danceric.net/2009/06/06/zfdebug-and-doctrine-orm
Clone URL: git://github.com/danceric/zfdebugdoctrine.git
100644 117 lines (102 sloc) 3.267 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* ZFDebug Doctrine ORM plugin
*
* @category Danceric
* @package Danceric_Controller
* @subpackage Plugins
*/
 
/**
* @category Danceric
* @package Danceric_Controller
* @subpackage Plugins
*/
class Danceric_Controller_Plugin_Debug_Plugin_Doctrine extends ZFDebug_Controller_Plugin_Debug_Plugin implements ZFDebug_Controller_Plugin_Debug_Plugin_Interface
{
    /**
* Contains plugin identifier name
*
* @var string
*/
    protected $_identifier = 'doctrine';
 
    /**
* @var array Doctrine connection profiler that will listen to events
*/
    protected $_profilers = array();
 
    /**
* Create ZFDebug_Controller_Plugin_Debug_Plugin_Variables
*
* @param Doctrine_Manager|array $options
* @return void
*/
    public function __construct(array $options = array())
    {
        if(!isset($options['manager']) || !count($options['manager'])) {
            if (Doctrine_Manager::getInstance()) {
                $options['manager'] = Doctrine_Manager::getInstance();
            }
        }
 
        foreach ($options['manager']->getIterator() as $connection) {
            $this->_profilers[$connection->getName()] = new Doctrine_Connection_Profiler();
            $connection->setListener($this->_profilers[$connection->getName()]);
        }
    }
 
    /**
* Gets identifier for this plugin
*
* @return string
*/
    public function getIdentifier()
    {
        return $this->_identifier;
    }
 
    /**
* Gets menu tab for the Debugbar
*
* @return string
*/
    public function getTab()
    {
        if (!$this->_profilers)
            return 'No Profiler';
 
        foreach ($this->_profilers as $profiler) {
            $time = 0;
            foreach ($profiler as $event) {
                $time += $event->getElapsedSecs();
            }
            $profilerInfo[] = $profiler->count() . ' in ' . round($time*1000, 2) . ' ms';
        }
        $html = implode(' / ', $profilerInfo);
 
        return $html;
    }
 
    /**
* Gets content panel for the Debugbar
*
* @return string
*/
    public function getPanel()
    {
        if (!$this->_profilers)
            return '';
 
        $html = '<h4>Database queries</h4>';
        
        foreach ($this->_profilers as $name => $profiler) {
                $html .= '<h4>Connection '.$name.'</h4><ol>';
                foreach ($profiler as $event) {
                    if (in_array($event->getName(), array('query', 'execute', 'exec'))) {
                        $info = htmlspecialchars($event->getQuery());
                    } else {
                        $info = '<em>' . htmlspecialchars($event->getName()) . '</em>';
                    }
 
                    $html .= '<li><strong>[' . round($event->getElapsedSecs()*1000, 2) . ' ms]</strong> ';
                    $html .= $info;
                
                    $params = $event->getParams();
                    if(!empty($params)) {
                        $html .= '<ul><em>bindings:</em> <li>'. implode('</li><li>', $params) . '</li></ul>';
                    }
                    $html .= '</li>';
                }
                $html .= '</ol>';
        }
 
        return $html;
    }
 
}