@@ -25,11 +25,24 @@ class PHPCR
25
25
protected $ container ;
26
26
protected $ om ;
27
27
28
+ /**
29
+ * @var PHPCRExecutor
30
+ */
31
+ private $ executor ;
32
+
33
+ /**
34
+ * @param ContainerInterface
35
+ */
28
36
public function __construct (ContainerInterface $ container )
29
37
{
30
38
$ this ->container = $ container ;
31
39
}
32
40
41
+ /**
42
+ * Return the PHPCR ODM registry
43
+ *
44
+ * @return Symfony\Bridge\Doctrine\RegistryInterface
45
+ */
33
46
public function getRegistry ()
34
47
{
35
48
return $ this ->container ->get ('doctrine_phpcr ' );
@@ -48,26 +61,42 @@ public function getOm($managerName = null)
48
61
return $ this ->om ;
49
62
}
50
63
51
- public function loadFixtures (array $ classNames , $ initialize = false )
64
+ /**
65
+ * Purge the database
66
+ *
67
+ * @param boolean $initialize If the ODM repository initializers should be executed.
68
+ */
69
+ public function purgeRepository ($ initialize = false )
52
70
{
53
- $ initializerManager = $ initialize ? $ this ->container ->get ('doctrine_phpcr.initializer_manager ' ) : null ;
54
-
55
- $ loader = new ContainerAwareLoader ($ this ->container );;
56
71
$ purger = new PHPCRPurger ();
57
- $ executor = new PHPCRExecutor ($ this ->getOm (), $ purger , $ initializerManager );
72
+ $ this ->getExecutor ($ initialize )->purge ();
73
+ }
58
74
59
- $ referenceRepository = new ProxyReferenceRepository ($ this ->getOm ());
75
+ /**
76
+ * Load fixtures
77
+ *
78
+ * @param array $classNames Fixture classes to load
79
+ * @param boolean $initialize If the ODM repository initializers should be executed.
80
+ */
81
+ public function loadFixtures (array $ classNames , $ initialize = false )
82
+ {
83
+ $ this ->purgeRepository ();
60
84
61
- $ executor ->setReferenceRepository ($ referenceRepository );
62
- $ executor ->purge ();
85
+ $ loader = new ContainerAwareLoader ($ this ->container );;
63
86
64
87
foreach ($ classNames as $ className ) {
65
88
$ this ->loadFixtureClass ($ loader , $ className );
66
89
}
67
90
68
- $ executor ->execute ($ loader ->getFixtures (), true );
91
+ $ this -> getExecutor ( $ initialize ) ->execute ($ loader ->getFixtures (), true );
69
92
}
70
93
94
+ /**
95
+ * Load the named fixture class with the given loader.
96
+ *
97
+ * @param \Doctrine\Common\DataFixtures\Loader $loader
98
+ * @param string $className
99
+ */
71
100
public function loadFixtureClass ($ loader , $ className )
72
101
{
73
102
if (!class_exists ($ className )) {
@@ -93,6 +122,9 @@ public function loadFixtureClass($loader, $className)
93
122
}
94
123
}
95
124
125
+ /**
126
+ * Create a test node, if the test node already exists, remove it.
127
+ */
96
128
public function createTestNode ()
97
129
{
98
130
$ session = $ this ->container ->get ('doctrine_phpcr.session ' );
@@ -105,4 +137,29 @@ public function createTestNode()
105
137
106
138
$ session ->save ();
107
139
}
140
+
141
+ /**
142
+ * Return the PHPCR Executor class
143
+ *
144
+ * @return PHPCRExecutor
145
+ */
146
+ private function getExecutor ($ initialize = false )
147
+ {
148
+ static $ lastInitialize = null ;
149
+
150
+ if ($ this ->executor && $ initialize === $ lastInitialize ) {
151
+ return $ this ->executor ;
152
+ }
153
+
154
+ $ initializerManager = $ initialize ? $ this ->container ->get ('doctrine_phpcr.initializer_manager ' ) : null ;
155
+ $ purger = new PHPCRPurger ();
156
+ $ executor = new PHPCRExecutor ($ this ->getOm (), $ purger , $ initializerManager );
157
+ $ referenceRepository = new ProxyReferenceRepository ($ this ->getOm ());
158
+ $ executor ->setReferenceRepository ($ referenceRepository );
159
+
160
+ $ this ->executor = $ executor ;
161
+ $ lastInitialize = $ initialize ;
162
+
163
+ return $ executor ;
164
+ }
108
165
}
0 commit comments