diff --git a/src/Resources/config/auditable.php b/src/Resources/config/auditable.php index d301aef7..97ff1fd5 100644 --- a/src/Resources/config/auditable.php +++ b/src/Resources/config/auditable.php @@ -63,7 +63,7 @@ ->args([new ReferenceConfigurator('simplethings_entityaudit.manager')]) ->set('simplethings_entityaudit.username_callable.token_storage', TokenStorageUsernameCallable::class) - ->args([new ReferenceConfigurator('service_container')]) + ->args([new ReferenceConfigurator('security.token_storage')]) ->set('simplethings_entityaudit.config', AuditConfiguration::class) ->public() diff --git a/src/User/TokenStorageUsernameCallable.php b/src/User/TokenStorageUsernameCallable.php index b00bb883..775aae43 100644 --- a/src/User/TokenStorageUsernameCallable.php +++ b/src/User/TokenStorageUsernameCallable.php @@ -14,20 +14,45 @@ namespace SimpleThings\EntityAudit\User; use Symfony\Component\DependencyInjection\Container; -use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; class TokenStorageUsernameCallable { /** - * NEXT_MAJOR: Inject the required services instead of using the container. - * - * @var Container + * @var TokenStorageInterface */ - private $container; + private $tokenStorage; - public function __construct(Container $container) + /** + * NEXT_MAJOR: remove Container type. + * + * @param Container|TokenStorageInterface $tokenStorageOrContainer + */ + public function __construct(object $tokenStorageOrContainer) { - $this->container = $container; + if ($tokenStorageOrContainer instanceof TokenStorageInterface) { + $this->tokenStorage = $tokenStorageOrContainer; + } elseif ($tokenStorageOrContainer instanceof Container) { + @trigger_error(sprintf( + 'Passing as argument 1 an instance of "%s" to "%s" is deprecated since' + .' sonata-project/entity-audit-bundle 1.x and will throw an "%s" in version 2.0.' + .' You must pass an instance of "%s" instead.', + Container::class, + __METHOD__, + \TypeError::class, + TokenStorageInterface::class + ), \E_USER_DEPRECATED); + + $this->tokenStorage = $tokenStorageOrContainer->get('security.token_storage'); + } else { + throw new \TypeError(sprintf( + 'Argument 1 passed to "%s()" must be an instance of "%s" or %s, instance of "%s" given.', + __METHOD__, + TokenStorageInterface::class, + Container::class, + \get_class($tokenStorageOrContainer) + )); + } } /** @@ -37,10 +62,10 @@ public function __construct(Container $container) */ public function __invoke() { - /** @var TokenInterface $token */ - $token = $this->container->get('security.token_storage')->getToken(); + $token = $this->tokenStorage->getToken(); + if (null !== $token && null !== $token->getUser()) { - // @phpstan-ignore-next-line + // @phpstan-ignore-next-line Use only "getUserIdentifier" when dropping support of Symfony < 5.3 return method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername(); } diff --git a/tests/DependencyInjection/SimpleThingsEntityAuditExtensionTest.php b/tests/DependencyInjection/SimpleThingsEntityAuditExtensionTest.php index 416e583f..eeb01d2a 100644 --- a/tests/DependencyInjection/SimpleThingsEntityAuditExtensionTest.php +++ b/tests/DependencyInjection/SimpleThingsEntityAuditExtensionTest.php @@ -38,7 +38,7 @@ public function testItRegistersDefaultServices(): void $this->assertContainerBuilderHasServiceDefinitionWithTag('simplethings_entityaudit.create_schema_listener', 'doctrine.event_subscriber', ['connection' => 'default']); $this->assertContainerBuilderHasService('simplethings_entityaudit.username_callable.token_storage', 'SimpleThings\EntityAudit\User\TokenStorageUsernameCallable'); - $this->assertContainerBuilderHasServiceDefinitionWithArgument('simplethings_entityaudit.username_callable.token_storage', 0, 'service_container'); + $this->assertContainerBuilderHasServiceDefinitionWithArgument('simplethings_entityaudit.username_callable.token_storage', 0, 'security.token_storage'); $this->assertContainerBuilderHasService('simplethings_entityaudit.config', 'SimpleThings\EntityAudit\AuditConfiguration'); $this->assertContainerBuilderHasServiceDefinitionWithMethodCall('simplethings_entityaudit.config', 'setAuditedEntityClasses', ['%simplethings.entityaudit.audited_entities%']);