Skip to content

Commit 33271c9

Browse files
committed
Merge pull request #1081 from dbu/map-models
use the new doctrine compiler pass to map the model classes directly
2 parents 8be0c33 + 89be8e9 commit 33271c9

21 files changed

+368
-141
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
87+
public static function createCouchDBMappingDriver($mappings)
88+
{
89+
$arguments = array($mappings, '.couchdb.xml');
90+
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator', $arguments);
91+
$driver = new Definition('Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver', array($locator));
92+
93+
return new RegisterMappingsPass($driver, 'doctrine_couchdb.odm.%s_metadata_driver', $mappings, 'fos_user.backend_type_couchdb', 'doctrine_couchdb.default_document_manager');
94+
}
95+
}

DependencyInjection/FOSUserExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function load(array $configs, ContainerBuilder $container)
3030

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

3536
foreach (array('validator', 'security', 'util', 'mailer', 'listeners') as $basename) {

Document/Group.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
use FOS\UserBundle\Model\Group as BaseGroup;
1515

16+
/**
17+
* @deprecated directly extend the classes in the Model namespace
18+
*/
1619
abstract class Group extends BaseGroup
1720
{
21+
public function __construct($name, $roles = array())
22+
{
23+
// you should extend the class in the Model namespace directly
24+
trigger_error(E_USER_DEPRECATED);
25+
parent::__construct($name, $roles);
26+
}
1827
}

Document/User.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
use FOS\UserBundle\Model\User as AbstractUser;
1515

16+
/**
17+
* @deprecated directly extend the classes in the Model namespace
18+
*/
1619
abstract class User extends AbstractUser
1720
{
21+
public function __construct()
22+
{
23+
// you should extend the class in the Model namespace directly
24+
trigger_error(E_USER_DEPRECATED);
25+
parent::__construct();
26+
}
1827
}

Entity/Group.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
use FOS\UserBundle\Model\Group as BaseGroup;
1515

16+
/**
17+
* @deprecated directly extend the classes in the Model namespace
18+
*/
1619
class Group extends BaseGroup
1720
{
21+
public function __construct($name, $roles = array())
22+
{
23+
// you should extend the class in the Model namespace directly
24+
trigger_error(E_USER_DEPRECATED);
25+
parent::__construct($name, $roles);
26+
}
1827
}

Entity/User.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
use FOS\UserBundle\Model\User as AbstractUser;
1515

16+
/**
17+
* @deprecated directly extend the classes in the Model namespace
18+
*/
1619
abstract class User extends AbstractUser
1720
{
21+
public function __construct()
22+
{
23+
// you should extend the class in the Model namespace directly
24+
trigger_error(E_USER_DEPRECATED);
25+
parent::__construct();
26+
}
1827
}

FOSUserBundle.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
namespace FOS\UserBundle;
1313

14-
use FOS\UserBundle\DependencyInjection\Compiler\ValidationPass;
1514
use Symfony\Component\HttpKernel\Bundle\Bundle;
1615
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use FOS\UserBundle\DependencyInjection\Compiler\ValidationPass;
17+
use FOS\UserBundle\DependencyInjection\Compiler\RegisterMappingsPass;
18+
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
19+
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
20+
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass;
1721

1822
/**
1923
* @author Matthieu Bontemps <[email protected]>
@@ -25,5 +29,38 @@ public function build(ContainerBuilder $container)
2529
{
2630
parent::build($container);
2731
$container->addCompilerPass(new ValidationPass());
32+
33+
$this->addRegisterMappingsPass($container);
34+
}
35+
36+
/**
37+
* @param ContainerBuilder $container
38+
*/
39+
private function addRegisterMappingsPass(ContainerBuilder $container)
40+
{
41+
// the base class is only available since symfony 2.3
42+
$symfonyVersion = class_exists('Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass');
43+
44+
$mappings = array(
45+
realpath(__DIR__ . '/Resources/config/doctrine/model') => 'FOS\UserBundle\Model',
46+
);
47+
48+
if ($symfonyVersion && class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass')) {
49+
$container->addCompilerPass(DoctrineOrmMappingsPass::createXmlMappingDriver($mappings, array('fos_user.model_manager_name'), 'fos_user.backend_type_orm'));
50+
} else {
51+
$container->addCompilerPass(RegisterMappingsPass::createOrmMappingDriver($mappings));
52+
}
53+
54+
if ($symfonyVersion && class_exists('Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass')) {
55+
$container->addCompilerPass(DoctrineMongoDBMappingsPass::createXmlMappingDriver($mappings, array('fos_user.model_manager_name'), 'fos_user.backend_type_mongodb'));
56+
} else {
57+
$container->addCompilerPass(RegisterMappingsPass::createMongoDBMappingDriver($mappings));
58+
}
59+
60+
if ($symfonyVersion && class_exists('Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass')) {
61+
$container->addCompilerPass(DoctrineCouchDBMappingsPass::createXmlMappingDriver($mappings, array('fos_user.model_manager_name'), 'fos_user.backend_type_couchdb'));
62+
} else {
63+
$container->addCompilerPass(RegisterMappingsPass::createCouchDBMappingDriver($mappings));
64+
}
2865
}
2966
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<doctrine-mapping>
3-
<mapped-superclass name="FOS\UserBundle\Document\Group" indexed="true">
4-
5-
<field name="name" fieldName="name" type="string" indexed="true" />
6-
<field name="roles" fieldName="roles" type="mixed" />
7-
8-
</mapped-superclass>
9-
3+
<mapped-superclass name="FOS\UserBundle\CouchDocument\Group" indexed="true"/>
104
</doctrine-mapping>

Resources/config/doctrine/Group.mongodb.xml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@
44
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
55
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
66

7-
<mapped-superclass name="FOS\UserBundle\Document\Group" collection="fos_user_group">
8-
9-
<field name="name" fieldName="name" type="string" />
10-
11-
<field name="roles" fieldName="roles" type="collection" />
12-
13-
<indexes>
14-
<index unique="true" dropDups="true">
15-
<key name="name" order="asc" />
16-
<option name="safe" value="true" />
17-
</index>
18-
</indexes>
19-
20-
</mapped-superclass>
7+
<mapped-superclass name="FOS\UserBundle\Document\Group" collection="fos_user_group"/>
218

229
</doctrine-mongo-mapping>

Resources/config/doctrine/Group.orm.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
55
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
66

7-
<mapped-superclass name="FOS\UserBundle\Entity\Group">
8-
9-
<field name="name" column="name" type="string" length="255" unique="true" />
10-
11-
<field name="roles" column="roles" type="array" />
12-
13-
</mapped-superclass>
7+
<mapped-superclass name="FOS\UserBundle\Entity\Group"/>
148

159
</doctrine-mapping>
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<doctrine-mapping>
33

4-
<mapped-superclass name="FOS\UserBundle\Document\User" indexed="true">
5-
6-
<field name="username" fieldName="username" type="string" indexed="true" />
7-
<field name="usernameCanonical" fieldName="usernameCanonical" type="string" indexed="true" />
8-
<field name="email" fieldName="email" type="string" indexed="true" />
9-
<field name="emailCanonical" fieldName="emailCanonical" type="string" indexed="true" />
10-
<field name="enabled" fieldName="enabled" type="mixed" />
11-
<field name="salt" fieldName="salt" type="string" />
12-
<field name="password" fieldName="password" type="string" />
13-
<field name="lastLogin" fieldName="lastLogin" type="datetime" />
14-
<field name="locked" fieldName="locked" type="mixed" />
15-
<field name="expired" fieldName="expired" type="mixed" />
16-
<field name="expiresAt" fieldName="expiresAt" type="datetime" />
17-
<field name="confirmationToken" fieldName="confirmationToken" type="string" />
18-
<field name="passwordRequestedAt" fieldName="passwordRequestedAt" type="datetime" />
19-
<field name="roles" fieldName="roles" type="mixed" />
20-
21-
</mapped-superclass>
4+
<mapped-superclass name="FOS\UserBundle\CouchDocument\User" indexed="true"/>
225

236
</doctrine-mapping>

Resources/config/doctrine/User.mongodb.xml

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,6 @@
44
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
55
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
66

7-
<mapped-superclass name="FOS\UserBundle\Document\User" collection="fos_user_user">
8-
9-
<field name="username" fieldName="username" type="string" />
10-
11-
<field name="usernameCanonical" fieldName="usernameCanonical" type="string" />
12-
13-
<field name="email" fieldName="email" type="string" />
14-
15-
<field name="emailCanonical" fieldName="emailCanonical" type="string" />
16-
17-
<field name="enabled" fieldName="enabled" type="boolean" />
18-
19-
<field name="salt" fieldName="salt" type="string" />
20-
21-
<field name="password" fieldName="password" type="string" />
22-
23-
<field name="lastLogin" fieldName="lastLogin" type="date" />
24-
25-
<field name="locked" fieldName="locked" type="boolean" />
26-
27-
<field name="expired" fieldName="expired" type="boolean" />
28-
29-
<field name="expiresAt" fieldName="expiresAt" type="date" />
30-
31-
<field name="confirmationToken" fieldName="confirmationToken" type="string" />
32-
33-
<field name="passwordRequestedAt" fieldName="passwordRequestedAt" type="date" />
34-
35-
<field name="roles" fieldName="roles" type="collection" />
36-
37-
<indexes>
38-
<index unique="true" dropDups="true">
39-
<key name="usernameCanonical" order="asc" />
40-
<option name="safe" value="true" />
41-
</index>
42-
<index unique="true" dropDups="true">
43-
<key name="emailCanonical" order="asc" />
44-
<option name="safe" value="true" />
45-
</index>
46-
</indexes>
47-
48-
</mapped-superclass>
7+
<mapped-superclass name="FOS\UserBundle\Document\User" collection="fos_user_user"/>
498

509
</doctrine-mongo-mapping>

Resources/config/doctrine/User.orm.xml

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,6 @@
44
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
55
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
66

7-
<mapped-superclass name="FOS\UserBundle\Entity\User">
8-
9-
<field name="username" column="username" type="string" length="255" />
10-
11-
<field name="usernameCanonical" column="username_canonical" type="string" length="255" unique="true" />
12-
13-
<field name="email" column="email" type="string" length="255" />
14-
15-
<field name="emailCanonical" column="email_canonical" type="string" length="255" unique="true" />
16-
17-
<field name="enabled" column="enabled" type="boolean" />
18-
19-
<field name="salt" column="salt" type="string" />
20-
21-
<field name="password" column="password" type="string" />
22-
23-
<field name="lastLogin" column="last_login" type="datetime" nullable="true" />
24-
25-
<field name="locked" column="locked" type="boolean" />
26-
27-
<field name="expired" column="expired" type="boolean" />
28-
29-
<field name="expiresAt" column="expires_at" type="datetime" nullable="true" />
30-
31-
<field name="confirmationToken" column="confirmation_token" type="string" nullable="true" />
32-
33-
<field name="passwordRequestedAt" column="password_requested_at" type="datetime" nullable="true" />
34-
35-
<field name="roles" column="roles" type="array" />
36-
37-
<field name="credentialsExpired" column="credentials_expired" type="boolean" />
38-
39-
<field name="credentialsExpireAt" column="credentials_expire_at" type="datetime" nullable="true" />
40-
41-
</mapped-superclass>
7+
<mapped-superclass name="FOS\UserBundle\Entity\User" />
428

439
</doctrine-mapping>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping>
3+
<mapped-superclass name="FOS\UserBundle\Model\Group" indexed="true">
4+
5+
<field name="name" fieldName="name" type="string" indexed="true" />
6+
<field name="roles" fieldName="roles" type="mixed" />
7+
8+
</mapped-superclass>
9+
10+
</doctrine-mapping>

0 commit comments

Comments
 (0)