Passed
Push — master ( 60e77e...4fa0f7 )
by Thomas
02:04
created

ControllerExtension::onAfterInit()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 21
rs 9.2222
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
11
/**
12
 * A controller extension to log times and render the Debug Bar
13
 */
14
class ControllerExtension extends Extension
15
{
16
    public function onBeforeInit()
17
    {
18
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
19
            // We must set the current controller when it's available and before it's pushed out of stack
20
            /** @var \LeKoala\DebugBar\Collector\SilverStripeCollector $ssCollector */
21
            $ssCollector =  $debugbar->getCollector('silverstripe');
22
            $ssCollector->setController(Controller::curr());
23
24
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
25
            $timeData = $debugbar->getCollector('time');
26
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
27
                return;
28
            }
29
            if ($timeData->hasStartedMeasure('pre_request')) {
30
                $timeData->stopMeasure("pre_request");
31
            }
32
            $timeData->startMeasure("init", get_class($this->owner) . ' init');
33
        });
34
    }
35
36
    public function onAfterInit()
37
    {
38
        $result = DebugBar::includeRequirements();
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
39
40
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
41
            // Add the headers Collector
42
            if (!$debugbar->hasCollector('Headers') && DebugBar::config()->get('header_collector')) {
43
                $debugbar->addCollector(new HeaderCollector($this->owner));
44
            }
45
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
46
            $timeData = $debugbar->getCollector('time');
47
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
48
                return;
49
            }
50
            if ($timeData->hasStartedMeasure("cms_init")) {
51
                $timeData->stopMeasure("cms_init");
52
            }
53
            if ($timeData->hasStartedMeasure("init")) {
54
                $timeData->stopMeasure("init");
55
            }
56
            $timeData->startMeasure("handle", get_class($this->owner) . ' handle request');
57
        });
58
    }
59
60
    /**
61
     * @param HTTPRequest $request
0 ignored issues
show
Bug introduced by
The type LeKoala\DebugBar\Extension\HTTPRequest was not found. Did you mean HTTPRequest? If so, make sure to prefix the type with \.
Loading history...
62
     * @param string $action
63
     */
64
    public function beforeCallActionHandler($request, $action)
65
    {
66
        // If we don't have an action, getViewer will be called immediatly
67
        // If we have custom routes, request action is different than action
68
        $allParams     = $request->allParams();
69
        $requestAction = null;
70
        if (!empty($allParams['Action'])) {
71
            $requestAction = $allParams['Action'];
72
        }
73
        if (!$this->owner->hasMethod($action) || ($requestAction && $requestAction != $action)) {
74
            self::clearBuffer();
75
        }
76
77
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) {
78
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
79
            $timeData = $debugBar->getCollector('time');
80
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
81
                return;
82
            }
83
            if ($timeData->hasStartedMeasure("handle")) {
84
                $timeData->stopMeasure("handle");
85
            }
86
            $timeData->startMeasure("action", get_class($this->owner) . " action $action");
87
        });
88
    }
89
90
    /**
91
     * @param HTTPRequest $request
92
     * @param string $action
93
     * @param mixed $result
94
     */
95
    public function afterCallActionHandler($request, $action, $result)
0 ignored issues
show
Unused Code introduced by
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 ignore-unused  annotation

95
    public function afterCallActionHandler(/** @scrutinizer ignore-unused */ $request, $action, $result)

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...
Unused Code introduced by
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 ignore-unused  annotation

95
    public function afterCallActionHandler($request, $action, /** @scrutinizer ignore-unused */ $result)

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...
96
    {
97
        self::clearBuffer();
98
99
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($action) {
100
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
101
            $timeData = $debugBar->getCollector('time');
102
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
103
                return;
104
            }
105
            if ($timeData->hasStartedMeasure("action")) {
106
                $timeData->stopMeasure("action");
107
            }
108
            $timeData->startMeasure(
109
                "after_action",
110
                get_class($this->owner) . " after action $action"
111
            );
112
        });
113
    }
114
115
    protected static function clearBuffer()
116
    {
117
        if (!DebugBar::$bufferingEnabled) {
118
            return;
119
        }
120
        $buffer = ob_get_clean();
121
        if (!empty($buffer)) {
122
            unset($_REQUEST['debug_request']); // Disable further messages that we can't intercept
123
            SilverStripeCollector::setDebugData($buffer);
124
        }
125
    }
126
}
127