Skip to content

use the new doctrine compiler pass to map the model classes directly #1081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 25, 2013
Merged
95 changes: 95 additions & 0 deletions DependencyInjection/Compiler/RegisterMappingsPass.php
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');
}
}
1 change: 1 addition & 0 deletions DependencyInjection/FOSUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function load(array $configs, ContainerBuilder $container)

if ('custom' !== $config['db_driver']) {
$loader->load(sprintf('%s.xml', $config['db_driver']));
$container->setParameter($this->getAlias() . '.backend_type_' . $config['db_driver'], true);
}

foreach (array('validator', 'security', 'util', 'mailer', 'listeners') as $basename) {
Expand Down
9 changes: 9 additions & 0 deletions Document/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

use FOS\UserBundle\Model\Group as BaseGroup;

/**
* @deprecated directly extend the classes in the Model namespace
*/
abstract class Group extends BaseGroup
{
public function __construct($name, $roles = array())
{
// you should extend the class in the Model namespace directly
trigger_error(E_USER_DEPRECATED);
parent::__construct($name, $roles);
}
}
9 changes: 9 additions & 0 deletions Document/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

use FOS\UserBundle\Model\User as AbstractUser;

/**
* @deprecated directly extend the classes in the Model namespace
*/
abstract class User extends AbstractUser
{
public function __construct()
{
// you should extend the class in the Model namespace directly
trigger_error(E_USER_DEPRECATED);
parent::__construct();
}
}
9 changes: 9 additions & 0 deletions Entity/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

use FOS\UserBundle\Model\Group as BaseGroup;

/**
* @deprecated directly extend the classes in the Model namespace
*/
class Group extends BaseGroup
{
public function __construct($name, $roles = array())
{
// you should extend the class in the Model namespace directly
trigger_error(E_USER_DEPRECATED);
parent::__construct($name, $roles);
}
}
9 changes: 9 additions & 0 deletions Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

use FOS\UserBundle\Model\User as AbstractUser;

/**
* @deprecated directly extend the classes in the Model namespace
*/
abstract class User extends AbstractUser
{
public function __construct()
{
// you should extend the class in the Model namespace directly
trigger_error(E_USER_DEPRECATED);
parent::__construct();
}
}
39 changes: 38 additions & 1 deletion FOSUserBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand All @@ -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));
}
}
}
8 changes: 1 addition & 7 deletions Resources/config/doctrine/Group.couchdb.xml
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>
15 changes: 1 addition & 14 deletions Resources/config/doctrine/Group.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<mapped-superclass name="FOS\UserBundle\Document\Group" collection="fos_user_group">

<field name="name" fieldName="name" type="string" />

<field name="roles" fieldName="roles" type="collection" />

<indexes>
<index unique="true" dropDups="true">
<key name="name" order="asc" />
<option name="safe" value="true" />
</index>
</indexes>

</mapped-superclass>
<mapped-superclass name="FOS\UserBundle\Document\Group" collection="fos_user_group"/>

</doctrine-mongo-mapping>
8 changes: 1 addition & 7 deletions Resources/config/doctrine/Group.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<mapped-superclass name="FOS\UserBundle\Entity\Group">

<field name="name" column="name" type="string" length="255" unique="true" />

<field name="roles" column="roles" type="array" />

</mapped-superclass>
<mapped-superclass name="FOS\UserBundle\Entity\Group"/>

</doctrine-mapping>
19 changes: 1 addition & 18 deletions Resources/config/doctrine/User.couchdb.xml
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>
43 changes: 1 addition & 42 deletions Resources/config/doctrine/User.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,6 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<mapped-superclass name="FOS\UserBundle\Document\User" collection="fos_user_user">

<field name="username" fieldName="username" type="string" />

<field name="usernameCanonical" fieldName="usernameCanonical" type="string" />

<field name="email" fieldName="email" type="string" />

<field name="emailCanonical" fieldName="emailCanonical" type="string" />

<field name="enabled" fieldName="enabled" type="boolean" />

<field name="salt" fieldName="salt" type="string" />

<field name="password" fieldName="password" type="string" />

<field name="lastLogin" fieldName="lastLogin" type="date" />

<field name="locked" fieldName="locked" type="boolean" />

<field name="expired" fieldName="expired" type="boolean" />

<field name="expiresAt" fieldName="expiresAt" type="date" />

<field name="confirmationToken" fieldName="confirmationToken" type="string" />

<field name="passwordRequestedAt" fieldName="passwordRequestedAt" type="date" />

<field name="roles" fieldName="roles" type="collection" />

<indexes>
<index unique="true" dropDups="true">
<key name="usernameCanonical" order="asc" />
<option name="safe" value="true" />
</index>
<index unique="true" dropDups="true">
<key name="emailCanonical" order="asc" />
<option name="safe" value="true" />
</index>
</indexes>

</mapped-superclass>
<mapped-superclass name="FOS\UserBundle\Document\User" collection="fos_user_user"/>

</doctrine-mongo-mapping>
36 changes: 1 addition & 35 deletions Resources/config/doctrine/User.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,6 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<mapped-superclass name="FOS\UserBundle\Entity\User">

<field name="username" column="username" type="string" length="255" />

<field name="usernameCanonical" column="username_canonical" type="string" length="255" unique="true" />

<field name="email" column="email" type="string" length="255" />

<field name="emailCanonical" column="email_canonical" type="string" length="255" unique="true" />

<field name="enabled" column="enabled" type="boolean" />

<field name="salt" column="salt" type="string" />

<field name="password" column="password" type="string" />

<field name="lastLogin" column="last_login" type="datetime" nullable="true" />

<field name="locked" column="locked" type="boolean" />

<field name="expired" column="expired" type="boolean" />

<field name="expiresAt" column="expires_at" type="datetime" nullable="true" />

<field name="confirmationToken" column="confirmation_token" type="string" nullable="true" />

<field name="passwordRequestedAt" column="password_requested_at" type="datetime" nullable="true" />

<field name="roles" column="roles" type="array" />

<field name="credentialsExpired" column="credentials_expired" type="boolean" />

<field name="credentialsExpireAt" column="credentials_expire_at" type="datetime" nullable="true" />

</mapped-superclass>
<mapped-superclass name="FOS\UserBundle\Entity\User" />

</doctrine-mapping>
10 changes: 10 additions & 0 deletions Resources/config/doctrine/model/Group.couchdb.xml
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>
Loading