NotFoundActivationStrategy.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\FingersCrossed;
  11. use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
  12. use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\Exception\HttpException;
  15. /**
  16. * Activation strategy that ignores 404s for certain URLs.
  17. *
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Pierrick Vignand <pierrick.vignand@gmail.com>
  21. *
  22. * @final
  23. */
  24. class NotFoundActivationStrategy extends ErrorLevelActivationStrategy implements ActivationStrategyInterface
  25. {
  26. private $inner;
  27. private $exclude;
  28. private $requestStack;
  29. /**
  30. * @param ActivationStrategyInterface|int|string $inner an ActivationStrategyInterface to decorate
  31. */
  32. public function __construct(RequestStack $requestStack, array $excludedUrls, $inner)
  33. {
  34. if (!$inner instanceof ActivationStrategyInterface) {
  35. trigger_deprecation('symfony/monolog-bridge', '5.2', 'Passing an actionLevel (int|string) as constructor\'s 3rd argument of "%s" is deprecated, "%s" expected.', __CLASS__, ActivationStrategyInterface::class);
  36. $actionLevel = $inner;
  37. $inner = new ErrorLevelActivationStrategy($actionLevel);
  38. }
  39. $this->inner = $inner;
  40. $this->requestStack = $requestStack;
  41. $this->exclude = '{('.implode('|', $excludedUrls).')}i';
  42. }
  43. public function isHandlerActivated(array $record): bool
  44. {
  45. $isActivated = $this->inner->isHandlerActivated($record);
  46. if (
  47. $isActivated
  48. && isset($record['context']['exception'])
  49. && $record['context']['exception'] instanceof HttpException
  50. && 404 == $record['context']['exception']->getStatusCode()
  51. && ($request = $this->requestStack->getMasterRequest())
  52. ) {
  53. return !preg_match($this->exclude, $request->getPathInfo());
  54. }
  55. return $isActivated;
  56. }
  57. }