Skip to content

Commit

Permalink
consolidate doctrine listeners and move to Doctrine directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed May 27, 2013
1 parent 9e60b38 commit 3dba740
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 3 deletions.
41 changes: 41 additions & 0 deletions Doctrine/CouchDB/CouchDBUserListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace FOS\UserBundle\Doctrine\CouchDB;

use Doctrine\ODM\CouchDB\Event;
use Doctrine\ODM\CouchDB\Event\LifecycleEventArgs;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Doctrine\UserListener;

class CouchDBUserListener extends UserListener
{
public function getSubscribedEvents()
{
return array(
Event::prePersist,
Event::preUpdate,
);
}

/**
* @param LifecycleEventArgs $args
*/
public function prePersist($args)
{
$object = $args->getDocument();
if ($object instanceof UserInterface) {
$this->updateUserFields($object);
}
}

/**
* @param LifecycleEventArgs $args
*/
public function preUpdate($args)
{
$object = $args->getDocument();
if ($object instanceof UserInterface) {
$this->updateUserFields($object);
}
}
}
64 changes: 64 additions & 0 deletions Doctrine/MongoDB/MongoDBUserListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\UserBundle\Doctrine\MongoDB;

use Doctrine\Common\EventSubscriber;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs;
use FOS\UserBundle\Doctrine\UserListener;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Doctrine MongoDB ODM listener updating the canonical fields and the password.
*
* @author Christophe Coevoet <[email protected]>
*/
class MongoDBUserListener extends UserListener
{
public function getSubscribedEvents()
{
return array(
Events::prePersist,
Events::preUpdate,
);
}

/**
* @param LifecycleEventArgs $args
*/
public function prePersist($args)
{
$object = $args->getDocument();
if ($object instanceof UserInterface) {
$this->updateUserFields($object);
}
}

/**
* @param PreUpdateEventArgs $args
*/
public function preUpdate($args)
{
$object = $args->getDocument();
if ($object instanceof UserInterface) {
$this->updateUserFields($object);
// We are doing a update, so we must force Doctrine to update the
// changeset in case we changed something above
$dm = $args->getDocumentManager();
$uow = $dm->getUnitOfWork();
$meta = $dm->getClassMetadata(get_class($object));
$uow->recomputeSingleDocumentChangeSet($meta, $object);
}
}
}
63 changes: 63 additions & 0 deletions Doctrine/Orm/OrmUserListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\UserBundle\Doctrine\Orm;

use Doctrine\ORM\Events;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Doctrine\UserListener;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Doctrine ORM listener updating the canonical fields and the password.
*
* @author Christophe Coevoet <[email protected]>
*/
class OrmUserListener extends UserListener
{
public function getSubscribedEvents()
{
return array(
Events::prePersist,
Events::preUpdate,
);
}

/**
* @param LifecycleEventArgs $args
*/
public function prePersist($args)
{
$object = $args->getEntity();
if ($object instanceof UserInterface) {
$this->updateUserFields($object);
}
}

/**
* @param PreUpdateEventArgs $args
*/
public function preUpdate($args)
{
$object = $args->getEntity();
if ($object instanceof UserInterface) {
$this->updateUserFields($object);
// We are doing a update, so we must force Doctrine to update the
// changeset in case we changed something above
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$meta = $em->getClassMetadata(get_class($object));
$uow->recomputeSingleEntityChangeSet($meta, $object);
}
}
}
95 changes: 95 additions & 0 deletions Doctrine/UserListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\UserBundle\Doctrine;

use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* 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.
*
* @author Christophe Coevoet <[email protected]>
* @author David Buchmann <[email protected]>
*/
abstract class UserListener implements EventSubscriber
{
/**
* @var \FOS\UserBundle\Model\UserManagerInterface
*/
private $userManager;

/**
* @var ContainerInterface
*/
private $container;

/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

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

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

/**
* This must be called on prePersist and preUpdate if the event is about a
* user.
*
* @param UserInterface $user
*/
protected function updateUserFields(UserInterface $user)
{
if (null === $this->userManager) {
$this->userManager = $this->container->get('fos_user.user_manager');
}

$this->userManager->updateCanonicalFields($user);
$this->userManager->updatePassword($user);
}
}
2 changes: 1 addition & 1 deletion Resources/config/couchdb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<argument>%fos_user.model_manager_name%</argument>
</service>

<service id="fos_user.user_listener" class="FOS\UserBundle\CouchDocument\UserListener" public="false">
<service id="fos_user.user_listener" class="FOS\UserBundle\Doctrine\CouchDB\CouchDBUserListener" public="false">
<argument type="service" id="service_container" />
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<argument>%fos_user.model_manager_name%</argument>
</service>

<service id="fos_user.user_listener" class="FOS\UserBundle\Document\UserListener" public="false">
<service id="fos_user.user_listener" class="FOS\UserBundle\Doctrine\MongoDB\MongoDBUserListener" public="false">
<argument type="service" id="service_container" />
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<argument>%fos_user.model_manager_name%</argument>
</service>

<service id="fos_user.user_listener" class="FOS\UserBundle\Entity\UserListener" public="false">
<service id="fos_user.user_listener" class="FOS\UserBundle\Doctrine\Orm\OrmUserListener" public="false">
<argument type="service" id="service_container" />
</service>
</services>
Expand Down

0 comments on commit 3dba740

Please sign in to comment.