forked from FriendsOfSymfony/FOSUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request FriendsOfSymfony#1081 from dbu/map-models
use the new doctrine compiler pass to map the model classes directly
- Loading branch information
Showing
21 changed files
with
368 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace FOS\UserBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; | ||
|
||
/** | ||
* Forward compatibility class in case FOSUserBundle is used with older | ||
* versions of Symfony2 or the doctrine bundles that do not provide the | ||
* register mappings compiler pass yet. | ||
* | ||
* @deprecated Compatibility class to make the bundle work with Symfony < 2.3. | ||
* To be removed when this bundle drops support for Symfony < 2.3 | ||
* | ||
* @author David Buchmann <[email protected]> | ||
*/ | ||
class RegisterMappingsPass implements CompilerPassInterface | ||
{ | ||
private $driver; | ||
private $driverPattern; | ||
private $namespaces; | ||
private $enabledParameter; | ||
private $fallbackManagerParameter; | ||
|
||
public function __construct($driver, $driverPattern, $namespaces, $enabledParameter, $fallbackManagerParameter) | ||
{ | ||
$this->driver = $driver; | ||
$this->driverPattern = $driverPattern; | ||
$this->namespaces = $namespaces; | ||
$this->enabledParameter = $enabledParameter; | ||
$this->fallbackManagerParameter = $fallbackManagerParameter; | ||
} | ||
|
||
/** | ||
* Register mappings with the metadata drivers. | ||
* | ||
* @param ContainerBuilder $container | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasParameter($this->enabledParameter)) { | ||
return; | ||
} | ||
|
||
$chainDriverDefService = $this->getChainDriverServiceName($container); | ||
$chainDriverDef = $container->getDefinition($chainDriverDefService); | ||
foreach ($this->namespaces as $namespace) { | ||
$chainDriverDef->addMethodCall('addDriver', array($this->driver, $namespace)); | ||
} | ||
} | ||
|
||
protected function getChainDriverServiceName(ContainerBuilder $container) | ||
{ | ||
foreach (array('fos_user.model_manager_name', $this->fallbackManagerParameter) as $param) { | ||
if ($container->hasParameter($param)) { | ||
$name = $container->getParameter($param); | ||
if ($name) { | ||
return sprintf($this->driverPattern, $name); | ||
} | ||
} | ||
} | ||
|
||
throw new ParameterNotFoundException('None of the managerParameters resulted in a valid name'); | ||
} | ||
|
||
public static function createOrmMappingDriver(array $mappings) | ||
{ | ||
$arguments = array($mappings, '.orm.xml'); | ||
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator', $arguments); | ||
$driver = new Definition('Doctrine\ORM\Mapping\Driver\XmlDriver', array($locator)); | ||
|
||
return new RegisterMappingsPass($driver, 'doctrine.orm.%s_metadata_driver', $mappings, 'fos_user.backend_type_orm', 'doctrine.default_entity_manager'); | ||
} | ||
|
||
public static function createMongoDBMappingDriver($mappings) | ||
{ | ||
$arguments = array($mappings, '.mongodb.xml'); | ||
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator', $arguments); | ||
$driver = new Definition('Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver', array($locator)); | ||
|
||
return new RegisterMappingsPass($driver, 'doctrine_mongodb.odm.%s_metadata_driver', $mappings, 'fos_user.backend_type_mongodb', 'doctrine_mongodb.odm.default_document_manager'); | ||
} | ||
|
||
public static function createCouchDBMappingDriver($mappings) | ||
{ | ||
$arguments = array($mappings, '.couchdb.xml'); | ||
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator', $arguments); | ||
$driver = new Definition('Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver', array($locator)); | ||
|
||
return new RegisterMappingsPass($driver, 'doctrine_couchdb.odm.%s_metadata_driver', $mappings, 'fos_user.backend_type_couchdb', 'doctrine_couchdb.default_document_manager'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,13 @@ | |
|
||
namespace FOS\UserBundle; | ||
|
||
use FOS\UserBundle\DependencyInjection\Compiler\ValidationPass; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use FOS\UserBundle\DependencyInjection\Compiler\ValidationPass; | ||
use FOS\UserBundle\DependencyInjection\Compiler\RegisterMappingsPass; | ||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; | ||
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass; | ||
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass; | ||
|
||
/** | ||
* @author Matthieu Bontemps <[email protected]> | ||
|
@@ -25,5 +29,38 @@ public function build(ContainerBuilder $container) | |
{ | ||
parent::build($container); | ||
$container->addCompilerPass(new ValidationPass()); | ||
|
||
$this->addRegisterMappingsPass($container); | ||
} | ||
|
||
/** | ||
* @param ContainerBuilder $container | ||
*/ | ||
private function addRegisterMappingsPass(ContainerBuilder $container) | ||
{ | ||
// the base class is only available since symfony 2.3 | ||
$symfonyVersion = class_exists('Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass'); | ||
|
||
$mappings = array( | ||
realpath(__DIR__ . '/Resources/config/doctrine/model') => 'FOS\UserBundle\Model', | ||
); | ||
|
||
if ($symfonyVersion && class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) { | ||
$container->addCompilerPass(DoctrineOrmMappingsPass::createXmlMappingDriver($mappings, array('fos_user.model_manager_name'), 'fos_user.backend_type_orm')); | ||
} else { | ||
$container->addCompilerPass(RegisterMappingsPass::createOrmMappingDriver($mappings)); | ||
} | ||
|
||
if ($symfonyVersion && class_exists('Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass')) { | ||
$container->addCompilerPass(DoctrineMongoDBMappingsPass::createXmlMappingDriver($mappings, array('fos_user.model_manager_name'), 'fos_user.backend_type_mongodb')); | ||
} else { | ||
$container->addCompilerPass(RegisterMappingsPass::createMongoDBMappingDriver($mappings)); | ||
} | ||
|
||
if ($symfonyVersion && class_exists('Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass')) { | ||
$container->addCompilerPass(DoctrineCouchDBMappingsPass::createXmlMappingDriver($mappings, array('fos_user.model_manager_name'), 'fos_user.backend_type_couchdb')); | ||
} else { | ||
$container->addCompilerPass(RegisterMappingsPass::createCouchDBMappingDriver($mappings)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<doctrine-mapping> | ||
<mapped-superclass name="FOS\UserBundle\Document\Group" indexed="true"> | ||
|
||
<field name="name" fieldName="name" type="string" indexed="true" /> | ||
<field name="roles" fieldName="roles" type="mixed" /> | ||
|
||
</mapped-superclass> | ||
|
||
<mapped-superclass name="FOS\UserBundle\CouchDocument\Group" indexed="true"/> | ||
</doctrine-mapping> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<doctrine-mapping> | ||
|
||
<mapped-superclass name="FOS\UserBundle\Document\User" indexed="true"> | ||
|
||
<field name="username" fieldName="username" type="string" indexed="true" /> | ||
<field name="usernameCanonical" fieldName="usernameCanonical" type="string" indexed="true" /> | ||
<field name="email" fieldName="email" type="string" indexed="true" /> | ||
<field name="emailCanonical" fieldName="emailCanonical" type="string" indexed="true" /> | ||
<field name="enabled" fieldName="enabled" type="mixed" /> | ||
<field name="salt" fieldName="salt" type="string" /> | ||
<field name="password" fieldName="password" type="string" /> | ||
<field name="lastLogin" fieldName="lastLogin" type="datetime" /> | ||
<field name="locked" fieldName="locked" type="mixed" /> | ||
<field name="expired" fieldName="expired" type="mixed" /> | ||
<field name="expiresAt" fieldName="expiresAt" type="datetime" /> | ||
<field name="confirmationToken" fieldName="confirmationToken" type="string" /> | ||
<field name="passwordRequestedAt" fieldName="passwordRequestedAt" type="datetime" /> | ||
<field name="roles" fieldName="roles" type="mixed" /> | ||
|
||
</mapped-superclass> | ||
<mapped-superclass name="FOS\UserBundle\CouchDocument\User" indexed="true"/> | ||
|
||
</doctrine-mapping> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<doctrine-mapping> | ||
<mapped-superclass name="FOS\UserBundle\Model\Group" indexed="true"> | ||
|
||
<field name="name" fieldName="name" type="string" indexed="true" /> | ||
<field name="roles" fieldName="roles" type="mixed" /> | ||
|
||
</mapped-superclass> | ||
|
||
</doctrine-mapping> |
Oops, something went wrong.