- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从事 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 的一些特殊情况有关,例如 Doctrine
、FOSUserBundle
等。这些线程中讨论的解决方案在这里不起作用。
显而易见的解决方案是将 PaymentService
分成两部分:一部分包含 isPaymentComplete(int userId)
另一部分提供 updatePaymentStatus
。但这并不像听起来那么容易,因为这两种方法使用其他常见的 PaymentService
方法。为了避免将杠杆上的循环引用更深地移动,我需要将 PaymentService
分成三个部分:
isPaymentComplete
updatePaymentStatus
这(很有可能)会奏效,但会非常、非常难看。这三个服务中的所有方法都服务于相同的目的(处理付款),因此应该包含在相同的服务中。将服务分解成不同的部分与代码结构无关,而只会通过破解来分解循环引用。
我已经尝试将引用从构造函数移至 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/
环形缓冲区和循环链表有什么区别? Ring Buffer 的目的是什么,而循环链表不能,反之亦然? 最佳答案 环形缓冲区是一个连续的内存块,其中包含您的项目,当您到达终点时,您会循环回到起点: +--
我有一个像这样的多级依赖关系图,我需要检测该图中的任何循环引用。 A = B 乙=丙 C = [D, B] D = [C, A] 有人有这样的问题吗? 有什么解决办法??? 用英语表示感谢和抱歉。 =
尝试编写一个reduce函数来过滤掉任何重复项。我知道还有其他方法可以解决这个问题,但我正在尝试练习递归函数。 function addToSet(a, b) { a.add(b); retu
我想知道是否有人已经实现/知道(最好是 javascript)将处理循环间隔的间隔树算法。通过循环,我的意思是开始 > 结束的间隔。请注意,这也需要限制间隔的大小。 这只是常见区间树问题的一个子案例吗
我有一段代码,其中声明了两种数据类型。我已经将两者都自动派生为类型类 enum 的成员,但是,我不喜欢它们不是“圆形”的。我的意思是调用 succ Sun应该让我Mon . succ Dec应该导致
运行此代码时: function arrayToList(arr) { if (arr.length == 1) { return { value: arr[0],
我有以下模块结构: /components ├── Button.js ├── Checkbox.js ├── index.js ├── DateSelect ├── DateSelect.j
为了检查光线与三角形的碰撞,我们可以首先查看光线是否与三角形的平面发生碰撞。如果是,我们然后检查所有三角形边的交点是否在同一边。如果为真,则意味着该点位于三角形内部。此过程与矩形和其他凸图形类似。 这
我遇到了同样的问题,如下所示: NHibernate - not-null property reference a null or transient value 根本原因就像 Alun Harfo
我想以“循环”方式使用函数,如下例所示: fun cll1 (s)= cll2(s); fun cll2 (s)= cll3(s); fun cll3 (s)= cll(s); 编写此代码会在 SML
我想根据现有列的滞后值在 pyspark.sql.DataFrame 中创建一个新列。但是......我也希望最后一个值成为第一个值,第一个值成为最后一个值。这是一个例子: df = spark.cr
我正在使用 Microsoft.ServiceModel.Samples.CircularTracecListener 二进制文件来实现 Web 服务上的错误日志记录。 它应该写入一个文件,直到达到
我有一个由 8 个罗盘点组成的数组,从 SW 顺时针编号到 S: 2 3 4 1 5 0 7 6 我想计算从一点到另一点的最短路线是顺时针 (+1) 还是逆时针 (-1)。例如。从 7 到 5 是
我知道如何解决基本的循环依赖示例,有两个类,每个类都需要知道另一个存在。 但是,我现在的情况是,示例更复杂,前向声明不是可以解决该问题的东西。 考虑这三个文件 // my_thread.hpp tem
我正在开发一款用于在线播放歌曲的应用程序。我可以通过点击播放这首歌。 我的问题是我在所有 Activity 中都有一个 CircularSeekBar,我需要在所有屏幕上管理它。如果从任何 Activ
我正在尝试实现动画 shown here .它工作正常,除了 unreveal 动画,显示黑屏片刻。并非所有设备/api 版本都存在该故障。 Google Nexus 10 (API 22) 的 Ge
我正在使用 Canvas 创建倒数计时器循环,但有一个简单的问题,我需要将天数除以 364 天,当天数大于 364 天时,天数消失: 我需要的是这样的: (function($) { jQuery
我想在不使用 SVG 的情况下实现这样的效果: https://codepen.io/arforcee/pen/erbKi?limit=all&page=2&q=circular+loader 这是否
这个问题在这里已经有了答案: How to create a circle with links on border side (8 个答案) 关闭 4 年前。
我遇到了一个小设计问题。 如下情况 库界面 包含每个模型类的接口(interface)(仅限 getter 和 setter) 图书馆业务逻辑 包含接口(interface)库和 DAL 的实现。使用
我是一名优秀的程序员,十分优秀!