我正在开发一个Symfony 2 WebApp项目。集成商店的逻辑是在
在为bundle添加
我完全理解这个消息的含义,但目前我还没有找到解决问题的方法:
为了从
另一方面,Twig扩展需要引用
这是在
异常的来源很明显:
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:
结果仍然是一样的。
那么:有没有干净的解决这个引用问题的方法?
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 }
结果仍然是一样的。
那么:有没有干净的解决这个引用问题的方法?