vendor/contao/core-bundle/src/Monolog/ContaoTableProcessor.php line 103

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Monolog;
  11. use Contao\CoreBundle\Routing\ScopeMatcher;
  12. use Monolog\Logger;
  13. use Monolog\Processor\ProcessorInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. class ContaoTableProcessor implements ProcessorInterface
  18. {
  19.     /**
  20.      * @var RequestStack
  21.      */
  22.     private $requestStack;
  23.     /**
  24.      * @var TokenStorageInterface
  25.      */
  26.     private $tokenStorage;
  27.     /**
  28.      * @var ScopeMatcher
  29.      */
  30.     private $scopeMatcher;
  31.     /**
  32.      * @internal Do not inherit from this class; decorate the "contao.monolog.processor" service instead
  33.      */
  34.     public function __construct(RequestStack $requestStackTokenStorageInterface $tokenStorageScopeMatcher $scopeMatcher)
  35.     {
  36.         $this->requestStack $requestStack;
  37.         $this->tokenStorage $tokenStorage;
  38.         $this->scopeMatcher $scopeMatcher;
  39.     }
  40.     /**
  41.      * Move the Contao context into the "extra" section.
  42.      */
  43.     public function __invoke(array $record): array
  44.     {
  45.         if (!isset($record['context']['contao']) || !$record['context']['contao'] instanceof ContaoContext) {
  46.             return $record;
  47.         }
  48.         $context $record['context']['contao'];
  49.         $request $this->requestStack->getCurrentRequest();
  50.         $level $record['level'] ?? 0;
  51.         $this->updateAction($context$level);
  52.         $this->updateBrowser($context$request);
  53.         $this->updateUsername($context);
  54.         $this->updateSource($context$request);
  55.         $record['extra']['contao'] = $context;
  56.         unset($record['context']['contao']);
  57.         return $record;
  58.     }
  59.     private function updateAction(ContaoContext $contextint $level): void
  60.     {
  61.         if (null !== $context->getAction()) {
  62.             return;
  63.         }
  64.         if ($level >= Logger::ERROR) {
  65.             $context->setAction(ContaoContext::ERROR);
  66.         } else {
  67.             $context->setAction(ContaoContext::GENERAL);
  68.         }
  69.     }
  70.     private function updateBrowser(ContaoContext $contextRequest $request null): void
  71.     {
  72.         if (null !== $context->getBrowser()) {
  73.             return;
  74.         }
  75.         $context->setBrowser(null === $request 'N/A' : (string) $request->server->get('HTTP_USER_AGENT'));
  76.     }
  77.     private function updateUsername(ContaoContext $context): void
  78.     {
  79.         if (null !== $context->getUsername()) {
  80.             return;
  81.         }
  82.         $token $this->tokenStorage->getToken();
  83.         $context->setUsername(null === $token 'N/A' $token->getUsername());
  84.     }
  85.     private function updateSource(ContaoContext $contextRequest $request null): void
  86.     {
  87.         if (null !== $context->getSource()) {
  88.             return;
  89.         }
  90.         $context->setSource(null !== $request && $this->scopeMatcher->isBackendRequest($request) ? 'BE' 'FE');
  91.     }
  92. }