循环引用 Doctrine - Twig
Circular reference Doctrine - Twig
我在应用程序中做用户注册,我想在注册时收到邮件通知。我为此服务创建:
app.mail_service:
class: AppBundle\Mail\MailService
arguments: ["@mailer", "@templating"]
app.listener.user:
class: AppBundle\EventListener\UserSubscriber
arguments: ["@app.mail_service"]
tags:
- { name: doctrine.event_subscriber, connection: default }
模板化 - TwigEngine
邮件服务class:
class MailService
{
private $mailer;
private $renderer;
public function __construct(Swift_Mailer $mailer, EngineInterface $renderer)
{
$this->mailer = $mailer;
$this->renderer = $renderer;
}
/**
* @return Swift_Mailer
*/
public function getMailer()
{
return $this->mailer;
}
/**
* @return EngineInterface
*/
public function getRenderer()
{
return $this->renderer;
}
public function sendRegistrationMail(User $user)
{
/** @var \Swift_Message $message */
$message = $this->getMailer()
->createMessage();
$message->setSubject('You successful register in website')
->addTo($user->getEmail())
->setBody($this->getRenderer()->render('AppBundle:Mail:register.html.twig', array(
'user' => $user
)), 'text/html', 'UTF-8');
return $this->getMailer()->send($message);
}
}
和用户订阅监听器:
class UserSubscriber implements EventSubscriber
{
/**
* @var MailService
*/
private $mailService;
public function __construct(MailService $mailService)
{
$this->mailService = $mailService;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof User) {
$this->mailService->sendRegistrationMail($entity);
}
}
/**
* Returns an array of events this subscriber wants to listen to.
*
* @return array
*/
public function getSubscribedEvents()
{
return array(
'postPersist',
);
}
}
当我尝试添加新用户时,出现异常:
Circular reference detected for service "security.authorization_checker", path: "sensio_framework_extra.security.listener -> security.authorization_checker -> security.authentication.manager -> security.user.provider.concrete.entity_provider -> doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection -> app.listener.user -> app.mail_service -> templating -> twig -> security.context".
据我了解,错误的发生是由于 Twig 尝试使用 EntityManager 进行安全检查。
在this文章中,作者使用了一个简单的scheme,但是在Twig中也使用了Doctrine EventListener。那不是抛出异常。
我想说避免循环引用的最干净的方法是将事件调度程序注入您的学说侦听器。然后在 doctrine listener 中发送一个用户注册的事件,然后有一个 Symfony 内核监听器可以发送电子邮件。
学说订阅者
class UserSubscriber implements EventSubscriber
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof User) {
$dispatcher->dispatch('acme.user.registered', new UserEvent($user));
}
}
/**
* Returns an array of events this subscriber wants to listen to.
*
* @return array
*/
public function getSubscribedEvents()
{
return array(
'postPersist',
);
}
}
用户事件
class UserEvent extends GenericEvent
{
public function __construct(User $user, array $arguments = array())
{
parent::__construct($user, $arguments);
}
/**
* @return User
*/
public function getUser()
{
return $this->getSubject();
}
}
Symfony 订阅者
class UserRegistrationMailSubscriber implements EventSubscriberInterface
{
/**
* @var MailService
*/
private $mailService;
public function __construct(MailService $mailService)
{
$this->mailService = $mailService;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
'acme.user.registered' => 'sendRegistrationMail',
);
}
/**
* @param UserEvent $event
*/
public function sendRegistrationMail(UserEvent $event)
{
$this->mailService->sendRegistrationMail($event->getUser());
}
}
服务
app.mail_service:
class: AppBundle\Mail\MailService
arguments: ["@mailer", "@templating"]
app.doctrine.listener.user:
class: AppBundle\EventListener\UserSubscriber
arguments: ["@event_dispatcher"]
tags:
- { name: doctrine.event_subscriber, connection: default }
app.kernel.listener.user_registartion_mail:
class: AppBundle\EventListener\UserRegistrationMailSubscriber
arguments: ["@app.mail_service"}
tags:
- { name: kernel.event_subscriber }
我在应用程序中做用户注册,我想在注册时收到邮件通知。我为此服务创建:
app.mail_service:
class: AppBundle\Mail\MailService
arguments: ["@mailer", "@templating"]
app.listener.user:
class: AppBundle\EventListener\UserSubscriber
arguments: ["@app.mail_service"]
tags:
- { name: doctrine.event_subscriber, connection: default }
模板化 - TwigEngine
邮件服务class:
class MailService
{
private $mailer;
private $renderer;
public function __construct(Swift_Mailer $mailer, EngineInterface $renderer)
{
$this->mailer = $mailer;
$this->renderer = $renderer;
}
/**
* @return Swift_Mailer
*/
public function getMailer()
{
return $this->mailer;
}
/**
* @return EngineInterface
*/
public function getRenderer()
{
return $this->renderer;
}
public function sendRegistrationMail(User $user)
{
/** @var \Swift_Message $message */
$message = $this->getMailer()
->createMessage();
$message->setSubject('You successful register in website')
->addTo($user->getEmail())
->setBody($this->getRenderer()->render('AppBundle:Mail:register.html.twig', array(
'user' => $user
)), 'text/html', 'UTF-8');
return $this->getMailer()->send($message);
}
}
和用户订阅监听器:
class UserSubscriber implements EventSubscriber
{
/**
* @var MailService
*/
private $mailService;
public function __construct(MailService $mailService)
{
$this->mailService = $mailService;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof User) {
$this->mailService->sendRegistrationMail($entity);
}
}
/**
* Returns an array of events this subscriber wants to listen to.
*
* @return array
*/
public function getSubscribedEvents()
{
return array(
'postPersist',
);
}
}
当我尝试添加新用户时,出现异常:
Circular reference detected for service "security.authorization_checker", path: "sensio_framework_extra.security.listener -> security.authorization_checker -> security.authentication.manager -> security.user.provider.concrete.entity_provider -> doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection -> app.listener.user -> app.mail_service -> templating -> twig -> security.context".
据我了解,错误的发生是由于 Twig 尝试使用 EntityManager 进行安全检查。
在this文章中,作者使用了一个简单的scheme,但是在Twig中也使用了Doctrine EventListener。那不是抛出异常。
我想说避免循环引用的最干净的方法是将事件调度程序注入您的学说侦听器。然后在 doctrine listener 中发送一个用户注册的事件,然后有一个 Symfony 内核监听器可以发送电子邮件。
学说订阅者
class UserSubscriber implements EventSubscriber
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof User) {
$dispatcher->dispatch('acme.user.registered', new UserEvent($user));
}
}
/**
* Returns an array of events this subscriber wants to listen to.
*
* @return array
*/
public function getSubscribedEvents()
{
return array(
'postPersist',
);
}
}
用户事件
class UserEvent extends GenericEvent
{
public function __construct(User $user, array $arguments = array())
{
parent::__construct($user, $arguments);
}
/**
* @return User
*/
public function getUser()
{
return $this->getSubject();
}
}
Symfony 订阅者
class UserRegistrationMailSubscriber implements EventSubscriberInterface
{
/**
* @var MailService
*/
private $mailService;
public function __construct(MailService $mailService)
{
$this->mailService = $mailService;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
'acme.user.registered' => 'sendRegistrationMail',
);
}
/**
* @param UserEvent $event
*/
public function sendRegistrationMail(UserEvent $event)
{
$this->mailService->sendRegistrationMail($event->getUser());
}
}
服务
app.mail_service:
class: AppBundle\Mail\MailService
arguments: ["@mailer", "@templating"]
app.doctrine.listener.user:
class: AppBundle\EventListener\UserSubscriber
arguments: ["@event_dispatcher"]
tags:
- { name: doctrine.event_subscriber, connection: default }
app.kernel.listener.user_registartion_mail:
class: AppBundle\EventListener\UserRegistrationMailSubscriber
arguments: ["@app.mail_service"}
tags:
- { name: kernel.event_subscriber }