1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shapin\Datagen\DBAL; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Schema\Schema; |
9
|
|
|
use Shapin\Datagen\FixtureInterface; |
10
|
|
|
use Shapin\Datagen\ProcessorInterface; |
11
|
|
|
use Shapin\Datagen\ReferenceManager; |
12
|
|
|
|
13
|
|
|
class Processor implements ProcessorInterface |
14
|
|
|
{ |
15
|
|
|
private $connection; |
16
|
|
|
private $referenceManager; |
17
|
|
|
private $schema; |
18
|
|
|
|
19
|
|
|
private $fixturesToLoad = []; |
20
|
|
|
|
21
|
5 |
|
public function __construct(Connection $connection, ReferenceManager $referenceManager) |
22
|
|
|
{ |
23
|
5 |
|
$this->connection = $connection; |
24
|
5 |
|
$this->referenceManager = $referenceManager; |
25
|
5 |
|
$this->schema = new Schema(); |
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
5 |
|
public function process(FixtureInterface $fixture, array $options = []): void |
32
|
|
|
{ |
33
|
5 |
|
if ($fixture instanceof Table && !$this->resolveOption($options, 'fixtures_only', false)) { |
34
|
4 |
|
$fixture->addTableToSchema($this->schema); |
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
if ($fixture instanceof FixtureCollection && !$this->resolveOption($options, 'schema_only', false)) { |
38
|
4 |
|
$this->fixturesToLoad[] = $fixture; |
39
|
|
|
} |
40
|
5 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
5 |
|
public function flush(array $options = []): void |
46
|
|
|
{ |
47
|
5 |
|
if (!$this->resolveOption($options, 'fixtures_only', false)) { |
48
|
4 |
|
$statements = $this->schema->toSql($this->connection->getDatabasePlatform()); |
49
|
4 |
|
foreach ($statements as $statement) { |
50
|
4 |
|
$this->connection->query($statement); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Reset schema |
54
|
4 |
|
$this->schema = new Schema(); |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
if ($this->resolveOption($options, 'schema_only', false)) { |
58
|
2 |
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
foreach ($this->fixturesToLoad as $fixtureCollection) { |
62
|
4 |
|
foreach ($fixtureCollection->getFixtures() as $key => $fixture) { |
63
|
4 |
|
$fields = $this->referenceManager->findAndReplace($fixture->getFields()); |
64
|
|
|
|
65
|
4 |
|
$this->connection->insert($fixture->getTableName(), $fields, $fixture->getTypes()); |
66
|
|
|
|
67
|
4 |
|
if (\is_string($key)) { |
68
|
|
|
$this->referenceManager->add($fixture->getTableName(), $key, $fields); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
2 |
|
public function getName(): string |
78
|
|
|
{ |
79
|
2 |
|
return 'dbal'; |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
private function resolveOption(array $options, string $key, $defaultValue = null) |
83
|
|
|
{ |
84
|
5 |
|
if (!\array_key_exists($key, $options)) { |
85
|
3 |
|
return $defaultValue; |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
return $options[$key]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.