Reputation: 35
I have a service called TennantContext which deals with all tenant related functions including keeping track of the current tenant the user has active.
I was under the impression that once instantiated that the service is never instantiated again? Am I correct.
I have the following issue:
I am Injecting the TenantContect service in to multiple controllers and event triggers, but every time it is injected it is being instantiated again, e.g. the constructor is being run again and all previous values in the service have been reset:
TenantContext:
class TenantContext
{
private $tenant;
private $isInitialized = false;
...
...
public function __construct(
Security $security,
LoggerInterface $logger,
UserService $userService,
EntityManagerInterface $entityManager
)
{
$this->logger = $logger;
$this->logger->info("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY: Tenant instantiated");
...
...
}
This is instantiated first with my LoginEventListener which on successfuly login sets the tenant in the context:
class LoginListener
{
/**
*
* @var EntityManagerInterface
*/
private $em;
/**
*
* @var TenantContext
*/
private $tenantContext;
/**
*
* @var LoggerInterface
*/
private $logger;
public function __construct(
EntityManagerInterface $em,
LoggerInterface $logger,
TenantContext $tenantContext
)
{
$this->em = $em;
$this->logger = $logger;
$this->tenantContext = $tenantContext;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$this->logger->info("------------------------------------- onSecurityInteractiveLogin");
// Get the User entity.
/**
*
* @var User $user
*/
$user = $event->getAuthenticationToken()->getUser();
$this->tenantContext->initialize($user->getTenant());
}
}
This is working perfectly, however when I navigate to another page (using the routes e.g. In Twig: {{ path('dashboard') }}
Log:
Jan 10 14:38:11 |INFO | PHP Matched route "dashboard".
Jan 10 14:38:11 |DEBUG| PHP Read existing security token from the session.
Jan 10 14:38:11 |DEBUG| PHP SELECT t0.id AS id_1, t0.first_name AS first_name_2, t0.last_name AS last_name_3, t0.email AS email_4, t0.password AS password_5, t0.status AS status_6, t0.guid AS guid_7, t0.created_date AS created_date_8, t0.modif
0, t0.user_type AS user_type_11, t0.roles AS roles_12, t0.created_by_id AS created_by_id_13, t0.modified_by_id AS modified_by_id_14 FROM user t0 WHERE t0.id = ?
Jan 10 14:38:11 |DEBUG| PHP User was reloaded from a user provider.
Jan 10 14:38:11 |DEBUG| PHP Checking for guard authentication credentials.
Jan 10 14:38:11 |DEBUG| PHP Checking support on guard authenticator.
Jan 10 14:38:11 |DEBUG| PHP Guard authenticator does not support the request.
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "Nelmio\CorsBundle\EventListener\CorsListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\SessionListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::setDefaultLocale".
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\ResolveControllerNameSubscriber::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "ApiPlatform\Core\Filter\QueryParameterValidateListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleAwareListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener::configureLogoutUrlGenerator".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\AddFormatListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\ReadListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "ApiPlatform\Core\Security\EventListener\DenyAccessListener::onSecurity".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\DeserializeListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "ApiPlatform\Core\Security\EventListener\DenyAccessListener::onSecurityPostDenormalize".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.request" to listener "Knp\Bundle\PaginatorBundle\Subscriber\SlidingPaginationSubscriber::onKernelRequest".
Jan 10 14:38:12 |INFO | PHP User Deprecated: The "templating.locator" service is deprecated since Symfony 4.3 and will be removed in 5.0.
Jan 10 14:38:12 |INFO | PHP User Deprecated: The Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.
Jan 10 14:38:12 |INFO | PHP User Deprecated: The "templating.name_parser" service is deprecated since Symfony 4.3 and will be removed in 5.0.
Jan 10 14:38:12 |INFO | PHP User Deprecated: The Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
Jan 10 14:38:12 |INFO | PHP YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY: Tenant instantiated
Jan 10 14:38:12 |DEBUG| PHP Notified event "debug.security.authorization.vote" to listener "Symfony\Bundle\SecurityBundle\EventListener\VoteListener::onVoterVote".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.controller_arguments" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelControllerArguments".
Jan 10 14:38:12 |DEBUG| PHP Notified event "kernel.controller_arguments" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\IsGrantedListener::onKernelControllerArguments".
Jan 10 14:38:12 |INFO | PHP XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: Reinitializing tenant
Jan 10 14:38:12 |DEBUG| PHP SELECT t0.id AS id_1, t0.default_org AS default_org_2, t0.user_id AS user_id_3, t0.organization_id AS organization_id_4 FROM user_organization t0 WHERE t0.user_id = ?
Jan 10 14:38:12 |INFO | PHP ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ: Tenant initialized with id: 5
Jan 10 14:38:12 |INFO | PHP User Deprecated: Using the "templating" service is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.
Jan 10 14:38:12 |INFO | PHP User Deprecated: The Symfony\Bridge\Twig\TwigEngine class is deprecated since version 4.3 and will be removed in 5.0; use \Twig\Environment instead.
Jan 10 14:38:12 |INFO | PHP User Deprecated: The Symfony\Bundle\FrameworkBundle\Templating\EngineInterface interface is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.
Jan 10 14:38:12 |INFO | PHP User Deprecated: The Symfony\Bundle\TwigBundle\TwigEngine class is deprecated since version 4.3 and will be removed in 5.0; use \Twig\Environment instead.
I can see tfrom the logs that the constructor is meing called again, this happens for every request.
How do I get TenantContext to be one instance for the whole session?
Thanks in advance.
Edit: It seems this happens on other services too, I added some logging on another service I have and it performs the same. I haven't noticed this so far as all my services do not hold any data, just functions.
Upvotes: 0
Views: 1339
Reputation: 35
As per the comments, I now realize that my understanding of the Services is incorrect and they are instantiated on each request.
Therefore to get around my problem, I am storing hte ID in the session and retrieving the data when required using the Id from the session.
Upvotes: 2