-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.php
More file actions
458 lines (402 loc) · 11.7 KB
/
Loader.php
File metadata and controls
458 lines (402 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\Alice\Fixtures;
use Nelmio\Alice\Instances\Processor\Methods\Faker;
use Nelmio\Alice\Fixtures\ParameterBag;
use Psr\Log\LoggerInterface;
use Nelmio\Alice\PersisterInterface;
use Nelmio\Alice\Instances\Collection;
use Nelmio\Alice\Instances\Instantiator;
use Nelmio\Alice\Instances\Populator;
use Nelmio\Alice\Instances\Processor;
use Nelmio\Alice\Instances\Processor\Providers\IdentityProvider;
use Nelmio\Alice\Util\TypeHintChecker;
/**
* Loads fixtures from an array or file
*/
class Loader
{
/**
* @var Collection
*/
protected $objects;
/**
* @var TypeHintChecker
*/
protected $typeHintChecker;
/**
* @var Parser
**/
protected $parser;
/**
* @var Builder
*/
protected $builder;
/**
* @var Faker
*/
protected $fakerProcessorMethod;
/**
* @var Instantiator
*/
protected $instantiator;
/**
* @var Populator
*/
protected $populator;
/**
* @var PersisterInterface
*/
protected $manager;
/**
* @var \Nelmio\Alice\Fixtures\ParameterBag
*/
protected $parameterBag;
/**
* @var callable|LoggerInterface
*/
private $logger;
/**
* @param string $locale default locale to use with faker if none is
* specified in the expression
* @param array $providers custom faker providers in addition to the default
* ones from faker
* @param int $seed a seed to make sure faker generates data consistently across
* runs, set to null to disable
* @param array $parameters create loader with default parameters
*/
public function __construct($locale = 'en_US', array $providers = [], $seed = 1, array $parameters = [])
{
$this->objects = new Collection;
$this->typeHintChecker = new TypeHintChecker;
$this->parameterBag = new ParameterBag($parameters);
$allProviders = array_merge($this->getBuiltInProviders(), $providers);
$this->processor = new Processor\Processor(
$this->objects,
$this->getBuiltInProcessors($allProviders, $locale)
);
$this->parser = new Parser\Parser(
$this->getBuiltInParsers()
);
$this->builder = new Builder\Builder(
$this->getBuiltInBuilders()
);
$this->instantiator = new Instantiator\Instantiator(
$this->getBuiltInInstantiators($this->processor, $this->typeHintChecker),
$this->processor
);
$this->populator = new Populator\Populator(
$this->objects,
$this->processor,
$this->getBuiltInPopulators($this->typeHintChecker)
);
if (is_numeric($seed)) {
mt_srand($seed);
}
}
/**
* Loads a fixture file
*
* @param string|array $dataOrFilename data array or filename
*/
public function load($dataOrFilename)
{
// ensure our data is loaded
$data = !is_array($dataOrFilename) ? $this->parseFile($dataOrFilename) : $dataOrFilename;
// create fixtures
$newFixtures = $this->buildFixtures($data);
// instantiate fixtures
$this->instantiateFixtures($newFixtures);
// populate objects
return $this->populateObjects($newFixtures);
}
/**
* Returns a reference to a fixture by name
*
* @param string $name
* @param string $property optionally return only a given property of the reference
* @return object
*/
public function getReference($name, $property = null)
{
return $this->objects->find($name, $property);
}
/**
* Returns all references created by the loader
*
* @return array[object]
*/
public function getReferences()
{
return $this->objects->toArray();
}
/**
* @param array $providers
*/
public function setProviders(array $providers)
{
$this->fakerProcessorMethod->setProviders(array_merge($this->getBuiltInProviders(), $providers));
}
/**
* @param object|array $provider Provider or array of providers
*/
public function addProvider($provider)
{
$this->fakerProcessorMethod->addProvider($provider);
}
/**
* @param array $references
*/
public function setReferences(array $objects)
{
$this->objects->clear();
foreach ($objects as $name => $object) {
$this->objects->set($name, $object);
}
}
/**
* adds a processor for processing extensions
*
* @param Processor\Methods\MethodInterface $processor
**/
public function addProcessor(Processor\Methods\MethodInterface $processor)
{
$this->processor->addProcessor($processor);
}
/**
* adds a parser for fixture parsing extensions
*
* @param Parser\Methods\MethodInterface $parser
**/
public function addParser(Parser\Methods\MethodInterface $parser)
{
$this->parser->addParser($parser);
}
/**
* adds a builder for fixture building extensions
*
* @param Builder\Methods\MethodInterface $builder
**/
public function addBuilder(Builder\Methods\MethodInterface $builder)
{
$this->builder->addBuilder($builder);
}
/**
* adds an instantiator for instantiation extensions
*
* @param Instantiator\Methods\MethodInterface $instantiator
**/
public function addInstantiator(Instantiator\Methods\MethodInterface $instantiator)
{
$this->instantiator->addInstantiator($instantiator);
}
/**
* adds a populator for population extensions
*
* @param Populator\Methods\MethodInterface $populator
**/
public function addPopulator(Populator\Methods\MethodInterface $populator)
{
$this->populator->addPopulator($populator);
}
/**
* parses a file at the given filename
*
* @param string filename
* @return array data
*/
protected function parseFile($filename)
{
return $this->parser->parse($filename);
}
/**
* builds a collection of fixtures
*
* @param array $rawData
* @return array
*/
protected function buildFixtures(array $rawData)
{
$fixtures = [];
foreach ($rawData as $class => $specs) {
$this->log('Loading '.$class);
foreach ($specs as $name => $spec) {
$fixtures[] = $this->builder->build($class, $name, $spec);
}
}
return $fixtures ? call_user_func_array('array_merge', $fixtures) : [];
}
/**
* creates an empty instance for each fixture, and adds it to our object collection
*
* @param array $fixtures
*/
protected function instantiateFixtures(array $fixtures)
{
foreach ($fixtures as $fixture) {
$this->objects->set(
$fixture->getName(),
$this->instantiator->instantiate($fixture)
);
}
}
/**
* hydrates each instance described by fixtures and returns the final non-local list
*
* @param array $fixtures
* @return array
*/
protected function populateObjects(array $fixtures)
{
$objects = [];
foreach ($fixtures as $fixture) {
$this->objects->set('self', $this->objects->get($fixture->getName()));
$this->populator->populate($fixture);
$this->objects->remove('self');
// add the object in the object store unless it's local
if (!$fixture->isLocal()) {
$objects[$fixture->getName()] = $this->getReference($fixture->getName());
}
}
return $objects;
}
/**
* public interface to set the Persister interface
*
* @param PersisterInterface $manager
*/
public function setPersister(PersisterInterface $manager)
{
$this->manager = $manager;
$this->typeHintChecker->setPersister($manager);
}
/**
* Set the logger callable to execute with the log() method.
*
* @param callable|LoggerInterface $logger
*/
public function setLogger($logger)
{
$this->logger = $logger;
}
/**
* @return ParameterBag
*/
public function getParameterBag()
{
return $this->parameterBag;
}
/**
* Logs a message using the logger.
*
* @param string $message
*/
public function log($message)
{
if ($this->logger instanceof LoggerInterface) {
$this->logger->debug($message);
} elseif ($logger = $this->logger) {
$logger($message);
}
}
/**
* @return Processor\Methods\Faker
*/
public function getFakerProcessorMethod()
{
return $this->fakerProcessorMethod;
}
/**
* returns a list of all the default providers faker processing
*
* @return array
*/
private function getBuiltInProviders()
{
return [new IdentityProvider()];
}
/**
* returns a list of all the default processor methods
*
* @param array $providers - a list of all providers to build the processors with
* @param string $locale
* @return array
*/
private function getBuiltInProcessors(array $providers, $locale)
{
$this->fakerProcessorMethod = new Processor\Methods\Faker($providers, $locale);
return [
new Processor\Methods\Parameterized($this->parameterBag),
new Processor\Methods\ArrayValue(),
new Processor\Methods\Conditional(),
$this->fakerProcessorMethod,
new Processor\Methods\Reference(),
new Processor\Methods\UnescapeAt(),
];
}
/**
* returns a list of all the default parser methods
*
* @return array
*/
private function getBuiltInParsers()
{
return [
new Parser\Methods\Php($this),
new Parser\Methods\Yaml($this),
];
}
/**
* returns a list of all the default builder methods
*
* @return array
*/
private function getBuiltInBuilders()
{
return [
new Builder\Methods\RangeName(),
new Builder\Methods\ListName(),
new Builder\Methods\SimpleName(),
];
}
/**
* returns a list of all the default instantiator methods
*
* @param Processor\Processor $processor
* @param TypeHintChecker $typeHintChecker
* @return array
*/
private function getBuiltInInstantiators(Processor\Processor $processor, TypeHintChecker $typeHintChecker)
{
return [
new Instantiator\Methods\Unserialize(),
new Instantiator\Methods\ReflectionWithoutConstructor(),
new Instantiator\Methods\ReflectionWithConstructor($processor, $typeHintChecker),
new Instantiator\Methods\EmptyConstructor(),
];
}
/**
* returns a list of all the default populator methods
*
* @param TypeHintChecker $typeHintChecker
* @return array
*/
private function getBuiltInPopulators(TypeHintChecker $typeHintChecker)
{
return [
new Populator\Methods\ArrayAdd($typeHintChecker),
new Populator\Methods\Custom(),
new Populator\Methods\ArrayDirect($typeHintChecker),
new Populator\Methods\Direct($typeHintChecker),
new Populator\Methods\Property(),
new Populator\Methods\MagicCall(),
];
}
}