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.
consolidate doctrine listeners and move to Doctrine directory
- Loading branch information
Showing
7 changed files
with
266 additions
and
3 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,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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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 | ||
|
||
/* | ||
* 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); | ||
} | ||
} |
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