gpt4 book ai didi

交响乐 2 : Resolve circular reference

转载 作者:行者123 更新时间:2023-12-02 03:16:08 24 4
gpt4 key购买 nike

我正在从事 Symfony 2 WebApp 项目。集成商店的逻辑在 MyShopBundle 中实现。

在将 Twig Extension 添加到 bundle 后,我得到一个异常:

我完全理解这条消息的意思,但到目前为止我没有找到解决问题的方法:

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 Extension 需要引用 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/Services 的一些特殊情况有关,例如 DoctrineFOSUserBundle 等。这些线程中讨论的解决方案在这里不起作用。

显而易见的解决方案是将 PaymentService 分成两部分:一部分包含 isPaymentComplete(int userId) 另一部分提供 updatePaymentStatus。但这并不像听起来那么容易,因为这两种方法使用其他常见的 PaymentService 方法。为了避免将杠杆上的循环引用更深地移动,我需要将 PaymentService 分成三个部分:

  • Service1 包括 isPaymentComplete
  • 服务 2,包括 updatePaymentStatus
  • Service3 包括 Service1 和 2 使用的所有常用方法

这(很有可能)会奏效,但会非常、非常难看。这三个服务中的所有方法都服务于相同的目的(处理付款),因此应该包含在相同的服务中。将服务分解成不同的部分与代码结构无关,而只会通过破解来分解循环引用。

我已经尝试将引用从构造函数移至 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 }

结果还是一样。

那么:是否有任何干净的解决方案来解决这个引用问题?

最佳答案

这个问题意味着您将需要更多的服务和更少的依赖。例如,您可以拥有像 clientMailProvider 这样的服务,它会获取 twig html 电子邮件并将其与您的数据混合。例如,该服务将由商店支付服务或其他服务调用。

现在针对你的问题,我认为你可以注入(inject) 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));
...
}

关于交响乐 2 : Resolve circular reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36977046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com