Skip to content

Commit

Permalink
Removed support for older Doctrine versions
Browse files Browse the repository at this point in the history
The minimal versions are now Doctrine Common 2.3, Doctrine ORM 2.4 and
Doctrine MongoDB ODM 1.0-alpha10.
  • Loading branch information
stof committed Sep 26, 2014
1 parent 05f7f43 commit 417bc5e
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 307 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

### 2.0.0 (2014-XX-XX)

* The minimum requirement for Doctrine is now ORM 2.4 and MongoDB ODM 1.0-alpha10.
* The minimum requirement for Symfony has been bumped to 2.3 (older versions are already EOLed).
* [BC break] The ``FOSUserBundle:Security:login.html.twig`` template now receives an AuthenticationException in the ``error``
variable rather than an error message.
Expand Down
52 changes: 30 additions & 22 deletions DependencyInjection/FOSUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,29 @@
namespace FOS\UserBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;

class FOSUserExtension extends Extension
{
private static $doctrineDrivers = array(
'orm' => array(
'registry' => 'doctrine',
'tag' => 'doctrine.event_subscriber',
),
'mongodb' => array(
'registry' => 'doctrine_mongodb',
'tag' => 'doctrine_mongodb.odm.event_subscriber',
),
'couchdb' => array(
'registry' => 'doctrine_couchdb',
'tag' => 'doctrine_couchdb.event_subscriber',
),
);

public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
Expand All @@ -29,7 +45,12 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

if ('custom' !== $config['db_driver']) {
$loader->load(sprintf('%s.xml', $config['db_driver']));
if (isset(self::$doctrineDrivers[$config['db_driver']])) {
$loader->load('doctrine.xml');
$container->setAlias('fos_user.doctrine_registry', new Alias(self::$doctrineDrivers[$config['db_driver']]['registry'], false));
} else {
$loader->load(sprintf('%s.xml', $config['db_driver']));
}
$container->setParameter($this->getAlias() . '.backend_type_' . $config['db_driver'], true);
}

Expand All @@ -47,27 +68,10 @@ public function load(array $configs, ContainerBuilder $container)
$container->setAlias('fos_user.util.token_generator', $config['service']['token_generator']);
$container->setAlias('fos_user.user_manager', $config['service']['user_manager']);

if ($config['use_listener']) {
switch ($config['db_driver']) {
case 'orm':
$container->getDefinition('fos_user.user_listener')->addTag('doctrine.event_subscriber');
break;

case 'mongodb':
$container->getDefinition('fos_user.user_listener')->addTag('doctrine_mongodb.odm.event_subscriber');
break;

case 'couchdb':
$container->getDefinition('fos_user.user_listener')->addTag('doctrine_couchdb.event_subscriber');
break;

case 'propel':
break;

default:
break;
}
if ($config['use_listener'] && isset(self::$doctrineDrivers[$config['db_driver']])) {
$container->getDefinition('fos_user.user_listener')->addTag(self::$doctrineDrivers[$config['db_driver']]['tag']);
}

if ($config['use_username_form_type']) {
$loader->load('username_form_type.xml');
}
Expand Down Expand Up @@ -166,7 +170,11 @@ private function loadGroups(array $config, ContainerBuilder $container, XmlFileL
{
$loader->load('group.xml');
if ('custom' !== $dbDriver) {
$loader->load(sprintf('%s_group.xml', $dbDriver));
if (isset(self::$doctrineDrivers[$dbDriver])) {
$loader->load('doctrine_group.xml');
} else {
$loader->load(sprintf('%s_group.xml', $dbDriver));
}
}

$container->setAlias('fos_user.group_manager', $config['group_manager']);
Expand Down
50 changes: 0 additions & 50 deletions Doctrine/CouchDB/UserListener.php

This file was deleted.

62 changes: 0 additions & 62 deletions Doctrine/MongoDB/UserListener.php

This file was deleted.

62 changes: 0 additions & 62 deletions Doctrine/Orm/UserListener.php

This file was deleted.

61 changes: 44 additions & 17 deletions Doctrine/AbstractUserListener.php → Doctrine/UserListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@

use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use FOS\UserBundle\Model\UserInterface;

/**
* Base Doctrine listener updating the canonical username and password fields.
*
* Overwritten by database specific listeners to register the right events and
* to let the UoW recalculate the change set if needed.
* Doctrine listener updating the canonical username and password fields.
*
* @author Christophe Coevoet <[email protected]>
* @author David Buchmann <[email protected]>
*/
abstract class AbstractUserListener implements EventSubscriber
class UserListener implements EventSubscriber
{
/**
* @var \FOS\UserBundle\Model\UserManagerInterface
Expand All @@ -47,13 +47,21 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

public function getSubscribedEvents()
{
return array(
'prePersist',
'preUpdate',
);
}


/**
* Pre persist listener based on doctrine commons, overwrite for drivers
* that are not compatible with the commons events.
* Pre persist listener based on doctrine common
*
* @param LifecycleEventArgs $args weak typed to allow overwriting
* @param LifecycleEventArgs $args
*/
public function prePersist($args)
public function prePersist(LifecycleEventArgs $args)
{
$object = $args->getObject();
if ($object instanceof UserInterface) {
Expand All @@ -62,27 +70,25 @@ public function prePersist($args)
}

/**
* Pre update listener based on doctrine commons, overwrite to update
* the changeset in the UoW and to handle non-common event argument
* class.
* Pre update listener based on doctrine common
*
* @param LifecycleEventArgs $args weak typed to allow overwriting
* @param LifecycleEventArgs $args
*/
public function preUpdate($args)
public function preUpdate(LifecycleEventArgs $args)
{
$object = $args->getObject();
if ($object instanceof UserInterface) {
$this->updateUserFields($object);
$this->recomputeChangeSet($args->getObjectManager(), $object);
}
}

/**
* This must be called on prePersist and preUpdate if the event is about a
* user.
* Updates the user properties.
*
* @param UserInterface $user
*/
protected function updateUserFields(UserInterface $user)
private function updateUserFields(UserInterface $user)
{
if (null === $this->userManager) {
$this->userManager = $this->container->get('fos_user.user_manager');
Expand All @@ -91,4 +97,25 @@ protected function updateUserFields(UserInterface $user)
$this->userManager->updateCanonicalFields($user);
$this->userManager->updatePassword($user);
}

/**
* Recomputes change set for Doctrine implementations not doing it automatically after the event.
*
* @param ObjectManager $om
* @param UserInterface $user
*/
private function recomputeChangeSet(ObjectManager $om, UserInterface $user)
{
$meta = $om->getClassMetadata(get_class($user));

if ($om instanceof EntityManager) {
$om->getUnifOfWork()->recomputeSingleEntityChangeSet($meta, $user);

return;
}

if ($om instanceof DocumentManager) {
$om->getUnifOfWork()->recomputeSingleDocumentChangeSet($meta, $user);
}
}
}
Loading

0 comments on commit 417bc5e

Please sign in to comment.