1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Context; |
4
|
|
|
|
5
|
|
|
use Knp\FriendlyContexts\Alice\Fixtures\Alice3\Loader as Alice3Loader; |
6
|
|
|
|
7
|
|
|
class AliceContext extends Context |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @BeforeBackground |
11
|
|
|
**/ |
12
|
|
|
public function loadAlice($event) |
13
|
|
|
{ |
14
|
|
|
$this->storeTags($event); |
15
|
|
|
$fixtures = $this->getParameter('friendly.alice.fixtures'); |
16
|
|
|
$loader = $this->getAliceLoader(); |
17
|
|
|
|
18
|
|
|
$files = $this->getTagContent('alice'); |
19
|
|
|
|
20
|
|
|
if (in_array('*', $files)) { |
21
|
|
|
$files = array_keys($fixtures); |
22
|
|
|
} else { |
23
|
|
|
$files = $this->resolveDepsFromArray($files); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
foreach ($files as $name) { |
27
|
|
|
if (!array_key_exists($name, $fixtures)) { |
28
|
|
|
|
29
|
|
|
throw new \Exception(sprintf('Fixture "%s" unknown. "%s" expected', $name, implode('", "', array_keys($fixtures)))); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->loadFixtures($loader, $fixtures, $files); |
34
|
|
|
$this->registerCache($loader); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function loadFixtures($loader, $fixtures, $files) |
38
|
|
|
{ |
39
|
|
|
$persistable = $this->getPersistableClasses(); |
40
|
|
|
|
41
|
|
|
if ($loader instanceof Alice3Loader) { |
42
|
|
|
$fileList = array_intersect_key($fixtures, array_flip($files)); |
43
|
|
|
foreach ($loader->load($fileList) as $object) { |
44
|
|
|
if (in_array(get_class($object), $persistable)) { |
45
|
|
|
$this->getEntityManager()->persist($object); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->getEntityManager()->flush(); |
50
|
|
|
} |
51
|
|
|
else { |
52
|
|
|
foreach ($fixtures as $id => $fixture) { |
53
|
|
|
if (in_array($id, $files)) { |
54
|
|
|
foreach ($loader->load($fixture) as $object) { |
55
|
|
|
if (in_array(get_class($object), $persistable)) { |
56
|
|
|
$this->getEntityManager()->persist($object); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->getEntityManager()->flush(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getPersistableClasses() |
67
|
|
|
{ |
68
|
|
|
$persistable = array(); |
69
|
|
|
$metadatas = $this->getEntityManager()->getMetadataFactory()->getAllMetadata(); |
70
|
|
|
|
71
|
|
|
foreach ($metadatas as $metadata) { |
72
|
|
|
if (isset($metadata->isEmbeddedClass) && $metadata->isEmbeddedClass) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$persistable[] = $metadata->getName(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $persistable; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function registerCache($loader) |
83
|
|
|
{ |
84
|
|
|
foreach ($loader->getCache() as $cache) { |
85
|
|
|
list($values, $entity) = $cache; |
86
|
|
|
$reflection = new \ReflectionClass($entity); |
87
|
|
|
do { |
88
|
|
|
$this |
89
|
|
|
->getRecordBag() |
90
|
|
|
->getCollection($reflection->getName()) |
91
|
|
|
->attach($entity, $values) |
92
|
|
|
; |
93
|
|
|
$reflection = $reflection->getParentClass(); |
94
|
|
|
} while (false !== $reflection); |
95
|
|
|
} |
96
|
|
|
$loader->clearCache(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function resolveDepsFromArray(array $fixtures) |
100
|
|
|
{ |
101
|
|
|
$result = []; |
102
|
|
|
|
103
|
|
|
foreach ($fixtures as $fixture) { |
104
|
|
|
$this->resolveDeps($fixture, $result); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function resolveDeps($fixture, array &$result = []) |
111
|
|
|
{ |
112
|
|
|
$result[] = $fixture; |
113
|
|
|
$tree = $this->getParameter('friendly.alice.dependencies'); |
114
|
|
|
|
115
|
|
|
if (!empty($tree[$fixture])) { |
116
|
|
|
foreach ($tree[$fixture] as $dep) { |
117
|
|
|
if (!in_array($dep, $result)) { |
118
|
|
|
$this->resolveDeps($dep, $result); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|