vendor/friendsofsymfony/user-bundle/DependencyInjection/FOSUserExtension.php line 53

Open in your IDE?
  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\Config\FileLocator;
  13. use Symfony\Component\DependencyInjection\Alias;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  18. /**
  19.  * @internal
  20.  * @final
  21.  */
  22. class FOSUserExtension extends Extension
  23. {
  24.     /**
  25.      * @var array
  26.      */
  27.     private static $doctrineDrivers = [
  28.         'orm' => [
  29.             'registry' => 'doctrine',
  30.             'tag' => 'doctrine.event_subscriber',
  31.         ],
  32.         'mongodb' => [
  33.             'registry' => 'doctrine_mongodb',
  34.             'tag' => 'doctrine_mongodb.odm.event_subscriber',
  35.         ],
  36.         'couchdb' => [
  37.             'registry' => 'doctrine_couchdb',
  38.             'tag' => 'doctrine_couchdb.event_subscriber',
  39.             'listener_class' => 'FOS\UserBundle\Doctrine\CouchDB\UserListener',
  40.         ],
  41.     ];
  42.     private $mailerNeeded false;
  43.     private $sessionNeeded false;
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function load(array $configsContainerBuilder $container)
  48.     {
  49.         $processor = new Processor();
  50.         $configuration = new Configuration();
  51.         $config $processor->processConfiguration($configuration$configs);
  52.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  53.         if ('custom' !== $config['db_driver']) {
  54.             if (isset(self::$doctrineDrivers[$config['db_driver']])) {
  55.                 $loader->load('doctrine.xml');
  56.                 $container->setAlias('fos_user.doctrine_registry', new Alias(self::$doctrineDrivers[$config['db_driver']]['registry'], false));
  57.             } else {
  58.                 $loader->load(sprintf('%s.xml'$config['db_driver']));
  59.             }
  60.             $container->setParameter($this->getAlias().'.backend_type_'.$config['db_driver'], true);
  61.         }
  62.         if (isset(self::$doctrineDrivers[$config['db_driver']])) {
  63.             $definition $container->getDefinition('fos_user.object_manager');
  64.             $definition->setFactory([new Reference('fos_user.doctrine_registry'), 'getManager']);
  65.         }
  66.         foreach (['validator''security''util''mailer''listeners''commands'] as $basename) {
  67.             $loader->load(sprintf('%s.xml'$basename));
  68.         }
  69.         if (!$config['use_authentication_listener']) {
  70.             $container->removeDefinition('fos_user.listener.authentication');
  71.         }
  72.         if ($config['use_flash_notifications']) {
  73.             $this->sessionNeeded true;
  74.             $loader->load('flash_notifications.xml');
  75.         }
  76.         $container->setAlias('fos_user.util.email_canonicalizer'$config['service']['email_canonicalizer']);
  77.         $container->setAlias('fos_user.util.username_canonicalizer'$config['service']['username_canonicalizer']);
  78.         $container->setAlias('fos_user.util.token_generator'$config['service']['token_generator']);
  79.         $container->setAlias('fos_user.user_manager', new Alias($config['service']['user_manager'], true));
  80.         if ($config['use_listener'] && isset(self::$doctrineDrivers[$config['db_driver']])) {
  81.             $listenerDefinition $container->getDefinition('fos_user.user_listener');
  82.             $listenerDefinition->addTag(self::$doctrineDrivers[$config['db_driver']]['tag']);
  83.             if (isset(self::$doctrineDrivers[$config['db_driver']]['listener_class'])) {
  84.                 $listenerDefinition->setClass(self::$doctrineDrivers[$config['db_driver']]['listener_class']);
  85.             }
  86.         }
  87.         if ($config['use_username_form_type']) {
  88.             $loader->load('username_form_type.xml');
  89.         }
  90.         $this->remapParametersNamespaces($config$container, [
  91.             '' => [
  92.                 'db_driver' => 'fos_user.storage',
  93.                 'firewall_name' => 'fos_user.firewall_name',
  94.                 'model_manager_name' => 'fos_user.model_manager_name',
  95.                 'user_class' => 'fos_user.model.user.class',
  96.             ],
  97.         ]);
  98.         if (!empty($config['profile'])) {
  99.             $this->loadProfile($config['profile'], $container$loader);
  100.         }
  101.         if (!empty($config['registration'])) {
  102.             $this->loadRegistration($config['registration'], $container$loader$config['from_email']);
  103.         }
  104.         if (!empty($config['change_password'])) {
  105.             $this->loadChangePassword($config['change_password'], $container$loader);
  106.         }
  107.         if (!empty($config['resetting'])) {
  108.             $this->loadResetting($config['resetting'], $container$loader$config['from_email']);
  109.         }
  110.         if (!empty($config['group'])) {
  111.             @trigger_error('Using Groups is deprecated since version 2.2 and will be removed in 3.0.'E_USER_DEPRECATED);
  112.             $this->loadGroups($config['group'], $container$loader$config['db_driver']);
  113.         }
  114.         if ($this->mailerNeeded) {
  115.             $container->setAlias('fos_user.mailer'$config['service']['mailer']);
  116.         }
  117.         if ($this->sessionNeeded) {
  118.             // Use a private alias rather than a parameter, to avoid leaking it at runtime (the private alias will be removed)
  119.             $container->setAlias('fos_user.session', new Alias('session'false));
  120.         }
  121.     }
  122.     /**
  123.      * {@inheritdoc}
  124.      */
  125.     public function getNamespace()
  126.     {
  127.         return 'http://friendsofsymfony.github.io/schema/dic/user';
  128.     }
  129.     protected function remapParameters(array $configContainerBuilder $container, array $map)
  130.     {
  131.         foreach ($map as $name => $paramName) {
  132.             if (array_key_exists($name$config)) {
  133.                 $container->setParameter($paramName$config[$name]);
  134.             }
  135.         }
  136.     }
  137.     protected function remapParametersNamespaces(array $configContainerBuilder $container, array $namespaces)
  138.     {
  139.         foreach ($namespaces as $ns => $map) {
  140.             if ($ns) {
  141.                 if (!array_key_exists($ns$config)) {
  142.                     continue;
  143.                 }
  144.                 $namespaceConfig $config[$ns];
  145.             } else {
  146.                 $namespaceConfig $config;
  147.             }
  148.             if (is_array($map)) {
  149.                 $this->remapParameters($namespaceConfig$container$map);
  150.             } else {
  151.                 foreach ($namespaceConfig as $name => $value) {
  152.                     $container->setParameter(sprintf($map$name), $value);
  153.                 }
  154.             }
  155.         }
  156.     }
  157.     private function loadProfile(array $configContainerBuilder $containerXmlFileLoader $loader)
  158.     {
  159.         $loader->load('profile.xml');
  160.         $this->remapParametersNamespaces($config$container, [
  161.             'form' => 'fos_user.profile.form.%s',
  162.         ]);
  163.     }
  164.     private function loadRegistration(array $configContainerBuilder $containerXmlFileLoader $loader, array $fromEmail)
  165.     {
  166.         $loader->load('registration.xml');
  167.         $this->sessionNeeded true;
  168.         if ($config['confirmation']['enabled']) {
  169.             $this->mailerNeeded true;
  170.             $loader->load('email_confirmation.xml');
  171.         }
  172.         if (isset($config['confirmation']['from_email'])) {
  173.             // overwrite the global one
  174.             $fromEmail $config['confirmation']['from_email'];
  175.             unset($config['confirmation']['from_email']);
  176.         }
  177.         $container->setParameter('fos_user.registration.confirmation.from_email', [$fromEmail['address'] => $fromEmail['sender_name']]);
  178.         $this->remapParametersNamespaces($config$container, [
  179.             'confirmation' => 'fos_user.registration.confirmation.%s',
  180.             'form' => 'fos_user.registration.form.%s',
  181.         ]);
  182.     }
  183.     private function loadChangePassword(array $configContainerBuilder $containerXmlFileLoader $loader)
  184.     {
  185.         $loader->load('change_password.xml');
  186.         $this->remapParametersNamespaces($config$container, [
  187.             'form' => 'fos_user.change_password.form.%s',
  188.         ]);
  189.     }
  190.     private function loadResetting(array $configContainerBuilder $containerXmlFileLoader $loader, array $fromEmail)
  191.     {
  192.         $this->mailerNeeded true;
  193.         $loader->load('resetting.xml');
  194.         if (isset($config['email']['from_email'])) {
  195.             // overwrite the global one
  196.             $fromEmail $config['email']['from_email'];
  197.             unset($config['email']['from_email']);
  198.         }
  199.         $container->setParameter('fos_user.resetting.email.from_email', [$fromEmail['address'] => $fromEmail['sender_name']]);
  200.         $this->remapParametersNamespaces($config$container, [
  201.             '' => [
  202.                 'retry_ttl' => 'fos_user.resetting.retry_ttl',
  203.                 'token_ttl' => 'fos_user.resetting.token_ttl',
  204.             ],
  205.             'email' => 'fos_user.resetting.email.%s',
  206.             'form' => 'fos_user.resetting.form.%s',
  207.         ]);
  208.     }
  209.     /**
  210.      * @param string $dbDriver
  211.      */
  212.     private function loadGroups(array $configContainerBuilder $containerXmlFileLoader $loader$dbDriver)
  213.     {
  214.         $loader->load('group.xml');
  215.         if ('custom' !== $dbDriver) {
  216.             if (isset(self::$doctrineDrivers[$dbDriver])) {
  217.                 $loader->load('doctrine_group.xml');
  218.             } else {
  219.                 $loader->load(sprintf('%s_group.xml'$dbDriver));
  220.             }
  221.         }
  222.         $container->setAlias('fos_user.group_manager', new Alias($config['group_manager'], true));
  223.         $container->setAlias('FOS\UserBundle\Model\GroupManagerInterface', new Alias('fos_user.group_manager'false));
  224.         $this->remapParametersNamespaces($config$container, [
  225.             '' => [
  226.                 'group_class' => 'fos_user.model.group.class',
  227.             ],
  228.             'form' => 'fos_user.group.form.%s',
  229.         ]);
  230.     }
  231. }