У меня есть служба TennantContext, которая имеет дело со всеми функциями, связанными с арендатором, включая отслеживание текущего арендатора, которого активировал пользователь.
У меня сложилось впечатление, что однажды было подтверждено, что эта служба никогда больше не создается? Правильно ли я.
У меня есть следующая проблема:
Я внедряю службу TenantContect в несколько контроллеров и триггеры событий, но каждый раз, когда она вводится, она снова создается, например, конструктор запускается снова, и все предыдущие значения в службе были сброшены:
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");
...
...
}
Это сначала создается с помощью моего LoginEventListener, который при успешном входе в систему устанавливает арендатора в контексте :
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());
}
}
Это работает отлично, однако, когда я перехожу на другую страницу (используя маршруты, например, в 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.
Я вижу из журналов, что конструктор вызывается снова, это происходит для каждого запроса.
Как получить TenantContext одним экземпляром для всего сеанса?
Заранее спасибо.
Редактировать: Похоже, это происходит и в других сервисах, я добавил некоторые записи в другой сервис, который у меня есть, и он выполняет то же самое. не заметил этого, поскольку все мои сервисы не содержат никаких данных, только функции.