1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\FixturesBundle\Builder; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Kunstmaan\FixturesBundle\Loader\Fixture; |
7
|
|
|
use Kunstmaan\FixturesBundle\Populator\Populator; |
8
|
|
|
use Kunstmaan\PagePartBundle\Entity\PagePartRef; |
9
|
|
|
use Kunstmaan\PagePartBundle\Helper\PagePartInterface; |
10
|
|
|
|
11
|
|
|
class PagePartBuilder implements BuilderInterface |
12
|
|
|
{ |
13
|
|
|
/** @var \Doctrine\ORM\EntityManager */ |
14
|
|
|
private $em; |
15
|
|
|
|
16
|
|
|
/** @var \Kunstmaan\PagePartBundle\Repository\PagePartRefRepository */ |
17
|
|
|
private $pagePartRepo; |
18
|
|
|
|
19
|
|
|
/** @var \Kunstmaan\FixturesBundle\Populator\Populator */ |
20
|
|
|
private $populator; |
21
|
|
|
|
22
|
|
|
public function __construct(EntityManager $em, Populator $populator) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$this->em = $em; |
25
|
|
|
$this->pagePartRepo = $em->getRepository(PagePartRef::class); |
26
|
|
|
$this->populator = $populator; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function canBuild(Fixture $fixture) |
30
|
|
|
{ |
31
|
|
|
if ($fixture->getEntity() instanceof PagePartInterface) { |
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function preBuild(Fixture $fixture) |
39
|
|
|
{ |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function postBuild(Fixture $fixture) |
44
|
|
|
{ |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function postFlushBuild(Fixture $fixture) |
49
|
|
|
{ |
50
|
|
|
$params = $fixture->getParameters(); |
51
|
|
|
$translations = $fixture->getTranslations(); |
52
|
|
|
$original = $fixture->getEntity(); |
53
|
|
|
|
54
|
|
View Code Duplication |
if (!isset($params['page']) || empty($translations)) { |
|
|
|
|
55
|
|
|
throw new \Exception('No page reference and/or translations detected for pagepart fixture ' . $fixture->getName() . ' (' . $fixture->getClass() . ')'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$pageFixture = $params['page']; |
59
|
|
|
if (!$pageFixture instanceof Fixture) { |
60
|
|
|
throw new \Exception('Could not find a reference "' . $params['page'] . '"" for fixture ' . $fixture->getName() . ' (' . $fixture->getClass() . ')'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$additionalEntities = $pageFixture->getAdditionalEntities(); |
64
|
|
|
$pp = $original; |
65
|
|
|
$first = null; |
66
|
|
|
foreach ($translations as $language => $data) { |
67
|
|
|
$key = $pageFixture->getName() . '_' . $language; |
68
|
|
|
if (!isset($additionalEntities[$key])) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($first !== null) { |
73
|
|
|
$pp = clone $original; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$page = $additionalEntities[$key]; |
77
|
|
|
$this->populator->populate($pp, $data); |
78
|
|
|
$this->em->persist($pp); |
79
|
|
|
$this->em->flush($pp); |
80
|
|
|
|
81
|
|
|
// Find latest position. |
82
|
|
|
$position = array_key_exists('position', $params) ? $params['position'] : null; |
83
|
|
|
$context = isset($params['context']) ? $params['context'] : 'main'; |
84
|
|
View Code Duplication |
if (is_null($position)) { |
|
|
|
|
85
|
|
|
$pageParts = $this->pagePartRepo->getPagePartRefs($page, $context); |
86
|
|
|
$position = count($pageParts) + 1; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->pagePartRepo->addPagePart($page, $pp, $position, $context); |
90
|
|
|
$first = false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.