|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace FOS\UserBundle\DependencyInjection\Compiler; |
| 4 | + |
| 5 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 6 | +use Symfony\Component\DependencyInjection\Definition; |
| 7 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 8 | +use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
| 9 | + |
| 10 | +/** |
| 11 | + * Forward compatibility class in case FOSUserBundle is used with older |
| 12 | + * versions of Symfony2 or the doctrine bundles that do not provide the |
| 13 | + * register mappings compiler pass yet. |
| 14 | + * |
| 15 | + * @deprecated Compatibility class to make the bundle work with Symfony < 2.3. |
| 16 | + * To be removed when this bundle drops support for Symfony < 2.3 |
| 17 | + * |
| 18 | + * @author David Buchmann <[email protected]> |
| 19 | + */ |
| 20 | +class RegisterMappingsPass implements CompilerPassInterface |
| 21 | +{ |
| 22 | + private $driver; |
| 23 | + private $driverPattern; |
| 24 | + private $namespaces; |
| 25 | + private $enabledParameter; |
| 26 | + private $fallbackManagerParameter; |
| 27 | + |
| 28 | + public function __construct($driver, $driverPattern, $namespaces, $enabledParameter, $fallbackManagerParameter) |
| 29 | + { |
| 30 | + $this->driver = $driver; |
| 31 | + $this->driverPattern = $driverPattern; |
| 32 | + $this->namespaces = $namespaces; |
| 33 | + $this->enabledParameter = $enabledParameter; |
| 34 | + $this->fallbackManagerParameter = $fallbackManagerParameter; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Register mappings with the metadata drivers. |
| 39 | + * |
| 40 | + * @param ContainerBuilder $container |
| 41 | + */ |
| 42 | + public function process(ContainerBuilder $container) |
| 43 | + { |
| 44 | + if (!$container->hasParameter($this->enabledParameter)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $chainDriverDefService = $this->getChainDriverServiceName($container); |
| 49 | + $chainDriverDef = $container->getDefinition($chainDriverDefService); |
| 50 | + foreach ($this->namespaces as $namespace) { |
| 51 | + $chainDriverDef->addMethodCall('addDriver', array($this->driver, $namespace)); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected function getChainDriverServiceName(ContainerBuilder $container) |
| 56 | + { |
| 57 | + foreach (array('fos_user.model_manager_name', $this->fallbackManagerParameter) as $param) { |
| 58 | + if ($container->hasParameter($param)) { |
| 59 | + $name = $container->getParameter($param); |
| 60 | + if ($name) { |
| 61 | + return sprintf($this->driverPattern, $name); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + throw new ParameterNotFoundException('None of the managerParameters resulted in a valid name'); |
| 67 | + } |
| 68 | + |
| 69 | + public static function createOrmMappingDriver(array $mappings) |
| 70 | + { |
| 71 | + $arguments = array($mappings, '.orm.xml'); |
| 72 | + $locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator', $arguments); |
| 73 | + $driver = new Definition('Doctrine\ORM\Mapping\Driver\XmlDriver', array($locator)); |
| 74 | + |
| 75 | + return new RegisterMappingsPass($driver, 'doctrine.orm.%s_metadata_driver', $mappings, 'fos_user.backend_type_orm', 'doctrine.default_entity_manager'); |
| 76 | + } |
| 77 | + |
| 78 | + public static function createMongoDBMappingDriver($mappings) |
| 79 | + { |
| 80 | + $arguments = array($mappings, '.mongodb.xml'); |
| 81 | + $locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator', $arguments); |
| 82 | + $driver = new Definition('Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver', array($locator)); |
| 83 | + |
| 84 | + return new RegisterMappingsPass($driver, 'doctrine_mongodb.odm.%s_metadata_driver', $mappings, 'fos_user.backend_type_mongodb', 'doctrine_mongodb.odm.default_document_manager'); |
| 85 | + } |
| 86 | +} |
0 commit comments