Skip to content

Commit f7aa6c0

Browse files
committed
[MonologBridge] Added the Response-aware ChromePhpHandler
1 parent 3236fc5 commit f7aa6c0

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Handler;
13+
14+
use Monolog\Handler\ChromePhpHandler as BaseChromePhpHandler;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
19+
/**
20+
* ChromePhpHandler.
21+
*
22+
* @author Christophe Coevoet <stof@notk.org>
23+
*/
24+
class ChromePhpHandler extends BaseChromePhpHandler
25+
{
26+
/**
27+
* @var array
28+
*/
29+
private $headers = array();
30+
31+
/**
32+
* @var Response
33+
*/
34+
private $response;
35+
36+
/**
37+
* Adds the headers to the response once it's created
38+
*/
39+
public function onKernelResponse(FilterResponseEvent $event)
40+
{
41+
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
42+
return;
43+
}
44+
45+
/* TODO: change this part once ChromePhpHandler has a way to detect the extension
46+
if (!preg_match('{\bFirePHP/\d+\.\d+\b}', $event->getRequest()->headers->get('User-Agent'))
47+
&& !$event->getRequest()->headers->has('X-FirePHP-Version')) {
48+
49+
$this->sendHeaders = false;
50+
$this->headers = array();
51+
52+
return;
53+
}
54+
*/
55+
56+
$this->response = $event->getResponse();
57+
foreach ($this->headers as $header => $content) {
58+
$this->response->headers->set($header, $content);
59+
}
60+
$this->headers = array();
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*/
66+
protected function sendHeader($header, $content)
67+
{
68+
if (!$this->sendHeaders) {
69+
return;
70+
}
71+
72+
if ($this->response) {
73+
$this->response->headers->set($header, $content);
74+
} else {
75+
$this->headers[$header] = $content;
76+
}
77+
}
78+
79+
/**
80+
* Override default behavior since we check it in onKernelResponse
81+
*/
82+
protected function headersAccepted()
83+
{
84+
return true;
85+
}
86+
}

0 commit comments

Comments
 (0)