ChromePhpHandler.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\ChromePHPHandler as BaseChromePhpHandler;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. /**
  15. * ChromePhpHandler.
  16. *
  17. * @author Christophe Coevoet <stof@notk.org>
  18. *
  19. * @final
  20. */
  21. class ChromePhpHandler extends BaseChromePhpHandler
  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. if (!preg_match(static::USER_AGENT_REGEX, $event->getRequest()->headers->get('User-Agent'))) {
  37. self::$sendHeaders = false;
  38. $this->headers = [];
  39. return;
  40. }
  41. $this->response = $event->getResponse();
  42. foreach ($this->headers as $header => $content) {
  43. $this->response->headers->set($header, $content);
  44. }
  45. $this->headers = [];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function sendHeader($header, $content): void
  51. {
  52. if (!self::$sendHeaders) {
  53. return;
  54. }
  55. if ($this->response) {
  56. $this->response->headers->set($header, $content);
  57. } else {
  58. $this->headers[$header] = $content;
  59. }
  60. }
  61. /**
  62. * Override default behavior since we check it in onKernelResponse.
  63. */
  64. protected function headersAccepted(): bool
  65. {
  66. return true;
  67. }
  68. }