循环参考主义 - 树枝

时间:2015-05-09 09:20:44

标签: php security symfony doctrine-orm 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

MailService类:

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',
    );
}
}

当我尝试添加新用户时,我得到了例外:

  

检测到服务的循环引用" security.authorization_checker",路径:" 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 - >模板 - >树枝 - > security.context"

据我了解,错误的发生是因为Twig尝试使用EntityManager进行安全检查。

this文章中,作者使用了一个简单的方案,但在Doctrine EventListener中的Twig中也使用了该方案。这不是抛出异常。

1 个答案:

答案 0 :(得分:9)

我认为避免循环引用的最简洁方法是将事件调度程序注入您的学说监听器。然后在doctrine listener中调度一个用户注册的事件,然后该事件将有一个Symfony内核侦听器,然后可以发送该电子邮件。

Doctrine Subscriber

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 Subscriber

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 }