1 | <?php |
||||||
2 | |||||||
3 | namespace LeKoala\DebugBar\Extension; |
||||||
4 | |||||||
5 | use LeKoala\DebugBar\DebugBar; |
||||||
6 | use SilverStripe\CMS\Controllers\ContentController; |
||||||
0 ignored issues
–
show
|
|||||||
7 | use SilverStripe\Control\Controller; |
||||||
8 | use SilverStripe\Core\Extension; |
||||||
9 | use LeKoala\DebugBar\Collector\HeaderCollector; |
||||||
10 | use LeKoala\DebugBar\Collector\SilverStripeCollector; |
||||||
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 | /** @var \LeKoala\DebugBar\Collector\SilverStripeCollector $ssCollector */ |
||||||
22 | $ssCollector = $debugbar->getCollector('silverstripe'); |
||||||
23 | $ssCollector->setController(Controller::curr()); |
||||||
24 | |||||||
25 | /** @var DebugBar\DataCollector\TimeDataCollector $timeData */ |
||||||
26 | $timeData = $debugbar->getCollector('time'); |
||||||
27 | if (!$timeData) { |
||||||
0 ignored issues
–
show
|
|||||||
28 | return; |
||||||
29 | } |
||||||
30 | if ($timeData->hasStartedMeasure('pre_request')) { |
||||||
31 | $timeData->stopMeasure("pre_request"); |
||||||
32 | } |
||||||
33 | $timeData->startMeasure("init", get_class($this->owner) . ' init'); |
||||||
34 | }); |
||||||
35 | } |
||||||
36 | |||||||
37 | public function onAfterInit() |
||||||
38 | { |
||||||
39 | // Avoid requirements being called to early due to RootURLController |
||||||
40 | if ($this->owner instanceof ContentController) { |
||||||
41 | DebugBar::includeRequirements(); |
||||||
42 | } |
||||||
43 | |||||||
44 | DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
||||||
45 | // Add the headers Collector |
||||||
46 | if (!$debugbar->hasCollector('Headers') && DebugBar::config()->get('header_collector')) { |
||||||
47 | $debugbar->addCollector(new HeaderCollector($this->owner)); |
||||||
48 | } |
||||||
49 | /** @var DebugBar\DataCollector\TimeDataCollector $timeData */ |
||||||
50 | $timeData = $debugbar->getCollector('time'); |
||||||
51 | if (!$timeData) { |
||||||
0 ignored issues
–
show
|
|||||||
52 | return; |
||||||
53 | } |
||||||
54 | if ($timeData->hasStartedMeasure("cms_init")) { |
||||||
55 | $timeData->stopMeasure("cms_init"); |
||||||
56 | } |
||||||
57 | if ($timeData->hasStartedMeasure("init")) { |
||||||
58 | $timeData->stopMeasure("init"); |
||||||
59 | } |
||||||
60 | $timeData->startMeasure("handle", get_class($this->owner) . ' handle request'); |
||||||
61 | }); |
||||||
62 | } |
||||||
63 | |||||||
64 | /** |
||||||
65 | * @param HTTPRequest $request |
||||||
0 ignored issues
–
show
|
|||||||
66 | * @param string $action |
||||||
67 | */ |
||||||
68 | public function beforeCallActionHandler($request, $action) |
||||||
69 | { |
||||||
70 | // If we don't have an action, getViewer will be called immediatly |
||||||
71 | // If we have custom routes, request action is different than action |
||||||
72 | $allParams = $request->allParams(); |
||||||
73 | $requestAction = null; |
||||||
74 | if (!empty($allParams['Action'])) { |
||||||
75 | $requestAction = $allParams['Action']; |
||||||
76 | } |
||||||
77 | if (!$this->owner->hasMethod($action) || ($requestAction && $requestAction != $action)) { |
||||||
78 | self::clearBuffer(); |
||||||
79 | } |
||||||
80 | |||||||
81 | DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) { |
||||||
82 | /** @var DebugBar\DataCollector\TimeDataCollector $timeData */ |
||||||
83 | $timeData = $debugBar->getCollector('time'); |
||||||
84 | if (!$timeData) { |
||||||
0 ignored issues
–
show
|
|||||||
85 | return; |
||||||
86 | } |
||||||
87 | if ($timeData->hasStartedMeasure("handle")) { |
||||||
88 | $timeData->stopMeasure("handle"); |
||||||
89 | } |
||||||
90 | $timeData->startMeasure("action", get_class($this->owner) . " action $action"); |
||||||
91 | }); |
||||||
92 | } |
||||||
93 | |||||||
94 | /** |
||||||
95 | * @param HTTPRequest $request |
||||||
96 | * @param string $action |
||||||
97 | * @param mixed $result |
||||||
98 | */ |
||||||
99 | public function afterCallActionHandler($request, $action, $result) |
||||||
0 ignored issues
–
show
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
The parameter
$result is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||||
100 | { |
||||||
101 | self::clearBuffer(); |
||||||
102 | |||||||
103 | DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) { |
||||||
104 | /** @var DebugBar\DataCollector\TimeDataCollector $timeData */ |
||||||
105 | $timeData = $debugBar->getCollector('time'); |
||||||
106 | if (!$timeData) { |
||||||
0 ignored issues
–
show
|
|||||||
107 | return; |
||||||
108 | } |
||||||
109 | if ($timeData->hasStartedMeasure("action")) { |
||||||
110 | $timeData->stopMeasure("action"); |
||||||
111 | } |
||||||
112 | $timeData->startMeasure( |
||||||
113 | "after_action", |
||||||
114 | get_class($this->owner) . " after action $action" |
||||||
115 | ); |
||||||
116 | }); |
||||||
117 | } |
||||||
118 | |||||||
119 | protected static function clearBuffer() |
||||||
120 | { |
||||||
121 | if (!DebugBar::$bufferingEnabled) { |
||||||
122 | return; |
||||||
123 | } |
||||||
124 | $buffer = ob_get_clean(); |
||||||
125 | if (!empty($buffer)) { |
||||||
126 | unset($_REQUEST['debug_request']); // Disable further messages that we can't intercept |
||||||
127 | SilverStripeCollector::setDebugData($buffer); |
||||||
128 | } |
||||||
129 | } |
||||||
130 | } |
||||||
131 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths