HttpCodeActivationStrategy.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 certain HTTP codes.
  17. *
  18. * @author Shaun Simmons <shaun@envysphere.com>
  19. * @author Pierrick Vignand <pierrick.vignand@gmail.com>
  20. *
  21. * @final
  22. */
  23. class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy implements ActivationStrategyInterface
  24. {
  25. private $inner;
  26. private $exclusions;
  27. private $requestStack;
  28. /**
  29. * @param array $exclusions each exclusion must have a "code" and "urls" keys
  30. * @param ActivationStrategyInterface|int|string $inner an ActivationStrategyInterface to decorate
  31. */
  32. public function __construct(RequestStack $requestStack, array $exclusions, $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. foreach ($exclusions as $exclusion) {
  40. if (!\array_key_exists('code', $exclusion)) {
  41. throw new \LogicException('An exclusion must have a "code" key.');
  42. }
  43. if (!\array_key_exists('urls', $exclusion)) {
  44. throw new \LogicException('An exclusion must have a "urls" key.');
  45. }
  46. }
  47. $this->inner = $inner;
  48. $this->requestStack = $requestStack;
  49. $this->exclusions = $exclusions;
  50. }
  51. public function isHandlerActivated(array $record): bool
  52. {
  53. $isActivated = $this->inner->isHandlerActivated($record);
  54. if (
  55. $isActivated
  56. && isset($record['context']['exception'])
  57. && $record['context']['exception'] instanceof HttpException
  58. && ($request = $this->requestStack->getMasterRequest())
  59. ) {
  60. foreach ($this->exclusions as $exclusion) {
  61. if ($record['context']['exception']->getStatusCode() !== $exclusion['code']) {
  62. continue;
  63. }
  64. if (\count($exclusion['urls'])) {
  65. return !preg_match('{('.implode('|', $exclusion['urls']).')}i', $request->getPathInfo());
  66. }
  67. return false;
  68. }
  69. }
  70. return $isActivated;
  71. }
  72. }