1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Khepin\YamlFixturesBundle\Loader; |
4
|
|
|
|
5
|
|
|
use Khepin\YamlFixturesBundle\Fixture\YamlAclFixture; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
10
|
|
|
|
11
|
|
|
class YamlLoader |
12
|
|
|
{ |
13
|
|
|
protected $bundles; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @var type |
18
|
|
|
*/ |
19
|
|
|
protected $kernel; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Doctrine entity manager |
23
|
|
|
* @var type |
24
|
|
|
*/ |
25
|
|
|
protected $object_manager; |
26
|
|
|
|
27
|
|
|
protected $acl_manager = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Array of all yml files containing fixtures that should be loaded |
31
|
|
|
* @var type |
32
|
|
|
*/ |
33
|
|
|
protected $fixture_files = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Maintains references to already created objects |
37
|
|
|
* @var type |
38
|
|
|
*/ |
39
|
|
|
protected $references = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The directory containing the fixtures files |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $directory; |
47
|
|
|
|
48
|
|
|
public function __construct(KernelInterface $kernel, $bundles, $directory) |
49
|
|
|
{ |
50
|
|
|
$this->bundles = $bundles; |
51
|
|
|
$this->kernel = $kernel; |
|
|
|
|
52
|
|
|
$this->directory = $directory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* @param type $manager |
58
|
|
|
*/ |
59
|
|
|
public function setAclManager($manager = null) |
60
|
|
|
{ |
61
|
|
|
$this->acl_manager = $manager; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns a previously saved reference |
66
|
|
|
* @param type $reference_name |
67
|
|
|
* @return type |
68
|
|
|
*/ |
69
|
|
|
public function getReference($reference_name) |
70
|
|
|
{ |
71
|
|
|
return !is_null($reference_name) ? $this->references[$reference_name] : null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Sets a reference to an object |
76
|
|
|
* @param type $name |
77
|
|
|
* @param type $object |
78
|
|
|
*/ |
79
|
|
|
public function setReference($name, $object) |
80
|
|
|
{ |
81
|
|
|
$this->references[$name] = $object; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets all fixtures files |
86
|
|
|
*/ |
87
|
|
|
protected function loadFixtureFiles() |
88
|
|
|
{ |
89
|
|
|
foreach ($this->bundles as $bundle) { |
90
|
|
|
$file = '*'; |
91
|
|
|
if (strpos($bundle, '/')) { |
92
|
|
|
list($bundle, $file) = explode('/', $bundle); |
93
|
|
|
} |
94
|
|
|
$path = $this->kernel->locateResource('@' . $bundle); |
95
|
|
|
$files = glob($path . $this->directory . '/'.$file.'.yml'); |
96
|
|
|
$this->fixture_files = array_unique(array_merge($this->fixture_files, $files)); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Loads the fixtures file by file and saves them to the database |
102
|
|
|
*/ |
103
|
|
|
public function loadFixtures() |
104
|
|
|
{ |
105
|
|
|
$this->loadFixtureFiles(); |
106
|
|
|
foreach ($this->fixture_files as $file) { |
107
|
|
|
$fixture_data = Yaml::parse(file_get_contents($file)); |
108
|
|
|
// if nothing is specified, we use doctrine orm for persistence |
109
|
|
|
$persistence = isset($fixture_data['persistence']) ? $fixture_data['persistence'] : 'orm'; |
110
|
|
|
|
111
|
|
|
$persister = $this->getPersister($persistence); |
112
|
|
|
$manager = $persister->getManagerForClass($fixture_data['model']); |
113
|
|
|
|
114
|
|
|
$fixture = $this->getFixtureClass($persistence); |
115
|
|
|
$fixture = new $fixture($fixture_data, $this, $file); |
116
|
|
|
$fixture->load($manager, func_get_args()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (!is_null($this->acl_manager)) { |
120
|
|
|
foreach ($this->fixture_files as $file) { |
121
|
|
|
$fixture = new YamlAclFixture($file, $this); |
122
|
|
|
$fixture->load($this->acl_manager, func_get_args()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Remove all fixtures from the database |
129
|
|
|
*/ |
130
|
|
|
public function purgeDatabase($persistence, $databaseName = null, $withTruncate = false) |
131
|
|
|
{ |
132
|
|
|
$purgetools = array( |
133
|
|
|
'orm' => array( |
134
|
|
|
'purger' => 'Doctrine\Common\DataFixtures\Purger\ORMPurger', |
135
|
|
|
'executor' => 'Doctrine\Common\DataFixtures\Executor\ORMExecutor', |
136
|
|
|
), |
137
|
|
|
'mongodb' => array( |
138
|
|
|
'purger' => 'Doctrine\Common\DataFixtures\Purger\MongoDBPurger', |
139
|
|
|
'executor' => 'Doctrine\Common\DataFixtures\Executor\MongoDBExecutor', |
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
// Retrieve the correct purger and executor |
143
|
|
|
$purge_class = $purgetools[$persistence]['purger']; |
144
|
|
|
$executor_class = $purgetools[$persistence]['executor']; |
145
|
|
|
|
146
|
|
|
// Instanciate purger and executor |
147
|
|
|
$persister = $this->getPersister($persistence); |
148
|
|
|
$entityManagers = ($databaseName) |
149
|
|
|
? array($persister->getManager($databaseName)) |
150
|
|
|
: $persister->getManagers(); |
151
|
|
|
|
152
|
|
|
foreach ($entityManagers as $entityManager) { |
153
|
|
|
$purger = new $purge_class($entityManager); |
154
|
|
|
if ($withTruncate && $purger instanceof ORMPurger) { |
155
|
|
|
$purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE); |
156
|
|
|
} |
157
|
|
|
$executor = new $executor_class($entityManager, $purger); |
158
|
|
|
// purge |
159
|
|
|
$executor->purge(); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/* |
164
|
|
|
* Returns the doctrine persister for the given persistence layer |
165
|
|
|
* @return ManagerRegistry |
166
|
|
|
*/ |
167
|
|
|
public function getPersister($persistence) |
168
|
|
|
{ |
169
|
|
|
$managers = array( |
170
|
|
|
'orm' => 'doctrine', |
171
|
|
|
'mongodb' => 'doctrine_mongodb', |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
return $this->kernel->getContainer()->get($managers[$persistence]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string classname |
179
|
|
|
*/ |
180
|
|
|
public function getFixtureClass($persistence) |
181
|
|
|
{ |
182
|
|
|
$classes = array( |
183
|
|
|
'orm' => 'Khepin\YamlFixturesBundle\Fixture\OrmYamlFixture', |
184
|
|
|
'mongodb' => 'Khepin\YamlFixturesBundle\Fixture\MongoYamlFixture' |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
return $classes[$persistence]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return the service with given id |
192
|
|
|
*/ |
193
|
|
|
public function getService($service_id) |
194
|
|
|
{ |
195
|
|
|
return $this->kernel->getContainer()->get($service_id); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..