AbstractTokenProcessor.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\Processor;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. /**
  14. * The base class for security token processors.
  15. *
  16. * @author Dany Maillard <danymaillard93b@gmail.com>
  17. * @author Igor Timoshenko <igor.timoshenko@i.ua>
  18. */
  19. abstract class AbstractTokenProcessor
  20. {
  21. /**
  22. * @var TokenStorageInterface
  23. */
  24. protected $tokenStorage;
  25. public function __construct(TokenStorageInterface $tokenStorage)
  26. {
  27. $this->tokenStorage = $tokenStorage;
  28. }
  29. abstract protected function getKey(): string;
  30. abstract protected function getToken(): ?TokenInterface;
  31. public function __invoke(array $record): array
  32. {
  33. $record['extra'][$this->getKey()] = null;
  34. if (null !== $token = $this->getToken()) {
  35. $record['extra'][$this->getKey()] = [
  36. 'username' => $token->getUsername(),
  37. 'authenticated' => $token->isAuthenticated(),
  38. 'roles' => $token->getRoleNames(),
  39. ];
  40. }
  41. return $record;
  42. }
  43. }