Completed
Pull Request — master (#47)
by Robbie
01:29
created

RequestFilter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 4
dl 0
loc 80
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A preRequest() 0 19 3
C postRequest() 0 46 13
1
<?php
2
3
namespace LeKoala\DebugBar;
4
5
use LeKoala\DebugBar\DebugBar;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\HTTPResponse;
9
use SilverStripe\Control\RequestFilter as BaseRequestFilter;
10
11
/**
12
 * A request filter to log pre request time
13
 */
14
class RequestFilter implements BaseRequestFilter
0 ignored issues
show
Deprecated Code introduced by
The interface SilverStripe\Control\RequestFilter has been deprecated with message: 4.0..5.0 Use HTTPMiddleware instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
15
{
16
17
    /**
18
     * Filter executed before a request processes
19
     *
20
     * {@inheritDoc}
21
     */
22
    public function preRequest(HTTPRequest $request)
0 ignored issues
show
Coding Style introduced by
preRequest uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
23
    {
24
        DebugBar::withDebugBar(function (DebugBar\DebugBar $debugbar) {
25
            /* @var $timeData DebugBar\DataCollector\TimeDataCollector */
26
            $timeData = $debugbar['time'];
27
            if (!$timeData) {
28
                return;
29
            }
30
            if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
31
                $timeData = $debugbar['time'];
32
                $timeData->addMeasure(
33
                    "framework boot",
34
                    $_SERVER['REQUEST_TIME_FLOAT'],
35
                    microtime(true)
36
                );
37
            }
38
            $timeData->startMeasure("pre_request", "pre request");
39
        });
40
    }
41
42
    /**
43
     * Filter executed AFTER a request
44
     *
45
     * {@inheritDoc}
46
     */
47
    public function postRequest(HTTPRequest $request, HTTPResponse $response)
48
    {
49
        $debugbar = DebugBar::getDebugBar();
50
        if (!$debugbar) {
51
            return;
52
        }
53
54
        // All queries have been displayed
55
        if (DebugBar::getShowQueries()) {
56
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method postRequest() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
57
        }
58
59
        $script = DebugBar::renderDebugBar();
60
61
        // If the bar is not renderable, return early
62
        if (!$script) {
63
            return;
64
        }
65
66
        // Inject init script into the HTML response
67
        $body = $response->getBody();
68
        if (strpos($body, '</body>') !== false) {
69
            $body = str_replace('</body>', $script . '</body>', $body);
70
            $response->setBody($body);
71
        }
72
73
        // Ajax support
74
        if (Director::is_ajax() && !headers_sent()) {
75
            if (DebugBar::isAdminUrl() && !DebugBar::config()->enabled_in_admin) {
76
                return;
77
            }
78
            // Skip anything that is not a GET request
79
            if (!$request->isGET()) {
80
                return;
81
            }
82
            // Always enable in admin because everything is mostly loaded through ajax
83
            if (DebugBar::config()->ajax || DebugBar::isAdminUrl()) {
84
                $headers = $debugbar->getDataAsHeaders();
85
86
                // Prevent throwing js errors in case header size is too large
87
                if (is_array($headers)) {
88
                    $debugbar->sendDataInHeaders();
89
                }
90
            }
91
        }
92
    }
93
}
94