Skip to content

Commit 3e0b03d

Browse files
committed
Merge pull request #76 from symfony-cmf/phpcr_odm_purge_database
[WIP] Added PHPCR purge
2 parents b3e062a + 8b95434 commit 3e0b03d

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

src/Functional/DbManager/PHPCR.php

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,24 @@ class PHPCR
2525
protected $container;
2626
protected $om;
2727

28+
/**
29+
* @var PHPCRExecutor
30+
*/
31+
private $executor;
32+
33+
/**
34+
* @param ContainerInterface
35+
*/
2836
public function __construct(ContainerInterface $container)
2937
{
3038
$this->container = $container;
3139
}
3240

41+
/**
42+
* Return the PHPCR ODM registry
43+
*
44+
* @return Symfony\Bridge\Doctrine\RegistryInterface
45+
*/
3346
public function getRegistry()
3447
{
3548
return $this->container->get('doctrine_phpcr');
@@ -48,26 +61,42 @@ public function getOm($managerName = null)
4861
return $this->om;
4962
}
5063

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)
5270
{
53-
$initializerManager = $initialize ? $this->container->get('doctrine_phpcr.initializer_manager') : null;
54-
55-
$loader = new ContainerAwareLoader($this->container);;
5671
$purger = new PHPCRPurger();
57-
$executor = new PHPCRExecutor($this->getOm(), $purger, $initializerManager);
72+
$this->getExecutor($initialize)->purge();
73+
}
5874

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();
6084

61-
$executor->setReferenceRepository($referenceRepository);
62-
$executor->purge();
85+
$loader = new ContainerAwareLoader($this->container);;
6386

6487
foreach ($classNames as $className) {
6588
$this->loadFixtureClass($loader, $className);
6689
}
6790

68-
$executor->execute($loader->getFixtures(), true);
91+
$this->getExecutor($initialize)->execute($loader->getFixtures(), true);
6992
}
7093

94+
/**
95+
* Load the named fixture class with the given loader.
96+
*
97+
* @param \Doctrine\Common\DataFixtures\Loader $loader
98+
* @param string $className
99+
*/
71100
public function loadFixtureClass($loader, $className)
72101
{
73102
if (!class_exists($className)) {
@@ -93,6 +122,9 @@ public function loadFixtureClass($loader, $className)
93122
}
94123
}
95124

125+
/**
126+
* Create a test node, if the test node already exists, remove it.
127+
*/
96128
public function createTestNode()
97129
{
98130
$session = $this->container->get('doctrine_phpcr.session');
@@ -105,4 +137,29 @@ public function createTestNode()
105137

106138
$session->save();
107139
}
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+
}
108165
}

0 commit comments

Comments
 (0)