/
extension.driver.php
241 lines (191 loc) · 8.15 KB
/
extension.driver.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
class Extension_Firebug_Profiler extends Extension {
private $params = array();
/*-------------------------------------------------------------------------
Extension definition
-------------------------------------------------------------------------*/
public function about() {
return array(
'name' => 'Firebug Profiler',
'version' => '1.2',
'release-date' => '2010-01-06',
'author' => array(
'name' => 'Nick Dunn',
'website' => 'http://airlock.com'
),
'description' => 'View Symphony profile and debug information in Firebug.'
);
}
public function install(){
$this->_Parent->Configuration->set('enabled', 'yes', 'firebug_profiler');
$this->_Parent->saveConfig();
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'CustomActions',
'callback' => 'toggleFirebugProfiler'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendOutputPreGenerate',
'callback' => 'frontendOutputPreGenerate'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendOutputPostGenerate',
'callback' => 'frontendOutputPostGenerate'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendPageResolved',
'callback' => 'frontendPageResolved'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsResolve',
'callback' => 'frontendParamsResolve'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsPostResolve',
'callback' => 'frontendParamsPostResolve'
)
);
}
public function appendPreferences($context){
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', 'Firebug Profiler'));
$label = Widget::Label();
$input = Widget::Input('settings[firebug_profiler][enabled]', 'yes', 'checkbox');
if($this->_Parent->Configuration->get('enabled', 'firebug_profiler') == 'yes') $input->setAttribute('checked', 'checked');
$label->setValue($input->generate() . ' Send XML of each Data Source and Event');
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'Enable this option to send the XML fragments for each Data Source and Event to the Firebug console. This can add several KB to each page request, depending on the XML size.', array('class' => 'help')));
$context['wrapper']->appendChild($group);
}
public function savePreferences($context){
if(!is_array($context['settings'])) $context['settings'] = array('firebug_profiler' => array('enabled' => 'no'));
elseif(!isset($context['settings']['firebug_profiler'])){
$context['settings']['firebug_profiler'] = array('enabled' => 'no');
}
}
public function toggleFirebugProfiler($context){
if($_REQUEST['action'] == 'toggle-firebug-profiler'){
$value = ($this->_Parent->Configuration->get('enabled', 'firebug_profiler') == 'no' ? 'yes' : 'no');
$this->_Parent->Configuration->set('enabled', $value, 'firebug_profiler');
$this->_Parent->saveConfig();
redirect((isset($_REQUEST['redirect']) ? URL . '/symphony' . $_REQUEST['redirect'] : $this->_Parent->getCurrentPageURL() . '/'));
}
}
/*-------------------------------------------------------------------------
Delegates:
-------------------------------------------------------------------------*/
public function frontendOutputPreGenerate($context) {
$this->xml = $context['xml'];
}
public function frontendOutputPostGenerate($context) {
// don't output anything for unauthenticated users
if (!Frontend::instance()->isLoggedIn()) return;
require_once(EXTENSIONS . '/firebug_profiler/lib/FirePHPCore/FirePHP.class.php');
$firephp = FirePHP::getInstance(true);
$events = Frontend::instance()->Profiler->retrieveGroup('Events');
$datasources = Frontend::instance()->Profiler->retrieveGroup('Datasource');
$xml_generation = Frontend::instance()->Profiler->retrieveByMessage('XML Generation');
$dbstats = Frontend::instance()->Database->getStatistics();
// Profile group
$firephp->group('Profile', array('Collapsed' => false));
$table = array();
$table[] = array('', '');
foreach(Frontend::instance()->Profiler->retrieveGroup('General') as $profile) {
$table[] = array($profile[0], $profile[1] . 's');
}
$firephp->table('Page Building', $table);
$event_total = 0;
foreach($events as $r) $event_total += $r[1];
$ds_total = 0;
foreach($datasources as $r) $ds_total += $r[1];
$table = array();
$table[] = array('', '');
$table[] = array(__('Total Database Queries'), $dbstats['queries']);
if (count($dbstats['slow-queries']) > 0) {
$table[] = array(__('Slow Queries (> 0.09s)'), count($dbstats['slow-queries']) . 's');
}
$table[] = array(__('Total Time Spent on Queries'), $dbstats['total-query-time'] . 's');
$table[] = array(__('Time Triggering All Events'), $event_total) . 's';
$table[] = array(__('Time Running All Data Sources'), $ds_total . 's');
$table[] = array(__('XML Generation Function'), $xml_generation[1] . 's');
// $table[] = array(__('XSLT Generation'), $xsl_transformation[1]); not available for this delegate
$total = Frontend::instance()->Profiler->retrieveTotalRunningTime();
$table[] = array(__('Output Creation Time'), $total);
$table[] = array(__('Total Memory Usage'), General::formatFilesize(Frontend::instance()->Profiler->retrieveTotalMemoryUsage()));
$firephp->table('Page Output (' . $total . 's, ' . $dbstats['queries'] .' queries, ' . General::formatFilesize(Frontend::instance()->Profiler->retrieveTotalMemoryUsage()) . ')', $table);
if (count($datasources) > 0) {
$table = array();
$table[] = array('Data Source', 'Time', 'Queries');
foreach($datasources as $profile) {
$table[] = array($profile[0], $profile[1] . 's', $profile[4]);
}
$firephp->table('Data Sources', $table);
}
if (count($events) > 0) {
$table = array();
$table[] = array('Event', 'Time', 'Queries');
foreach($events as $profile) {
$table[] = array($profile[0], $profile[1] . 's', $profile[4]);
}
$firephp->table('Events', $table);
}
$firephp->groupEnd();
// Debug group
$xml = simplexml_load_string($this->xml);
$firephp->group('Debug', array('Collapsed' => false));
if ($this->_Parent->Configuration->get('enabled', 'firebug_profiler') == 'yes') {
$table = array();
$table[] = array('Event', 'XML');
$xml_events = $xml->xpath('/data/events/*');
if (count($xml_events) > 0) {
foreach($xml_events as $event) {
$table[] = array($event->getName(), $event->asXML());
}
$firephp->table('Events (' . count($xml_events) .')', $table);
}
$table = array();
$table[] = array('Data Source', 'Entries', 'XML');
$xml_datasources = $xml->xpath('/data/*[name() != "events"]');
if (count($xml_datasources) > 0) {
foreach($xml_datasources as $ds) {
$entries = $ds->xpath('entry[@id]');
$table[] = array($ds->getName(), count($entries), $ds->asXML());
}
$firephp->table('Data Sources (' . count($xml_datasources) .')', $table);
}
}
$param_table = array();
$param_table[] = array('Parameter', 'Value');
foreach($this->params as $name => $value) {
if ($name == 'root') continue;
$param_table[] = array('$' . trim($name), ($value == null) ? '' : $value);
}
$firephp->table('Page Parameters', $param_table);
$firephp->groupEnd();
}
public function frontendParamsResolve($context) {
$this->params = $context['params'];
}
public function frontendParamsPostResolve($context) {
$this->params = array_merge($context['params'], $this->params);
}
}