|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Context; |
|
4
|
|
|
|
|
5
|
|
|
class AliceContext extends Context |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @BeforeBackground |
|
9
|
|
|
**/ |
|
10
|
|
|
public function loadAlice($event) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->storeTags($event); |
|
13
|
|
|
$fixtures = $this->getParameter('friendly.alice.fixtures'); |
|
14
|
|
|
$loader = $this->getAliceLoader(); |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
$files = $this->getTagContent('alice'); |
|
17
|
|
|
|
|
18
|
|
|
if (in_array('*', $files)) { |
|
19
|
|
|
$files = array_keys($fixtures); |
|
20
|
|
|
} else { |
|
21
|
|
|
$files = $this->resolveDepsFromArray($files); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
foreach ($files as $name) { |
|
25
|
|
View Code Duplication |
if (!array_key_exists($name, $fixtures)) { |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
throw new \Exception(sprintf('Fixture "%s" unknown. "%s" expected', $name, implode('", "', array_keys($fixtures)))); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->loadFixtures($loader, $fixtures, $files); |
|
32
|
|
|
$this->registerCache($loader); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function loadFixtures($loader, $fixtures, $files) |
|
36
|
|
|
{ |
|
37
|
|
|
$persistable = $this->getPersistableClasses(); |
|
38
|
|
|
|
|
39
|
|
|
foreach ($fixtures as $id => $fixture) { |
|
40
|
|
|
if (in_array($id, $files)) { |
|
41
|
|
|
foreach ($loader->load($fixture) as $object) { |
|
42
|
|
|
if (in_array(get_class($object), $persistable)) { |
|
43
|
|
|
$this->getEntityManager()->persist($object); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->getEntityManager()->flush(); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function getPersistableClasses() |
|
53
|
|
|
{ |
|
54
|
|
|
$persistable = array(); |
|
55
|
|
|
$metadatas = $this->getEntityManager()->getMetadataFactory()->getAllMetadata(); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($metadatas as $metadata) { |
|
58
|
|
|
if (isset($metadata->isEmbeddedClass) && $metadata->isEmbeddedClass) { |
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$persistable[] = $metadata->getName(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $persistable; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function registerCache($loader) |
|
69
|
|
|
{ |
|
70
|
|
|
foreach ($loader->getCache() as $cache) { |
|
71
|
|
|
list($values, $entity) = $cache; |
|
72
|
|
|
$reflection = new \ReflectionClass($entity); |
|
|
|
|
|
|
73
|
|
View Code Duplication |
do { |
|
|
|
|
|
|
74
|
|
|
$this |
|
75
|
|
|
->getRecordBag() |
|
76
|
|
|
->getCollection($reflection->getName()) |
|
77
|
|
|
->attach($entity, $values) |
|
78
|
|
|
; |
|
79
|
|
|
$reflection = $reflection->getParentClass(); |
|
80
|
|
|
} while (false !== $reflection); |
|
81
|
|
|
} |
|
82
|
|
|
$loader->clearCache(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function resolveDepsFromArray(array $fixtures) |
|
86
|
|
|
{ |
|
87
|
|
|
$result = []; |
|
88
|
|
|
|
|
89
|
|
|
foreach ($fixtures as $fixture) { |
|
90
|
|
|
$this->resolveDeps($fixture, $result); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function resolveDeps($fixture, array &$result = []) |
|
97
|
|
|
{ |
|
98
|
|
|
$result[] = $fixture; |
|
99
|
|
|
$tree = $this->getParameter('friendly.alice.dependencies'); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
if (!empty($tree[$fixture])) { |
|
102
|
|
|
foreach ($tree[$fixture] as $dep) { |
|
103
|
|
|
if (!in_array($dep, $result)) { |
|
104
|
|
|
$this->resolveDeps($dep, $result); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $result; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.