1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\DebugBar\Extension; |
4
|
|
|
|
5
|
|
|
use LeKoala\DebugBar\Collector\HeaderCollector; |
6
|
|
|
use LeKoala\DebugBar\Collector\SilverStripeCollector; |
7
|
|
|
use LeKoala\DebugBar\DebugBar; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\Core\Extension; |
10
|
|
|
use SilverStripe\Security\Security; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A controller extension to log times and render the Debug Bar |
14
|
|
|
*/ |
15
|
|
|
class ControllerExtension extends Extension |
16
|
|
|
{ |
17
|
|
|
public function onBeforeInit() |
18
|
|
|
{ |
19
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
20
|
|
|
// We must set the current controller when it's available and before it's pushed out of stack |
21
|
|
|
$debugbar->getCollector('silverstripe')->setController(Controller::curr()); |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** @var $timeData DebugBar\DataCollector\TimeDataCollector */ |
24
|
|
|
$timeData = $debugbar->getCollector('time'); |
25
|
|
|
if (!$timeData) { |
|
|
|
|
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
if ($timeData->hasStartedMeasure('pre_request')) { |
29
|
|
|
$timeData->stopMeasure("pre_request"); |
30
|
|
|
} |
31
|
|
|
$timeData->startMeasure("init", get_class($this->owner) . ' init'); |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function onAfterInit() |
36
|
|
|
{ |
37
|
|
|
// On Security, onAfterInit is called before init() in your Page method |
38
|
|
|
// jQuery is most likely not included yet |
39
|
|
|
if (!$this->owner instanceof Security) { |
40
|
|
|
DebugBar::includeRequirements(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
44
|
|
|
// Add the headers Collector |
45
|
|
|
if (!$debugbar->hasCollector('Headers') && DebugBar::config()->get('header_collector')) { |
46
|
|
|
$debugbar->addCollector(new HeaderCollector($this->owner)); |
47
|
|
|
} |
48
|
|
|
/** @var $timeData DebugBar\DataCollector\TimeDataCollector */ |
49
|
|
|
$timeData = $debugbar->getCollector('time'); |
50
|
|
|
if (!$timeData) { |
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
if ($timeData->hasStartedMeasure("cms_init")) { |
54
|
|
|
$timeData->stopMeasure("cms_init"); |
55
|
|
|
} |
56
|
|
|
if ($timeData->hasStartedMeasure("init")) { |
57
|
|
|
$timeData->stopMeasure("init"); |
58
|
|
|
} |
59
|
|
|
$timeData->startMeasure("handle", get_class($this->owner) . ' handle request'); |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param HTTPRequest $request |
|
|
|
|
65
|
|
|
* @param string $action |
66
|
|
|
*/ |
67
|
|
|
public function beforeCallActionHandler($request, $action) |
68
|
|
|
{ |
69
|
|
|
// This could be called twice |
70
|
|
|
// TODO: check if we can remove this safely |
71
|
|
|
if ($this->owner->beforeCallActionHandlerCalled) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// If we don't have an action, getViewer will be called immediatly |
76
|
|
|
// If we have custom routes, request action is different than action |
77
|
|
|
$allParams = $request->allParams(); |
78
|
|
|
$requestAction = null; |
79
|
|
|
if (!empty($allParams['Action'])) { |
80
|
|
|
$requestAction = $allParams['Action']; |
81
|
|
|
} |
82
|
|
|
if (!$this->owner->hasMethod($action) || ($requestAction && $requestAction != $action)) { |
83
|
|
|
self::clearBuffer(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) { |
87
|
|
|
/** @var $timeData DebugBar\DataCollector\TimeDataCollector */ |
88
|
|
|
$timeData = $debugBar->getCollector('time'); |
89
|
|
|
if (!$timeData) { |
|
|
|
|
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
if ($timeData->hasStartedMeasure("handle")) { |
93
|
|
|
$timeData->stopMeasure("handle"); |
94
|
|
|
} |
95
|
|
|
$timeData->startMeasure("action", get_class($this->owner) . " action $action"); |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
$this->owner->beforeCallActionHandlerCalled = true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param HTTPRequest $request |
103
|
|
|
* @param string $action |
104
|
|
|
* @param mixed $result |
105
|
|
|
*/ |
106
|
|
|
public function afterCallActionHandler($request, $action, $result) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
self::clearBuffer(); |
109
|
|
|
|
110
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) { |
111
|
|
|
/** @var $timeData DebugBar\DataCollector\TimeDataCollector */ |
112
|
|
|
$timeData = $debugBar->getCollector('time'); |
113
|
|
|
if (!$timeData) { |
|
|
|
|
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
if ($timeData->hasStartedMeasure("action")) { |
117
|
|
|
$timeData->stopMeasure("action"); |
118
|
|
|
} |
119
|
|
|
$timeData->startMeasure( |
120
|
|
|
"after_action", |
121
|
|
|
get_class($this->owner) . " after action $action" |
122
|
|
|
); |
123
|
|
|
}); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected static function clearBuffer() |
127
|
|
|
{ |
128
|
|
|
if (!DebugBar::$bufferingEnabled) { |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
$buffer = ob_get_clean(); |
132
|
|
|
if (!empty($buffer)) { |
133
|
|
|
unset($_REQUEST['debug_request']); // Disable further messages that we can't intercept |
134
|
|
|
SilverStripeCollector::setDebugData($buffer); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|