Completed
Push — master ( c2f224...2bc1cd )
by Oscar
03:33
created

DebugBar::debugBar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use DebugBar\DebugBar as Bar;
6
use DebugBar\StandardDebugBar;
7
use Psr7Middlewares\Middleware;
8
use Psr7Middlewares\Utils;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Middleware to render a debugbar in html responses.
14
 */
15
class DebugBar
16
{
17
    use Utils\HtmlInjectorTrait;
18
19
    private $debugBar;
20
21
    /**
22
     * Constructor. Set the debug bar.
23
     *
24
     * @param Bar|null $debugBar
25
     */
26
    public function __construct(Bar $debugBar = null)
27
    {
28
        if ($debugBar !== null) {
29
            $this->debugBar($debugBar);
30
        }
31
    }
32
33
    /**
34
     * Set the debug bar.
35
     *
36
     * @param Bar $debugBar
37
     * 
38
     * @return self
39
     */
40
    public function debugBar(Bar $debugBar)
41
    {
42
        $this->debugBar = $debugBar;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Execute the middleware.
49
     *
50
     * @param ServerRequestInterface $request
51
     * @param ResponseInterface      $response
52
     * @param callable               $next
53
     *
54
     * @return ResponseInterface
55
     */
56
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
57
    {
58
        if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
59
            throw new RuntimeException('This middleware needs FormatNegotiator executed before');
60
        }
61
62
        $ajax = Utils\Helpers::isAjax($request);
63
        $debugBar = $this->debugBar ?: new StandardDebugBar();
64
65
        //Redirection response
66
        if (Utils\Helpers::isRedirect($response)) {
67
            if (session_status() === PHP_SESSION_ACTIVE) {
68
                $debugBar->stackData();
69
            }
70
        
71
        //Html response
72
        } elseif (FormatNegotiator::getFormat($request) === 'html') {
73
            $renderer = $debugBar->getJavascriptRenderer();
74
75
            ob_start();
76
            echo '<style>';
77
            $renderer->dumpCssAssets();
78
            echo '</style>';
79
80
            echo '<script>';
81
            $renderer->dumpJsAssets();
82
            echo '</script>';
83
84
            echo $renderer->render(!$ajax);
85
86
            $response = $this->inject($response, ob_get_clean());
87
        
88
        //Ajax response
89
        } elseif ($ajax) {
90
            $headers = $debugBar->getDataAsHeaders();
91
92
            foreach ($headers as $name => $value) {
93
                $response = $response->withHeader($name, $value);
94
            }
95
        }
96
97
        return $next($request, $response);
98
    }
99
}
100