forked from KnpLabs/KnpUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed support for older Doctrine versions
The minimal versions are now Doctrine Common 2.3, Doctrine ORM 2.4 and Doctrine MongoDB ODM 1.0-alpha10.
- Loading branch information
Showing
12 changed files
with
79 additions
and
307 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -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) { | ||
|
@@ -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'); | ||
|
@@ -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); | ||
} | ||
} | ||
} |
Oops, something went wrong.