FirePHPHandler.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Monolog\Handler;
  11. use Monolog\Handler\FirePHPHandler as BaseFirePHPHandler;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. /**
  15. * FirePHPHandler.
  16. *
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. *
  19. * @final
  20. */
  21. class FirePHPHandler extends BaseFirePHPHandler
  22. {
  23. private $headers = [];
  24. /**
  25. * @var Response
  26. */
  27. private $response;
  28. /**
  29. * Adds the headers to the response once it's created.
  30. */
  31. public function onKernelResponse(ResponseEvent $event)
  32. {
  33. if (!$event->isMasterRequest()) {
  34. return;
  35. }
  36. $request = $event->getRequest();
  37. if (!preg_match('{\bFirePHP/\d+\.\d+\b}', $request->headers->get('User-Agent'))
  38. && !$request->headers->has('X-FirePHP-Version')) {
  39. self::$sendHeaders = false;
  40. $this->headers = [];
  41. return;
  42. }
  43. $this->response = $event->getResponse();
  44. foreach ($this->headers as $header => $content) {
  45. $this->response->headers->set($header, $content);
  46. }
  47. $this->headers = [];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function sendHeader($header, $content): void
  53. {
  54. if (!self::$sendHeaders) {
  55. return;
  56. }
  57. if ($this->response) {
  58. $this->response->headers->set($header, $content);
  59. } else {
  60. $this->headers[$header] = $content;
  61. }
  62. }
  63. /**
  64. * Override default behavior since we check the user agent in onKernelResponse.
  65. */
  66. protected function headersAccepted(): bool
  67. {
  68. return true;
  69. }
  70. }