|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Hautelook\AliceBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Baldur Rensch <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Hautelook\AliceBundle\Alice\DataFixtures; |
|
13
|
|
|
|
|
14
|
|
|
use Hautelook\AliceBundle\Alice\DataFixtures\Fixtures\Loader as FixturesLoader; |
|
15
|
|
|
use Hautelook\AliceBundle\Alice\DataFixtures\Fixtures\LoaderInterface as FixturesLoaderInterface; |
|
16
|
|
|
use Hautelook\AliceBundle\Alice\ProcessorChain; |
|
17
|
|
|
use Nelmio\Alice\PersisterInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Bootstraps the given loader to persist the objects retrieved by the loader. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Baldur Rensch <[email protected]> |
|
23
|
|
|
* @author Théo FIDRY <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Loader implements LoaderInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var FixturesLoaderInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $fixturesLoader; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ProcessorChain |
|
34
|
|
|
*/ |
|
35
|
|
|
private $processorChain; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
private $persistOnce; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
private $loadingLimit; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string[] |
|
49
|
|
|
*/ |
|
50
|
|
|
private $errorMessages; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param FixturesLoaderInterface $fixturesLoader |
|
54
|
|
|
* @param ProcessorChain $processorChain |
|
55
|
|
|
* @param bool $persistOnce |
|
56
|
|
|
* @param int $loadingLimit |
|
57
|
|
|
*/ |
|
58
|
42 |
|
public function __construct( |
|
59
|
|
|
FixturesLoaderInterface $fixturesLoader, |
|
60
|
|
|
ProcessorChain $processorChain, |
|
61
|
|
|
$persistOnce, |
|
62
|
|
|
$loadingLimit |
|
63
|
|
|
) { |
|
64
|
42 |
|
$this->fixturesLoader = $fixturesLoader; |
|
65
|
42 |
|
$this->processorChain = $processorChain; |
|
66
|
42 |
|
$this->persistOnce = $persistOnce; |
|
67
|
42 |
|
$this->loadingLimit = $loadingLimit; |
|
68
|
42 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
33 |
|
public function load(PersisterInterface $persister, array $fixturesFiles) |
|
74
|
|
|
{ |
|
75
|
33 |
|
if ($this->fixturesLoader instanceof FixturesLoader) { |
|
76
|
21 |
|
$_persister = $this->fixturesLoader->getPersister(); |
|
77
|
21 |
|
$this->fixturesLoader->setPersister($persister); |
|
78
|
21 |
|
} |
|
79
|
|
|
|
|
80
|
33 |
|
if (0 === count($fixturesFiles)) { |
|
81
|
3 |
|
return []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
30 |
|
$objects = []; |
|
85
|
30 |
|
$loadFileAttempts = 0; |
|
86
|
30 |
|
$normalizedFixturesFiles = $this->normalizeFixturesFiles($fixturesFiles); |
|
87
|
|
|
|
|
88
|
30 |
|
$this->errorMessages = []; |
|
89
|
30 |
|
while (true) { |
|
90
|
30 |
|
$objects = array_merge($objects, $this->tryToLoadFiles($persister, $normalizedFixturesFiles, $objects)); |
|
91
|
|
|
|
|
92
|
30 |
|
if (true === $this->areAllFixturesLoaded($normalizedFixturesFiles)) { |
|
93
|
24 |
|
break; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
6 |
|
if ($this->loadingLimit <= $loadFileAttempts) { |
|
97
|
6 |
|
throw new LoadingLimitException($this->loadingLimit, $normalizedFixturesFiles, $this->errorMessages); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
++$loadFileAttempts; |
|
101
|
6 |
|
} |
|
102
|
|
|
|
|
103
|
24 |
|
if (true === $this->persistOnce) { |
|
104
|
6 |
|
$this->persist($persister, $objects); |
|
105
|
6 |
|
} |
|
106
|
|
|
|
|
107
|
24 |
|
if (isset($_persister)) { |
|
108
|
12 |
|
$this->fixturesLoader->setPersister($_persister); |
|
109
|
12 |
|
} |
|
110
|
|
|
|
|
111
|
24 |
|
return $objects; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return ProcessorChain |
|
116
|
|
|
*/ |
|
117
|
27 |
|
public function getProcessorChain() |
|
118
|
|
|
{ |
|
119
|
27 |
|
return $this->processorChain; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
27 |
|
public function getProcessors() |
|
126
|
|
|
{ |
|
127
|
27 |
|
return $this->getProcessorChain()->getProcessors(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
12 |
|
public function getPersistOnce() |
|
134
|
|
|
{ |
|
135
|
12 |
|
return $this->persistOnce; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
6 |
|
public function getLoadingLimit() |
|
142
|
|
|
{ |
|
143
|
6 |
|
return $this->loadingLimit; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
30 |
|
private function areAllFixturesLoaded(array $normalizedFixturesFiles) |
|
147
|
|
|
{ |
|
148
|
30 |
|
foreach ($normalizedFixturesFiles as $fileRealPath => $fileHasBeenLoaded) { |
|
149
|
30 |
|
if (false === $fileHasBeenLoaded) { |
|
150
|
6 |
|
return false; |
|
151
|
|
|
} |
|
152
|
24 |
|
} |
|
153
|
|
|
|
|
154
|
24 |
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns an array where the key is the fixture file path and the value is the boolean false value. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string[] $fixturesFiles |
|
161
|
|
|
* |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
30 |
|
private function normalizeFixturesFiles(array $fixturesFiles) |
|
165
|
|
|
{ |
|
166
|
30 |
|
$normalizedFixturesFiles = array_flip($fixturesFiles); |
|
167
|
30 |
|
foreach ($normalizedFixturesFiles as $fileRealPath => $index) { |
|
168
|
30 |
|
$normalizedFixturesFiles[$fileRealPath] = false; |
|
169
|
30 |
|
} |
|
170
|
|
|
|
|
171
|
30 |
|
return $normalizedFixturesFiles; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Goes through all fixtures files to try to load them one by one and specify for each if the file could |
|
176
|
|
|
* successfuly be loaded or not. |
|
177
|
|
|
* |
|
178
|
|
|
* @param PersisterInterface $persister |
|
179
|
|
|
* @param array $normalizedFixturesFiles Array with the file real path as key and true as a value if |
|
180
|
|
|
* the files has been loaded. |
|
181
|
|
|
* @param array $references |
|
182
|
|
|
* |
|
183
|
|
|
* @return \object[] All objects that could have been loaded. |
|
184
|
|
|
*/ |
|
185
|
30 |
|
private function tryToLoadFiles(PersisterInterface $persister, array &$normalizedFixturesFiles, array $references) |
|
186
|
|
|
{ |
|
187
|
30 |
|
$objects = []; |
|
188
|
30 |
|
foreach ($normalizedFixturesFiles as $fixtureFilePath => $hasBeenLoaded) { |
|
189
|
30 |
|
if (true === $hasBeenLoaded) { |
|
190
|
|
|
continue; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
try { |
|
194
|
30 |
|
$dataSet = $this->fixturesLoader->load($fixtureFilePath, $references); |
|
195
|
24 |
|
$normalizedFixturesFiles[$fixtureFilePath] = true; |
|
196
|
|
|
|
|
197
|
24 |
|
if (false === $this->persistOnce) { |
|
198
|
18 |
|
$this->persist($persister, $dataSet); |
|
199
|
18 |
|
} |
|
200
|
|
|
|
|
201
|
24 |
|
$objects = array_merge($objects, $dataSet); |
|
202
|
30 |
|
} catch (\UnexpectedValueException $exception) { |
|
203
|
6 |
|
$this->registerErrorMessage($fixtureFilePath, $exception->getMessage()); |
|
204
|
|
|
} |
|
205
|
30 |
|
} |
|
206
|
|
|
|
|
207
|
30 |
|
return $objects; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Uses the Fixture persister to persist objects and calling the processors. |
|
212
|
|
|
* |
|
213
|
|
|
* @param PersisterInterface $persister |
|
214
|
|
|
* @param object[] $objects |
|
215
|
|
|
*/ |
|
216
|
24 |
|
private function persist(PersisterInterface $persister, array $objects) |
|
217
|
|
|
{ |
|
218
|
24 |
|
foreach ($this->getProcessors() as $processor) { |
|
219
|
12 |
|
foreach ($objects as $object) { |
|
220
|
12 |
|
$processor->preProcess($object); |
|
221
|
12 |
|
} |
|
222
|
24 |
|
} |
|
223
|
|
|
|
|
224
|
24 |
|
$persister->persist($objects); |
|
225
|
|
|
|
|
226
|
24 |
|
foreach ($this->getProcessors() as $processor) { |
|
227
|
12 |
|
foreach ($objects as $object) { |
|
228
|
12 |
|
$processor->postProcess($object); |
|
229
|
12 |
|
} |
|
230
|
24 |
|
} |
|
231
|
24 |
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Registers the error message with the related fixture file. |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $fixtureFilePath |
|
237
|
|
|
* @param string $errorMessage |
|
238
|
|
|
*/ |
|
239
|
6 |
|
private function registerErrorMessage($fixtureFilePath, $errorMessage) |
|
240
|
|
|
{ |
|
241
|
6 |
|
if (true === empty($errorMessage)) { |
|
242
|
6 |
|
return; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
if (!isset($this->errorMessages[$fixtureFilePath])) { |
|
246
|
|
|
$this->errorMessages[$fixtureFilePath] = []; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
array_push($this->errorMessages[$fixtureFilePath], $errorMessage); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|