| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Faker\ORM\Spot;
|
|
|
4 |
|
|
|
5 |
use Spot\Locator;
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* Service class for populating a database using the Spot ORM.
|
|
|
9 |
*/
|
|
|
10 |
class Populator
|
|
|
11 |
{
|
|
|
12 |
protected $generator;
|
|
|
13 |
protected $locator;
|
|
|
14 |
protected $entities = [];
|
|
|
15 |
protected $quantities = [];
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Populator constructor.
|
|
|
19 |
*/
|
|
|
20 |
public function __construct(\Faker\Generator $generator, Locator $locator = null)
|
|
|
21 |
{
|
|
|
22 |
$this->generator = $generator;
|
|
|
23 |
$this->locator = $locator;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Add an order for the generation of $number records for $entity.
|
|
|
28 |
*
|
|
|
29 |
* @param string $entityName Name of Entity object to generate
|
|
|
30 |
* @param int $number The number of entities to populate
|
|
|
31 |
* @param array $customColumnFormatters
|
|
|
32 |
* @param array $customModifiers
|
|
|
33 |
* @param bool $useExistingData Should we use existing rows (e.g. roles) to populate relations?
|
|
|
34 |
*/
|
|
|
35 |
public function addEntity(
|
|
|
36 |
$entityName,
|
|
|
37 |
$number,
|
|
|
38 |
$customColumnFormatters = [],
|
|
|
39 |
$customModifiers = [],
|
|
|
40 |
$useExistingData = false
|
|
|
41 |
) {
|
|
|
42 |
$mapper = $this->locator->mapper($entityName);
|
|
|
43 |
|
|
|
44 |
if (null === $mapper) {
|
|
|
45 |
throw new \InvalidArgumentException('No mapper can be found for entity ' . $entityName);
|
|
|
46 |
}
|
|
|
47 |
$entity = new EntityPopulator($mapper, $this->locator, $useExistingData);
|
|
|
48 |
|
|
|
49 |
$entity->setColumnFormatters($entity->guessColumnFormatters($this->generator));
|
|
|
50 |
|
|
|
51 |
if ($customColumnFormatters) {
|
|
|
52 |
$entity->mergeColumnFormattersWith($customColumnFormatters);
|
|
|
53 |
}
|
|
|
54 |
$entity->mergeModifiersWith($customModifiers);
|
|
|
55 |
|
|
|
56 |
$this->entities[$entityName] = $entity;
|
|
|
57 |
$this->quantities[$entityName] = $number;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Populate the database using all the Entity classes previously added.
|
|
|
62 |
*
|
|
|
63 |
* @param Locator $locator A Spot locator
|
|
|
64 |
*
|
|
|
65 |
* @return array A list of the inserted PKs
|
|
|
66 |
*/
|
|
|
67 |
public function execute($locator = null)
|
|
|
68 |
{
|
|
|
69 |
if (null === $locator) {
|
|
|
70 |
$locator = $this->locator;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if (null === $locator) {
|
|
|
74 |
throw new \InvalidArgumentException('No entity manager passed to Spot Populator.');
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
$insertedEntities = [];
|
|
|
78 |
|
|
|
79 |
foreach ($this->quantities as $entityName => $number) {
|
|
|
80 |
for ($i = 0; $i < $number; ++$i) {
|
|
|
81 |
$insertedEntities[$entityName][] = $this->entities[$entityName]->execute(
|
|
|
82 |
$insertedEntities,
|
|
|
83 |
);
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return $insertedEntities;
|
|
|
88 |
}
|
|
|
89 |
}
|