|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the DoyoUserBundle project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Doyo\UserBundle\DependencyInjection; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Config\ConfigCache; |
|
17
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
18
|
|
|
use Symfony\Component\Config\FileLocator; |
|
19
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
|
23
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
25
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
26
|
|
|
|
|
27
|
|
|
class DoyoUserExtension extends Extension implements PrependExtensionInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $doctrineDrivers = [ |
|
33
|
|
|
'orm' => [ |
|
34
|
|
|
'registry' => 'doctrine', |
|
35
|
|
|
'tag' => 'doctrine.event_subscriber', |
|
36
|
|
|
], |
|
37
|
|
|
'mongodb' => [ |
|
38
|
|
|
'registry' => 'doctrine_mongodb', |
|
39
|
|
|
'tag' => 'doctrine_mongodb.odm.event_subscriber', |
|
40
|
|
|
], |
|
41
|
|
|
'couchdb' => [ |
|
42
|
|
|
'registry' => 'doctrine_couchdb', |
|
43
|
|
|
'tag' => 'doctrine_couchdb.event_subscriber', |
|
44
|
|
|
'listener_class' => 'Doyo\UserBundle\Bridge\CouchDB\UserListener', |
|
45
|
|
|
], |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
3 |
|
public function prepend(ContainerBuilder $container) |
|
49
|
|
|
{ |
|
50
|
3 |
|
$container->prependExtensionConfig('api_platform',[ |
|
51
|
|
|
'mapping' => [ |
|
52
|
|
|
'paths' => [ |
|
53
|
3 |
|
__DIR__.'/../Resources/config/api_resources' |
|
54
|
|
|
] |
|
55
|
|
|
] |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
public function load(array $configs, ContainerBuilder $container) |
|
60
|
|
|
{ |
|
61
|
3 |
|
$processor = new Processor(); |
|
62
|
3 |
|
$configuration = new Configuration(); |
|
63
|
|
|
|
|
64
|
3 |
|
$config = $processor->processConfiguration($configuration, $configs); |
|
65
|
|
|
|
|
66
|
3 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
67
|
|
|
|
|
68
|
|
|
$map = [ |
|
69
|
3 |
|
'model_manager_name', |
|
70
|
|
|
'api_platform', |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
3 |
|
$container->setParameter('doyo_user.model.user.class', $config['user_class']); |
|
74
|
|
|
|
|
75
|
3 |
|
foreach($map as $key){ |
|
|
|
|
|
|
76
|
3 |
|
$container->setParameter('doyo_user.'.$key, $config[$key]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
$loader->load('util.xml'); |
|
80
|
3 |
|
$loader->load('command.xml'); |
|
81
|
|
|
|
|
82
|
3 |
|
$this->loadDbDriver($loader, $container, $config); |
|
83
|
|
|
|
|
84
|
3 |
|
if (!empty($config['group'])) { |
|
85
|
1 |
|
$this->loadGroups($config['group'], $container, $loader, $config['db_driver']); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
$container->setParameter('doyo_user.storage', $config['db_driver']); |
|
89
|
|
|
|
|
90
|
3 |
|
$container->setAlias('doyo_user.util.email_canonicalizer', $config['service']['email_canonicalizer']); |
|
91
|
3 |
|
$container->setAlias('doyo_user.util.username_canonicalizer', $config['service']['username_canonicalizer']); |
|
92
|
3 |
|
$container->setAlias('doyo_user.util.password_updater', $config['service']['password_updater']); |
|
93
|
3 |
|
$container->setAlias('doyo_user.user_manager', $config['service']['user_manager']); |
|
94
|
|
|
|
|
95
|
3 |
|
if ($config['api_platform']) { |
|
96
|
1 |
|
$loader->load('api-platform.xml'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
if(!empty($config['group'])){ |
|
|
|
|
|
|
100
|
1 |
|
$this->loadGroups($config['group'], $container, $loader, $config['db_driver']); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param array $config |
|
106
|
|
|
* @param ContainerBuilder $container |
|
107
|
|
|
* @param XmlFileLoader $loader |
|
108
|
|
|
* @param $dbDriver |
|
109
|
|
|
* @throws \Exception |
|
110
|
|
|
*/ |
|
111
|
1 |
|
private function loadGroups(array $config, ContainerBuilder $container, XmlFileLoader $loader, $dbDriver) |
|
112
|
|
|
{ |
|
113
|
1 |
|
if ('custom' !== $dbDriver) { |
|
114
|
1 |
|
if (isset(self::$doctrineDrivers[$dbDriver])) { |
|
115
|
1 |
|
$loader->load('group.xml'); |
|
116
|
|
|
} else { |
|
117
|
|
|
$loader->load(sprintf('%s_group.xml', $dbDriver)); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
$container->setAlias('doyo_user.group_manager', new Alias($config['group_manager'], true)); |
|
122
|
1 |
|
$container->setAlias('Doyo\UserBundle\Model\GroupManagerInterface', new Alias('doyo_user.group_manager', false)); |
|
123
|
|
|
|
|
124
|
1 |
|
$this->remapParametersNamespaces($config, $container, array( |
|
125
|
|
|
'' => array( |
|
126
|
1 |
|
'group_class' => 'model.group.class', |
|
127
|
|
|
) |
|
128
|
|
|
)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
3 |
|
private function loadDbDriver(XmlFileLoader $loader, ContainerBuilder $container, $config) |
|
132
|
|
|
{ |
|
133
|
3 |
|
if ('custom' !== $config['db_driver']) { |
|
134
|
3 |
|
if (isset(self::$doctrineDrivers[$config['db_driver']])) { |
|
135
|
3 |
|
$loader->load('doctrine.xml'); |
|
136
|
3 |
|
$container->setAlias('doyo_user.doctrine_registry', new Alias(self::$doctrineDrivers[$config['db_driver']]['registry'], false)); |
|
137
|
|
|
} else { |
|
138
|
|
|
$loader->load(sprintf('%s.xml', $config['db_driver'])); |
|
139
|
|
|
} |
|
140
|
3 |
|
$container->setParameter($this->getAlias() . '.backend_type_' . $config['db_driver'], true); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
3 |
|
if (isset(self::$doctrineDrivers[$config['db_driver']])) { |
|
144
|
3 |
|
$definition = $container->getDefinition('doyo_user.object_manager'); |
|
145
|
3 |
|
$definition->setFactory([new Reference('doyo_user.doctrine_registry'), 'getManager']); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param array $config |
|
151
|
|
|
* @param ContainerBuilder $container |
|
152
|
|
|
* @param array $map |
|
153
|
|
|
*/ |
|
154
|
1 |
|
protected function remapParameters(array $config, ContainerBuilder $container, array $map) |
|
155
|
|
|
{ |
|
156
|
1 |
|
foreach ($map as $name => $paramName) { |
|
157
|
1 |
|
if (array_key_exists($name, $config)) { |
|
158
|
1 |
|
$container->setParameter('doyo_user.'.$paramName, $config[$name]); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param array $config |
|
165
|
|
|
* @param ContainerBuilder $container |
|
166
|
|
|
* @param array $namespaces |
|
167
|
|
|
*/ |
|
168
|
1 |
|
protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces) |
|
169
|
|
|
{ |
|
170
|
1 |
|
foreach ($namespaces as $ns => $map) { |
|
171
|
1 |
|
if ($ns) { |
|
172
|
|
|
if (!array_key_exists($ns, $config)) { |
|
173
|
|
|
continue; |
|
174
|
|
|
} |
|
175
|
|
|
$namespaceConfig = $config[$ns]; |
|
176
|
|
|
} else { |
|
177
|
1 |
|
$namespaceConfig = $config; |
|
178
|
|
|
} |
|
179
|
1 |
|
if (is_array($map)) { |
|
180
|
1 |
|
$this->remapParameters($namespaceConfig, $container, $map); |
|
181
|
|
|
} else { |
|
182
|
|
|
foreach ($namespaceConfig as $name => $value) { |
|
183
|
|
|
$container->setParameter('doyo_user.'.sprintf($map, $name), $value); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|