1 | <?php |
||
2 | |||
3 | namespace DH\AuditorBundle\DependencyInjection; |
||
4 | |||
5 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
||
6 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
||
7 | use Symfony\Component\Config\Definition\ConfigurationInterface; |
||
8 | |||
9 | class Configuration implements ConfigurationInterface |
||
10 | { |
||
11 | /** |
||
12 | * Generates the configuration tree builder. |
||
13 | */ |
||
14 | public function getConfigTreeBuilder(): TreeBuilder |
||
15 | { |
||
16 | $treeBuilder = new TreeBuilder('dh_auditor'); |
||
17 | |||
18 | $this->getRootNode($treeBuilder, 'dh_auditor') |
||
19 | ->children() |
||
20 | ->booleanNode('enabled') |
||
21 | ->defaultTrue() |
||
22 | ->end() |
||
23 | ->scalarNode('timezone') |
||
24 | ->defaultValue('UTC') |
||
25 | ->end() |
||
26 | ->append($this->getProvidersNode()) |
||
27 | ->end() |
||
28 | ; |
||
29 | |||
30 | return $treeBuilder; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Proxy to get root node for Symfony < 4.2. |
||
35 | */ |
||
36 | protected function getRootNode(TreeBuilder $treeBuilder, string $name): ArrayNodeDefinition |
||
37 | { |
||
38 | if (method_exists($treeBuilder, 'getRootNode')) { |
||
39 | return $treeBuilder->getRootNode(); |
||
1 ignored issue
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
40 | } |
||
41 | |||
42 | return $treeBuilder->root($name); |
||
43 | } |
||
44 | |||
45 | private function getProvidersNode(): ArrayNodeDefinition |
||
46 | { |
||
47 | $treeBuilder = new TreeBuilder('providers'); |
||
48 | |||
49 | return $this->getRootNode($treeBuilder, 'providers') |
||
50 | ->requiresAtLeastOneElement() |
||
51 | ->useAttributeAsKey('name') |
||
52 | ->variablePrototype() |
||
53 | ->validate() |
||
54 | ->ifEmpty() |
||
55 | ->thenInvalid('Invalid provider configuration %s') |
||
56 | ->end() |
||
57 | ->end() |
||
58 | |||
59 | ->validate() |
||
60 | ->always() |
||
61 | ->then(function ($v) { |
||
62 | if (!\array_key_exists('doctrine', $v)) { |
||
63 | $v['doctrine'] = []; |
||
64 | } |
||
65 | |||
66 | // "table_prefix" is empty by default. |
||
67 | if (!\array_key_exists('table_prefix', $v['doctrine']) || !\is_string($v['doctrine']['table_prefix'])) { |
||
68 | $v['doctrine']['table_prefix'] = ''; |
||
69 | } |
||
70 | |||
71 | // "table_suffix" is "_audit" by default. |
||
72 | if (!\array_key_exists('table_suffix', $v['doctrine']) || !\is_string($v['doctrine']['table_suffix'])) { |
||
73 | $v['doctrine']['table_suffix'] = '_audit'; |
||
74 | } |
||
75 | |||
76 | // "entities" are "enabled" by default. |
||
77 | if (\array_key_exists('entities', $v['doctrine']) && \is_array($v['doctrine']['entities'])) { |
||
78 | foreach ($v['doctrine']['entities'] as $entity => $options) { |
||
79 | if (null === $options || !\array_key_exists('enabled', $options)) { |
||
80 | $v['doctrine']['entities'][$entity]['enabled'] = true; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | // "doctrine.orm.default_entity_manager" is the default "storage_services" |
||
86 | if (\array_key_exists('storage_services', $v['doctrine']) && \is_string($v['doctrine']['storage_services'])) { |
||
87 | $v['doctrine']['storage_services'] = [$v['doctrine']['storage_services']]; |
||
88 | } elseif (!\array_key_exists('storage_services', $v['doctrine']) || !\is_array($v['doctrine']['storage_services'])) { |
||
89 | $v['doctrine']['storage_services'] = ['doctrine.orm.default_entity_manager']; |
||
90 | } |
||
91 | |||
92 | // "doctrine.orm.default_entity_manager" is the default "auditing_services" |
||
93 | if (\array_key_exists('auditing_services', $v['doctrine']) && \is_string($v['doctrine']['auditing_services'])) { |
||
94 | $v['doctrine']['auditing_services'] = [$v['doctrine']['auditing_services']]; |
||
95 | } elseif (!\array_key_exists('auditing_services', $v['doctrine']) || !\is_array($v['doctrine']['auditing_services'])) { |
||
96 | $v['doctrine']['auditing_services'] = ['doctrine.orm.default_entity_manager']; |
||
97 | } |
||
98 | |||
99 | // "viewer" is enabled by default |
||
100 | if (!\array_key_exists('viewer', $v['doctrine']) || !\is_bool($v['doctrine']['viewer'])) { |
||
101 | $v['doctrine']['viewer'] = true; |
||
102 | } |
||
103 | |||
104 | // "storage_mapper" is null by default |
||
105 | if (!\array_key_exists('storage_mapper', $v['doctrine']) || !\is_string($v['doctrine']['storage_mapper'])) { |
||
106 | $v['doctrine']['storage_mapper'] = null; |
||
107 | } |
||
108 | |||
109 | // "role_checker" is null by default |
||
110 | if (!\array_key_exists('role_checker', $v['doctrine']) || !\is_string($v['doctrine']['role_checker'])) { |
||
111 | $v['doctrine']['role_checker'] = 'dh_auditor.role_checker'; |
||
112 | } |
||
113 | |||
114 | // "user_provider" is null by default |
||
115 | if (!\array_key_exists('user_provider', $v['doctrine']) || !\is_string($v['doctrine']['user_provider'])) { |
||
116 | $v['doctrine']['user_provider'] = 'dh_auditor.user_provider'; |
||
117 | } |
||
118 | |||
119 | // "security_provider" is null by default |
||
120 | if (!\array_key_exists('security_provider', $v['doctrine']) || !\is_string($v['doctrine']['security_provider'])) { |
||
121 | $v['doctrine']['security_provider'] = 'dh_auditor.security_provider'; |
||
122 | } |
||
123 | |||
124 | return $v; |
||
125 | }) |
||
126 | ->end() |
||
127 | ; |
||
128 | } |
||
129 | } |
||
130 |