1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebnetFr\DatabaseAnonymizer\Config; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Vlad Riabchenko <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
trait ConfigurationTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param ArrayNodeDefinition $node |
14
|
|
|
* |
15
|
|
|
* @author Vlad Riabchenko <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
public function configureAnonymizer(ArrayNodeDefinition $node) |
18
|
|
|
{ |
19
|
|
|
$node |
20
|
|
|
->children() |
21
|
|
|
->arrayNode('defaults') |
22
|
|
|
->scalarPrototype()->end() |
23
|
|
|
->end() |
|
|
|
|
24
|
|
|
->arrayNode('tables') |
25
|
|
|
->arrayPrototype() |
26
|
|
|
->children() |
27
|
|
|
->booleanNode('truncate')->defaultFalse()->end() |
28
|
|
|
->arrayNode('primary_key') |
29
|
|
|
->scalarPrototype()->end() |
30
|
|
|
->end() |
31
|
|
|
->arrayNode('fields') |
32
|
|
|
->arrayPrototype() |
33
|
|
|
->variablePrototype()->end() |
34
|
|
|
->end() |
35
|
|
|
->end() // fields |
36
|
|
|
->end() |
37
|
|
|
->end() |
38
|
|
|
->end() // tables |
39
|
|
|
->end() |
40
|
|
|
->beforeNormalization() |
41
|
|
|
// Pass default table configuration to field configuration. |
42
|
|
|
// defaults: |
43
|
|
|
// locale: fr_FR |
44
|
|
|
// seed: seed_key |
45
|
|
|
// tables: |
46
|
|
|
// user: |
47
|
|
|
// fields: |
48
|
|
|
// name: |
49
|
|
|
// generator: first_name |
50
|
|
|
// # locale "fr_FR" will be set here. |
51
|
|
|
// # seed "seed_key" will be set here. |
52
|
|
|
// lastname: |
53
|
|
|
// generator: first_name |
54
|
|
|
// locale: en_EN |
55
|
|
|
// seed: lastname_seed_key |
56
|
|
|
// # locale "en_EN" overwrites default value fr_FR. |
57
|
|
|
// # seed "lastname_seed_key" overwrites default value "seed_key". |
58
|
|
|
->ifTrue(static function ($v) { |
59
|
|
|
return \is_array($v) && \array_key_exists('defaults', $v) && \is_array($v['defaults']); |
60
|
|
|
}) |
61
|
|
|
->then(static function ($c) { |
62
|
|
|
if (isset($c['tables'])) { |
63
|
|
|
foreach ($c['tables'] as &$tableConfig) { |
64
|
|
|
if ($tableConfig['fields']) { |
65
|
|
|
foreach ($tableConfig['fields'] as &$fieldConfig) { |
66
|
|
|
if (isset($c['defaults'])) { |
67
|
|
|
foreach ($c['defaults'] as $defaultKey => $defaultValue) { |
68
|
|
|
if (!\array_key_exists($defaultKey, $fieldConfig)) { |
69
|
|
|
$fieldConfig[$defaultKey] = $defaultValue; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $c; |
79
|
|
|
}) |
80
|
|
|
->end() |
81
|
|
|
; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|