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
|
|
|
private $captureAjax = true; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. Set the debug bar. |
24
|
|
|
* |
25
|
|
|
* @param Bar|null $debugBar |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Bar $debugBar = null) |
28
|
|
|
{ |
29
|
|
|
if ($debugBar !== null) { |
30
|
|
|
$this->debugBar($debugBar); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set the debug bar. |
36
|
|
|
* |
37
|
|
|
* @param Bar $debugBar |
38
|
|
|
* |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
public function debugBar(Bar $debugBar) |
42
|
|
|
{ |
43
|
|
|
$this->debugBar = $debugBar; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Configure whether capture ajax requests to send the data with headers |
50
|
|
|
* |
51
|
|
|
* @param bool $captureAjax |
52
|
|
|
* |
53
|
|
|
* @return self |
54
|
|
|
*/ |
55
|
|
|
public function captureAjax($captureAjax = true) |
56
|
|
|
{ |
57
|
|
|
$this->captureAjax = $captureAjax; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Execute the middleware. |
64
|
|
|
* |
65
|
|
|
* @param ServerRequestInterface $request |
66
|
|
|
* @param ResponseInterface $response |
67
|
|
|
* @param callable $next |
68
|
|
|
* |
69
|
|
|
* @return ResponseInterface |
70
|
|
|
*/ |
71
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
72
|
|
|
{ |
73
|
|
|
if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { |
74
|
|
|
throw new RuntimeException('This middleware needs FormatNegotiator executed before'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$ajax = Utils\Helpers::isAjax($request); |
78
|
|
|
$debugBar = $this->debugBar ?: new StandardDebugBar(); |
79
|
|
|
|
80
|
|
|
//Redirection response |
81
|
|
|
if (Utils\Helpers::isRedirect($response)) { |
82
|
|
|
if ($debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { |
83
|
|
|
$debugBar->stackData(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//Html response |
87
|
|
|
} elseif (FormatNegotiator::getFormat($request) === 'html') { |
88
|
|
|
$renderer = $debugBar->getJavascriptRenderer(); |
89
|
|
|
|
90
|
|
|
ob_start(); |
91
|
|
|
echo '<style>'; |
92
|
|
|
$renderer->dumpCssAssets(); |
93
|
|
|
echo '</style>'; |
94
|
|
|
|
95
|
|
|
echo '<script>'; |
96
|
|
|
$renderer->dumpJsAssets(); |
97
|
|
|
echo '</script>'; |
98
|
|
|
|
99
|
|
|
echo $renderer->render(!$ajax); |
100
|
|
|
|
101
|
|
|
$response = $this->inject($response, ob_get_clean()); |
102
|
|
|
|
103
|
|
|
//Ajax response |
104
|
|
|
} elseif ($ajax && $this->captureAjax) { |
105
|
|
|
$headers = $debugBar->getDataAsHeaders(); |
106
|
|
|
|
107
|
|
|
foreach ($headers as $name => $value) { |
108
|
|
|
$response = $response->withHeader($name, $value); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $next($request, $response); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|