FOSUserExtension.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\DependencyInjection;
  11. use Symfony\Component\Config\Definition\Processor;
  12. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  13. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\Config\FileLocator;
  16. class FOSUserExtension extends Extension
  17. {
  18. public function load(array $configs, ContainerBuilder $container)
  19. {
  20. $processor = new Processor();
  21. $configuration = new Configuration();
  22. $config = $processor->processConfiguration($configuration, $configs);
  23. $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  24. if ('custom' !== $config['db_driver']) {
  25. $loader->load(sprintf('%s.xml', $config['db_driver']));
  26. }
  27. foreach (array('validator', 'security', 'util', 'mailer') as $basename) {
  28. $loader->load(sprintf('%s.xml', $basename));
  29. }
  30. $container->setAlias('fos_user.mailer', $config['service']['mailer']);
  31. $container->setAlias('fos_user.util.email_canonicalizer', $config['service']['email_canonicalizer']);
  32. $container->setAlias('fos_user.util.username_canonicalizer', $config['service']['username_canonicalizer']);
  33. $container->setAlias('fos_user.util.token_generator', $config['service']['token_generator']);
  34. $container->setAlias('fos_user.user_manager', $config['service']['user_manager']);
  35. if ($config['use_listener']) {
  36. switch ($config['db_driver']) {
  37. case 'orm':
  38. $container->getDefinition('fos_user.user_listener')->addTag('doctrine.event_subscriber');
  39. break;
  40. case 'mongodb':
  41. $container->getDefinition('fos_user.user_listener')->addTag('doctrine_mongodb.odm.event_subscriber');
  42. break;
  43. case 'couchdb':
  44. $container->getDefinition('fos_user.user_listener')->addTag('doctrine_couchdb.event_subscriber');
  45. break;
  46. case 'propel':
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. if ($config['use_username_form_type']) {
  53. $loader->load('username_form_type.xml');
  54. }
  55. $this->remapParametersNamespaces($config, $container, array(
  56. '' => array(
  57. 'db_driver' => 'fos_user.storage',
  58. 'firewall_name' => 'fos_user.firewall_name',
  59. 'model_manager_name' => 'fos_user.model_manager_name',
  60. 'user_class' => 'fos_user.model.user.class',
  61. ),
  62. 'template' => 'fos_user.template.%s',
  63. ));
  64. if (!empty($config['profile'])) {
  65. $this->loadProfile($config['profile'], $container, $loader);
  66. }
  67. if (!empty($config['registration'])) {
  68. $this->loadRegistration($config['registration'], $container, $loader, $config['from_email']);
  69. }
  70. if (!empty($config['change_password'])) {
  71. $this->loadChangePassword($config['change_password'], $container, $loader);
  72. }
  73. if (!empty($config['resetting'])) {
  74. $this->loadResetting($config['resetting'], $container, $loader, $config['from_email']);
  75. }
  76. if (!empty($config['group'])) {
  77. $this->loadGroups($config['group'], $container, $loader, $config['db_driver']);
  78. }
  79. }
  80. private function loadProfile(array $config, ContainerBuilder $container, XmlFileLoader $loader)
  81. {
  82. $loader->load('profile.xml');
  83. $container->setAlias('fos_user.profile.form.handler', $config['form']['handler']);
  84. unset($config['form']['handler']);
  85. $this->remapParametersNamespaces($config, $container, array(
  86. 'form' => 'fos_user.profile.form.%s',
  87. ));
  88. }
  89. private function loadRegistration(array $config, ContainerBuilder $container, XmlFileLoader $loader, array $fromEmail)
  90. {
  91. $loader->load('registration.xml');
  92. $container->setAlias('fos_user.registration.form.handler', $config['form']['handler']);
  93. unset($config['form']['handler']);
  94. if (isset($config['confirmation']['from_email'])) {
  95. // overwrite the global one
  96. $fromEmail = $config['confirmation']['from_email'];
  97. unset($config['confirmation']['from_email']);
  98. }
  99. $container->setParameter('fos_user.registration.confirmation.from_email', array($fromEmail['address'] => $fromEmail['sender_name']));
  100. $this->remapParametersNamespaces($config, $container, array(
  101. 'confirmation' => 'fos_user.registration.confirmation.%s',
  102. 'form' => 'fos_user.registration.form.%s',
  103. ));
  104. }
  105. private function loadChangePassword(array $config, ContainerBuilder $container, XmlFileLoader $loader)
  106. {
  107. $loader->load('change_password.xml');
  108. $container->setAlias('fos_user.change_password.form.handler', $config['form']['handler']);
  109. unset($config['form']['handler']);
  110. $this->remapParametersNamespaces($config, $container, array(
  111. 'form' => 'fos_user.change_password.form.%s',
  112. ));
  113. }
  114. private function loadResetting(array $config, ContainerBuilder $container, XmlFileLoader $loader, array $fromEmail)
  115. {
  116. $loader->load('resetting.xml');
  117. $container->setAlias('fos_user.resetting.form.handler', $config['form']['handler']);
  118. unset($config['form']['handler']);
  119. if (isset($config['email']['from_email'])) {
  120. // overwrite the global one
  121. $fromEmail = $config['email']['from_email'];
  122. unset($config['email']['from_email']);
  123. }
  124. $container->setParameter('fos_user.resetting.email.from_email', array($fromEmail['address'] => $fromEmail['sender_name']));
  125. $this->remapParametersNamespaces($config, $container, array(
  126. '' => array (
  127. 'token_ttl' => 'fos_user.resetting.token_ttl',
  128. ),
  129. 'email' => 'fos_user.resetting.email.%s',
  130. 'form' => 'fos_user.resetting.form.%s',
  131. ));
  132. }
  133. private function loadGroups(array $config, ContainerBuilder $container, XmlFileLoader $loader, $dbDriver)
  134. {
  135. $loader->load('group.xml');
  136. if ('custom' !== $dbDriver) {
  137. $loader->load(sprintf('%s_group.xml', $dbDriver));
  138. }
  139. $container->setAlias('fos_user.group_manager', $config['group_manager']);
  140. $container->setAlias('fos_user.group.form.handler', $config['form']['handler']);
  141. unset($config['form']['handler']);
  142. $this->remapParametersNamespaces($config, $container, array(
  143. '' => array(
  144. 'group_class' => 'fos_user.model.group.class',
  145. ),
  146. 'form' => 'fos_user.group.form.%s',
  147. ));
  148. }
  149. protected function remapParameters(array $config, ContainerBuilder $container, array $map)
  150. {
  151. foreach ($map as $name => $paramName) {
  152. if (array_key_exists($name, $config)) {
  153. $container->setParameter($paramName, $config[$name]);
  154. }
  155. }
  156. }
  157. protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces)
  158. {
  159. foreach ($namespaces as $ns => $map) {
  160. if ($ns) {
  161. if (!array_key_exists($ns, $config)) {
  162. continue;
  163. }
  164. $namespaceConfig = $config[$ns];
  165. } else {
  166. $namespaceConfig = $config;
  167. }
  168. if (is_array($map)) {
  169. $this->remapParameters($namespaceConfig, $container, $map);
  170. } else {
  171. foreach ($namespaceConfig as $name => $value) {
  172. $container->setParameter(sprintf($map, $name), $value);
  173. }
  174. }
  175. }
  176. }
  177. }