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