29
29
*/
30
30
class CmfHelper extends Helper
31
31
{
32
+ /**
33
+ * @var ManagerRegistry
34
+ */
35
+ private $ doctrineRegistry ;
36
+
37
+ /**
38
+ * @var string
39
+ */
40
+ private $ doctrineManagerName ;
41
+
32
42
/**
33
43
* @var DocumentManager
34
44
*/
@@ -40,18 +50,35 @@ class CmfHelper extends Helper
40
50
protected $ publishWorkflowChecker ;
41
51
42
52
/**
53
+ * The $registry constructor argument is deprecated in favor of
54
+ * setDcotrineRegistry in order to avoid circular dependencies when a
55
+ * doctrine event listener needs twig injected.
56
+ *
43
57
* @param SecurityContextInterface $publishWorkflowChecker
44
58
* @param ManagerRegistry $registry For loading PHPCR-ODM documents from
45
59
* Doctrine.
46
- * @param string $objectManagerName
60
+ * @param string $managerName
47
61
*/
48
- public function __construct (SecurityContextInterface $ publishWorkflowChecker = null , $ registry = null , $ objectManagerName = null )
62
+ public function __construct (SecurityContextInterface $ publishWorkflowChecker = null , $ registry = null , $ managerName = null )
49
63
{
50
64
$ this ->publishWorkflowChecker = $ publishWorkflowChecker ;
65
+ $ this ->setDoctrineRegistry ($ registry , $ managerName );
66
+ }
51
67
52
- if ($ registry && $ registry instanceof ManagerRegistry) {
53
- $ this ->dm = $ registry ->getManager ($ objectManagerName );
68
+ /**
69
+ * Set the doctrine manager registry to fetch the object manager from.
70
+ *
71
+ * @param ManagerRegistry $registry
72
+ * @param string|null $managerName Manager name if not the default
73
+ */
74
+ public function setDoctrineRegistry ($ registry , $ managerName = null )
75
+ {
76
+ if ($ this ->doctrineRegistry ) {
77
+ throw new \LogicException ('Do not call this setter repeatedly or after using constructor injection ' );
54
78
}
79
+
80
+ $ this ->doctrineRegistry = $ registry ;
81
+ $ this ->doctrineManagerName = $ managerName ;
55
82
}
56
83
57
84
/**
@@ -60,7 +87,11 @@ public function __construct(SecurityContextInterface $publishWorkflowChecker = n
60
87
protected function getDm ()
61
88
{
62
89
if (!$ this ->dm ) {
63
- throw new \RuntimeException ('Doctrine is not available. ' );
90
+ if (!$ this ->doctrineRegistry ) {
91
+ throw new \RuntimeException ('Doctrine is not available. ' );
92
+ }
93
+
94
+ $ this ->dm = $ this ->doctrineRegistry ->getManager ($ this ->doctrineManagerName );
64
95
}
65
96
66
97
return $ this ->dm ;
0 commit comments