I have tried to inject security.token_storage
service via CompilerPass
, I do not have errors and user is currently logged, however when I access the TokenStorage, $tokenStorage->getToken()
I got a null response.
I checked these links to reference, but I cannot achieve to get a valid token.
How to inject shared service from compiler pass in Symfony and Access currently logged in user in UserRepository in Sylius
Do you an idea why it returns null response?
- Sylius version: 1.4 and Symfony 4.4
Bundle class:
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ModifyRepositoryPass());
}
ModifyRepositoryPass class:
class ModifyRepositoryPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container
->getDefinition('sylius.repository.order')
->addMethodCall('getTokenStorage', array(
new Reference('security.token_storage')
));
}
}
Get token:
class OrderRepository extends BaseOrderRepository
{
/** @var TokenStorageInterface */
protected $tokenStorage;
public function getTokenStorage(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function createListFilter()
{
$token = $this->tokenStorage->getToken()); // NULL token
}