Symfony service instantiated on every request

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 "SymfonyComponentHttpKernelEventListenerDebugHandlersListener::configure".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerValidateRequestListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "NelmioCorsBundleEventListenerCorsListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerSessionListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerLocaleListener::setDefaultLocale".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerRouterListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyBundleFrameworkBundleEventListenerResolveControllerNameSubscriber::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatformCoreFilterQueryParameterValidateListener::onKernelRequest".
Jan 10 14:38:11 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerLocaleListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerLocaleAwareListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyBundleSecurityBundleDebugTraceableFirewallListener::configureLogoutUrlGenerator".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "SymfonyBundleSecurityBundleDebugTraceableFirewallListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatformCoreEventListenerAddFormatListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatformCoreEventListenerReadListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatformCoreSecurityEventListenerDenyAccessListener::onSecurity".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatformCoreEventListenerDeserializeListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatformCoreSecurityEventListenerDenyAccessListener::onSecurityPostDenormalize".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "ApiPlatformCoreBridgeSymfonyBundleEventListenerSwaggerUiListener::onKernelRequest".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.request" to listener "KnpBundlePaginatorBundleSubscriberSlidingPaginationSubscriber::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 SymfonyBundleFrameworkBundleTemplatingLoaderTemplateLocator 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 SymfonyBundleFrameworkBundleTemplatingTemplateNameParser 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 "SymfonyBundleFrameworkBundleDataCollectorRouterDataCollector::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "SymfonyComponentHttpKernelDataCollectorRequestDataCollector::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerControllerListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerParamConverterListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerHttpCacheListener::onKernelController".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerTemplateListener::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 "SymfonyBundleSecurityBundleEventListenerVoteListener::onVoterVote".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller_arguments" to listener "SensioBundleFrameworkExtraBundleEventListenerSecurityListener::onKernelControllerArguments".
Jan 10 14:38:12 |DEBUG| PHP    Notified event "kernel.controller_arguments" to listener "SensioBundleFrameworkExtraBundleEventListenerIsGrantedListener::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 SymfonyBridgeTwigTwigEngine class is deprecated since version 4.3 and will be removed in 5.0; use TwigEnvironment instead.
Jan 10 14:38:12 |INFO | PHP    User Deprecated: The SymfonyBundleFrameworkBundleTemplatingEngineInterface 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 SymfonyBundleTwigBundleTwigEngine class is deprecated since version 4.3 and will be removed in 5.0; use TwigEnvironment 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.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *