Completed
Push — master ( 2d18a1...66c2ca )
by Thomas
14s queued 10s
created

ControllerExtension::onAfterInit()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 25
rs 8.8333
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());
0 ignored issues
show
Bug introduced by Thomas
The method setController() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as LeKoala\DebugBar\Collector\SilverStripeCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
            $debugbar->getCollector('silverstripe')->/** @scrutinizer ignore-call */ setController(Controller::curr());
Loading history...
22
23
            /** @var $timeData DebugBar\DataCollector\TimeDataCollector */
24
            $timeData = $debugbar->getCollector('time');
25
            if (!$timeData) {
0 ignored issues
show
introduced by Thomas
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
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) {
0 ignored issues
show
introduced by Thomas
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
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
0 ignored issues
show
Bug introduced by Robbie Averill
The type LeKoala\DebugBar\Extension\HTTPRequest was not found. Did you mean HTTPRequest? If so, make sure to prefix the type with \.
Loading history...
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) {
0 ignored issues
show
introduced by Thomas
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
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)
0 ignored issues
show
Unused Code introduced by Thomas
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

106
    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 Thomas
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

106
    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...
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) {
0 ignored issues
show
introduced by Thomas
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
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