Symfony 2:解决循环引用问题

3
我正在开发一个Symfony 2 WebApp项目。集成商店的逻辑是在MyShopBundle中实现的。
在为bundle添加Twig Extension后,我遇到了一个异常:
我完全理解这个消息的含义,但目前我还没有找到解决问题的方法: MyShopBundle中的PaymentServices提供与支付过程相关的各种不同服务。除其他外,它观察支付状态并自动向用户发送电子邮件通知。此外,它还提供isPaymentComplete(int userId)
为了从Twig模板呈现电子邮件的内容,PaymentServices需要引用Twig_Environment。服务从其构造函数获取此引用:
class MyPaymentService {
    protected $twig;

    public function __construct(\Twig_Environment $twig, ...) {
        $this->twig = $twig;
        ...
    }

    public function updatePaymentStatus(int userId) {
        // Update Payment
        ...

        // Send notification to user
        $mailBody = $this->twig->render('MyShopBundle:Emails:statusUpdate.html.twig', array('user_name' => $username));
        ...
    }

    public function isPaymentComplete(int userId) {
        ...
        return true;   
    }
}

另一方面,Twig扩展需要引用PaymentService来创建isPaymentComplete(int userId)的Twig版本。
class ShopExtension extends \Twig_Extension {
    private $paymentService;

    public function __construct(PaymentService $service, ...) {
        $this->paymentService = $service;
        ...
    }

    public function getName() {
        return 'my_shop_bundle_extension';
    }

    public function getFunctions() {
        $functions = array();

        $functions[] = new \Twig_SimpleFunction('msb_IsPaymentComplete', array(
            $this,
            'msb_IsPaymentComplete'
        ));

        return $functions;
    }

    public function msb_IsPaymentComplete($user) {
        if ($this->paymentService)
            return $this->paymentService->isPaymentComplete($user);
        else 
            return false;
    }
} 

这是在service.yml中的服务定义。
services:
    shop.payment.service:
        class: MyShopBundle\Service\PaymentService
        arguments: 
          - "@twig"
          - ...

    app_subscription.twig_extension:
        class: MyShopBundle\Twig\ShopExtension
        arguments: 
            - "@shop.payment.service"
        tags:
          - { name: twig.extension }

异常的来源很明显:
PaymentService --> Twig --> ShopExtension --> PaymentService 问题是:如何解决?
我发现其他关于Symfony中循环引用的问题。但它们都与使用一些常见Bundle/Service(如Doctrine、FOSUserBundle等)的特殊情况有关。这些线程讨论的解决方案在这里不起作用。
显然的解决方案是将PaymentService分成两部分:一个包括isPaymentComplete(int userId),另一个提供updatePaymentStatus。但这并不像听起来那么容易,因为这两个方法使用其他通用的PaymentService方法。为了避免仅将循环引用移动到更深层次上,我需要将PaymentService分成三个部分:
1. Service1包括isPaymentComplete 2. Service2包括updatePaymentStatus 3. Service3包括Service1和2使用的所有通用方法
这可能会起作用,但非常丑陋。这三个服务中的所有方法都具有相同的目的(处理付款),因此应包含在同一服务中。将服务分解成不同的部分与代码结构无关,只是为了打破循环引用的hack。
我已经尝试将引用从构造函数移动到setter:
services:
    shop.payment.service:
        class: MyShopBundle\Service\PaymentService
        calls:
          - [ setTwig, [ "@twig" ] ]

    app_subscription.twig_extension:
        class: MyShopBundle\Twig\ShopExtension
        calls:
          - [ setService, [ "@shop.payment.service" ] ]
        tags:
          - { name: twig.extension }

结果仍然是一样的。
那么:有没有干净的解决这个引用问题的方法?

你可以将支付服务拆分,以便将更新支付信息和查询支付完成状态分开处理。 - xabbuh
1个回答

1
这个问题意味着您需要更少的依赖来提供更多的服务。例如,您可以拥有一个名为clientMailProvider的服务,该服务获取twig html电子邮件并使用您的数据进行填充。例如,购物支付服务将调用此服务,或者在另一个服务中调用。
现在针对您的问题,我认为您可以注入twig模板的部分而不是整个服务:
services:
    shop.payment.service:
        class: MyShopBundle\Service\PaymentService
        arguments: 
          - "@templating"

这项服务

use Symfony\Component\Templating\EngineInterface;

class MyPaymentService {
    protected $templating;

    public function __construct(EngineInterface $templating, ...) {
        $this->templating= $templating;
        ...
    }

    public function updatePaymentStatus(int userId) {
        // Update Payment
        ...

        // Send notification to user
        $mailBody = $this->templating->render('MyShopBundle:Emails:statusUpdate.html.twig', array('user_name' => $username));
        ...
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接