vendor/symfony/security-core/Authentication/Token/Storage/TokenStorage.php line 37

Open in your IDE?
  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\Component\Security\Core\Authentication\Token\Storage;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Contracts\Service\ResetInterface;
  13. /**
  14. * TokenStorage contains a TokenInterface.
  15. *
  16. * It gives access to the token representing the current user authentication.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20. */
  21. class TokenStorage implements TokenStorageInterface, ResetInterface
  22. {
  23. private $token;
  24. private $initializer;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getToken()
  29. {
  30. if ($initializer = $this->initializer) {
  31. $this->initializer = null;
  32. $initializer();
  33. }
  34. return $this->token;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function setToken(?TokenInterface $token = null)
  40. {
  41. if ($token) {
  42. // ensure any initializer is called
  43. $this->getToken();
  44. // @deprecated since Symfony 5.3
  45. if (!method_exists($token, 'getUserIdentifier')) {
  46. trigger_deprecation('symfony/security-core', '5.3', 'Not implementing method "getUserIdentifier(): string" in token class "%s" is deprecated. This method will replace "getUsername()" in Symfony 6.0.', get_debug_type($token));
  47. }
  48. }
  49. $this->initializer = null;
  50. $this->token = $token;
  51. }
  52. public function setInitializer(?callable $initializer): void
  53. {
  54. $this->initializer = $initializer;
  55. }
  56. public function reset()
  57. {
  58. $this->setToken(null);
  59. }
  60. }