|
3
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the FOSUserBundle package. |
|
|
5 |
* |
|
|
6 |
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
|
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 |
namespace FOS\UserBundle\DependencyInjection; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Config\Definition\Processor; |
|
|
15 |
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
|
16 |
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
|
17 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
18 |
use Symfony\Component\DependencyInjection\Alias; |
|
|
19 |
use Symfony\Component\Config\FileLocator; |
|
|
20 |
|
|
|
21 |
class FOSUserExtension extends Extension |
|
|
22 |
{ |
|
|
23 |
public function load(array $configs, ContainerBuilder $container) |
|
|
24 |
{ |
|
|
25 |
$processor = new Processor(); |
|
|
26 |
$configuration = new Configuration(); |
|
|
27 |
|
|
|
28 |
$config = $processor->processConfiguration($configuration, $configs); |
|
|
29 |
|
|
|
30 |
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
|
31 |
|
|
|
32 |
if (!in_array(strtolower($config['db_driver']), array('orm', 'mongodb', 'couchdb'))) { |
|
|
33 |
throw new \InvalidArgumentException(sprintf('Invalid db driver "%s".', $config['db_driver'])); |
|
|
34 |
} |
|
|
35 |
$loader->load(sprintf('%s.xml', $config['db_driver'])); |
|
|
36 |
|
|
|
37 |
foreach (array('validator', 'security', 'util', 'mailer') as $basename) { |
|
|
38 |
$loader->load(sprintf('%s.xml', $basename)); |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
$container->setAlias('fos_user.mailer', $config['service']['mailer']); |
|
|
42 |
$container->setAlias('fos_user.util.email_canonicalizer', $config['service']['email_canonicalizer']); |
|
|
43 |
$container->setAlias('fos_user.util.username_canonicalizer', $config['service']['username_canonicalizer']); |
|
|
44 |
$container->setAlias('fos_user.user_manager', $config['service']['user_manager']); |
|
|
45 |
|
|
|
46 |
if ($config['use_listener']) { |
|
|
47 |
switch ($config['db_driver']) { |
|
|
48 |
case 'orm': |
|
|
49 |
$container->getDefinition('fos_user.user_listener')->addTag('doctrine.event_subscriber'); |
|
|
50 |
break; |
|
|
51 |
|
|
|
52 |
case 'mongodb': |
|
|
53 |
$container->getDefinition('fos_user.user_listener')->addTag('doctrine.common.event_subscriber'); |
|
|
54 |
break; |
|
|
55 |
|
|
|
56 |
case 'couchdb': |
|
|
57 |
$container->getDefinition('fos_user.user_listener')->addTag('doctrine_couchdb.event_subscriber'); |
|
|
58 |
break; |
|
|
59 |
|
|
|
60 |
default: |
|
|
61 |
break; |
|
|
62 |
} |
|
|
63 |
} |
|
|
64 |
if ($config['use_username_form_type']) { |
|
|
65 |
$loader->load('username_form_type.xml'); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
$this->remapParametersNamespaces($config, $container, array( |
|
|
69 |
'' => array( |
|
|
70 |
'firewall_name' => 'fos_user.firewall_name', |
|
|
71 |
'model_manager_name' => 'fos_user.model_manager_name', |
|
|
72 |
'user_class' => 'fos_user.model.user.class', |
|
|
73 |
), |
|
|
74 |
'encoder' => 'fos_user.encoder.%s', |
|
|
75 |
'template' => 'fos_user.template.%s', |
|
|
76 |
)); |
|
|
77 |
$container->setParameter( |
|
|
78 |
'fos_user.registration.confirmation.from_email', |
|
|
79 |
array($config['from_email']['address'] => $config['from_email']['sender_name']) |
|
|
80 |
); |
|
|
81 |
$container->setParameter( |
|
|
82 |
'fos_user.resetting.email.from_email', |
|
|
83 |
array($config['from_email']['address'] => $config['from_email']['sender_name']) |
|
|
84 |
); |
|
|
85 |
|
|
|
86 |
// handle the MongoDB document manager name in a specific way as it does not have a registry to make it easy |
|
|
87 |
// TODO: change it if https://github.com/symfony/DoctrineMongoDBBundle/pull/31 is merged |
|
|
88 |
if ('mongodb' === $config['db_driver']) { |
|
|
89 |
if (null === $config['model_manager_name']) { |
|
|
90 |
$container->setAlias(new Alias('fos_user.document_manager', false), 'doctrine.odm.mongodb.document_manager'); |
|
|
91 |
} else { |
|
|
92 |
$container->setAlias(new Alias('fos_user.document_manager', false), sprintf('doctrine.odm.%s_mongodb.document_manager', $config['model_manager_name'])); |
|
|
93 |
} |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
if (!empty($config['profile'])) { |
|
|
97 |
$loader->load('profile.xml'); |
|
|
98 |
|
|
|
99 |
$container->setAlias('fos_user.profile.form.handler', $config['profile']['form']['handler']); |
|
|
100 |
unset($config['profile']['form']['handler']); |
|
|
101 |
|
|
|
102 |
$this->remapParametersNamespaces($config['profile'], $container, array( |
|
|
103 |
'form' => 'fos_user.profile.form.%s', |
|
|
104 |
)); |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
if (!empty($config['registration'])) { |
|
|
108 |
$loader->load('registration.xml'); |
|
|
109 |
|
|
|
110 |
$container->setAlias('fos_user.registration.form.handler', $config['registration']['form']['handler']); |
|
|
111 |
unset($config['registration']['form']['handler']); |
|
|
112 |
|
|
|
113 |
if (!empty($config['registration']['confirmation']['from_email'])) { |
|
|
114 |
$container->setParameter( |
|
|
115 |
'fos_user.registration.confirmation.from_email', |
|
|
116 |
array($config['registration']['confirmation']['from_email']['address'] => $config['registration']['confirmation']['from_email']['sender_name']) |
|
|
117 |
); |
|
|
118 |
} |
|
|
119 |
unset($config['registration']['confirmation']['from_email']); |
|
|
120 |
|
|
|
121 |
$this->remapParametersNamespaces($config['registration'], $container, array( |
|
|
122 |
'confirmation' => 'fos_user.registration.confirmation.%s', |
|
|
123 |
'form' => 'fos_user.registration.form.%s', |
|
|
124 |
)); |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
if (!empty($config['change_password'])) { |
|
|
128 |
$loader->load('change_password.xml'); |
|
|
129 |
|
|
|
130 |
$container->setAlias('fos_user.change_password.form.handler', $config['change_password']['form']['handler']); |
|
|
131 |
unset($config['change_password']['form']['handler']); |
|
|
132 |
|
|
|
133 |
$this->remapParametersNamespaces($config['change_password'], $container, array( |
|
|
134 |
'form' => 'fos_user.change_password.form.%s', |
|
|
135 |
)); |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
if (!empty($config['resetting'])) { |
|
|
139 |
$loader->load('resetting.xml'); |
|
|
140 |
|
|
|
141 |
$container->setAlias('fos_user.resetting.form.handler', $config['resetting']['form']['handler']); |
|
|
142 |
unset($config['resetting']['form']['handler']); |
|
|
143 |
|
|
|
144 |
if (!empty($config['resetting']['email']['from_email'])) { |
|
|
145 |
$container->setParameter( |
|
|
146 |
'fos_user.resetting.email.from_email', |
|
|
147 |
array($config['resetting']['email']['from_email']['address'] => $config['resetting']['email']['from_email']['sender_name']) |
|
|
148 |
); |
|
|
149 |
} |
|
|
150 |
unset($config['resetting']['email']['from_email']); |
|
|
151 |
|
|
|
152 |
$this->remapParametersNamespaces($config['resetting'], $container, array( |
|
|
153 |
'' => array ( |
|
|
154 |
'token_ttl' => 'fos_user.resetting.token_ttl', |
|
|
155 |
), |
|
|
156 |
'email' => 'fos_user.resetting.email.%s', |
|
|
157 |
'form' => 'fos_user.resetting.form.%s', |
|
|
158 |
)); |
|
|
159 |
} |
|
|
160 |
|
|
|
161 |
if (!empty($config['group'])) { |
|
|
162 |
$loader->load('group.xml'); |
|
|
163 |
$loader->load(sprintf('%s_group.xml', $config['db_driver'])); |
|
|
164 |
|
|
|
165 |
$container->setAlias('fos_user.group_manager', $config['group']['group_manager']); |
|
|
166 |
$container->setAlias('fos_user.group.form.handler', $config['group']['form']['handler']); |
|
|
167 |
unset($config['group']['form']['handler']); |
|
|
168 |
|
|
|
169 |
$this->remapParametersNamespaces($config['group'], $container, array( |
|
|
170 |
'' => array( |
|
|
171 |
'group_class' => 'fos_user.model.group.class', |
|
|
172 |
), |
|
|
173 |
'form' => 'fos_user.group.form.%s', |
|
|
174 |
)); |
|
|
175 |
} |
|
|
176 |
} |
|
|
177 |
|
|
|
178 |
protected function remapParameters(array $config, ContainerBuilder $container, array $map) |
|
|
179 |
{ |
|
|
180 |
foreach ($map as $name => $paramName) { |
|
|
181 |
if (array_key_exists($name, $config)) { |
|
|
182 |
$container->setParameter($paramName, $config[$name]); |
|
|
183 |
} |
|
|
184 |
} |
|
|
185 |
} |
|
|
186 |
|
|
|
187 |
protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces) |
|
|
188 |
{ |
|
|
189 |
foreach ($namespaces as $ns => $map) { |
|
|
190 |
if ($ns) { |
|
|
191 |
if (!array_key_exists($ns, $config)) { |
|
|
192 |
continue; |
|
|
193 |
} |
|
|
194 |
$namespaceConfig = $config[$ns]; |
|
|
195 |
} else { |
|
|
196 |
$namespaceConfig = $config; |
|
|
197 |
} |
|
|
198 |
if (is_array($map)) { |
|
|
199 |
$this->remapParameters($namespaceConfig, $container, $map); |
|
|
200 |
} else { |
|
|
201 |
foreach ($namespaceConfig as $name => $value) { |
|
|
202 |
$container->setParameter(sprintf($map, $name), $value); |
|
|
203 |
} |
|
|
204 |
} |
|
|
205 |
} |
|
|
206 |
} |
|
|
207 |
} |