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