Skip to content

Commit 3dba740

Browse files
committed
consolidate doctrine listeners and move to Doctrine directory
1 parent 9e60b38 commit 3dba740

File tree

7 files changed

+266
-3
lines changed

7 files changed

+266
-3
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Doctrine\CouchDB;
4+
5+
use Doctrine\ODM\CouchDB\Event;
6+
use Doctrine\ODM\CouchDB\Event\LifecycleEventArgs;
7+
use FOS\UserBundle\Model\UserInterface;
8+
use FOS\UserBundle\Doctrine\UserListener;
9+
10+
class CouchDBUserListener extends UserListener
11+
{
12+
public function getSubscribedEvents()
13+
{
14+
return array(
15+
Event::prePersist,
16+
Event::preUpdate,
17+
);
18+
}
19+
20+
/**
21+
* @param LifecycleEventArgs $args
22+
*/
23+
public function prePersist($args)
24+
{
25+
$object = $args->getDocument();
26+
if ($object instanceof UserInterface) {
27+
$this->updateUserFields($object);
28+
}
29+
}
30+
31+
/**
32+
* @param LifecycleEventArgs $args
33+
*/
34+
public function preUpdate($args)
35+
{
36+
$object = $args->getDocument();
37+
if ($object instanceof UserInterface) {
38+
$this->updateUserFields($object);
39+
}
40+
}
41+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSUserBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\UserBundle\Doctrine\MongoDB;
13+
14+
use Doctrine\Common\EventSubscriber;
15+
use Doctrine\ODM\MongoDB\Events;
16+
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
17+
use Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs;
18+
use FOS\UserBundle\Doctrine\UserListener;
19+
use FOS\UserBundle\Model\UserInterface;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
22+
/**
23+
* Doctrine MongoDB ODM listener updating the canonical fields and the password.
24+
*
25+
* @author Christophe Coevoet <[email protected]>
26+
*/
27+
class MongoDBUserListener extends UserListener
28+
{
29+
public function getSubscribedEvents()
30+
{
31+
return array(
32+
Events::prePersist,
33+
Events::preUpdate,
34+
);
35+
}
36+
37+
/**
38+
* @param LifecycleEventArgs $args
39+
*/
40+
public function prePersist($args)
41+
{
42+
$object = $args->getDocument();
43+
if ($object instanceof UserInterface) {
44+
$this->updateUserFields($object);
45+
}
46+
}
47+
48+
/**
49+
* @param PreUpdateEventArgs $args
50+
*/
51+
public function preUpdate($args)
52+
{
53+
$object = $args->getDocument();
54+
if ($object instanceof UserInterface) {
55+
$this->updateUserFields($object);
56+
// We are doing a update, so we must force Doctrine to update the
57+
// changeset in case we changed something above
58+
$dm = $args->getDocumentManager();
59+
$uow = $dm->getUnitOfWork();
60+
$meta = $dm->getClassMetadata(get_class($object));
61+
$uow->recomputeSingleDocumentChangeSet($meta, $object);
62+
}
63+
}
64+
}

Doctrine/Orm/OrmUserListener.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSUserBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\UserBundle\Doctrine\Orm;
13+
14+
use Doctrine\ORM\Events;
15+
use Doctrine\ORM\Event\LifecycleEventArgs;
16+
use Doctrine\ORM\Event\PreUpdateEventArgs;
17+
use FOS\UserBundle\Model\UserInterface;
18+
use FOS\UserBundle\Doctrine\UserListener;
19+
use Symfony\Component\DependencyInjection\ContainerInterface;
20+
21+
/**
22+
* Doctrine ORM listener updating the canonical fields and the password.
23+
*
24+
* @author Christophe Coevoet <[email protected]>
25+
*/
26+
class OrmUserListener extends UserListener
27+
{
28+
public function getSubscribedEvents()
29+
{
30+
return array(
31+
Events::prePersist,
32+
Events::preUpdate,
33+
);
34+
}
35+
36+
/**
37+
* @param LifecycleEventArgs $args
38+
*/
39+
public function prePersist($args)
40+
{
41+
$object = $args->getEntity();
42+
if ($object instanceof UserInterface) {
43+
$this->updateUserFields($object);
44+
}
45+
}
46+
47+
/**
48+
* @param PreUpdateEventArgs $args
49+
*/
50+
public function preUpdate($args)
51+
{
52+
$object = $args->getEntity();
53+
if ($object instanceof UserInterface) {
54+
$this->updateUserFields($object);
55+
// We are doing a update, so we must force Doctrine to update the
56+
// changeset in case we changed something above
57+
$em = $args->getEntityManager();
58+
$uow = $em->getUnitOfWork();
59+
$meta = $em->getClassMetadata(get_class($object));
60+
$uow->recomputeSingleEntityChangeSet($meta, $object);
61+
}
62+
}
63+
}

Doctrine/UserListener.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSUserBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\UserBundle\Doctrine;
13+
14+
use Doctrine\Common\EventSubscriber;
15+
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
16+
use Doctrine\Common\Persistence\Event\PreUpdateEventArgs;
17+
use FOS\UserBundle\Model\UserInterface;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
19+
20+
/**
21+
* Base Doctrine listener updating the canonical username and password fields.
22+
*
23+
* Overwritten by database specific listeners to register the right events and
24+
* to let the UoW recalculate the change set if needed.
25+
*
26+
* @author Christophe Coevoet <[email protected]>
27+
* @author David Buchmann <[email protected]>
28+
*/
29+
abstract class UserListener implements EventSubscriber
30+
{
31+
/**
32+
* @var \FOS\UserBundle\Model\UserManagerInterface
33+
*/
34+
private $userManager;
35+
36+
/**
37+
* @var ContainerInterface
38+
*/
39+
private $container;
40+
41+
/**
42+
* Constructor
43+
*
44+
* @param ContainerInterface $container
45+
*/
46+
public function __construct(ContainerInterface $container)
47+
{
48+
$this->container = $container;
49+
}
50+
51+
/**
52+
* Pre persist listener based on doctrine commons, overwrite for drivers
53+
* that are not compatible with the commons events.
54+
*
55+
* @param LifecycleEventArgs $args weak typed to allow overwriting
56+
*/
57+
public function prePersist($args)
58+
{
59+
$object = $args->getObject();
60+
if ($object instanceof UserInterface) {
61+
$this->updateUserFields($object);
62+
}
63+
}
64+
65+
/**
66+
* Pre update listener based on doctrine commons, overwrite to update
67+
* the changeset in the UoW and to handle non-common event argument
68+
* class.
69+
*
70+
* @param LifecycleEventArgs $args weak typed to allow overwriting
71+
*/
72+
public function preUpdate($args)
73+
{
74+
$object = $args->getObject();
75+
if ($object instanceof UserInterface) {
76+
$this->updateUserFields($object);
77+
}
78+
}
79+
80+
/**
81+
* This must be called on prePersist and preUpdate if the event is about a
82+
* user.
83+
*
84+
* @param UserInterface $user
85+
*/
86+
protected function updateUserFields(UserInterface $user)
87+
{
88+
if (null === $this->userManager) {
89+
$this->userManager = $this->container->get('fos_user.user_manager');
90+
}
91+
92+
$this->userManager->updateCanonicalFields($user);
93+
$this->userManager->updatePassword($user);
94+
}
95+
}

Resources/config/couchdb.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<argument>%fos_user.model_manager_name%</argument>
2222
</service>
2323

24-
<service id="fos_user.user_listener" class="FOS\UserBundle\CouchDocument\UserListener" public="false">
24+
<service id="fos_user.user_listener" class="FOS\UserBundle\Doctrine\CouchDB\CouchDBUserListener" public="false">
2525
<argument type="service" id="service_container" />
2626
</service>
2727
</services>

Resources/config/mongodb.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<argument>%fos_user.model_manager_name%</argument>
1818
</service>
1919

20-
<service id="fos_user.user_listener" class="FOS\UserBundle\Document\UserListener" public="false">
20+
<service id="fos_user.user_listener" class="FOS\UserBundle\Doctrine\MongoDB\MongoDBUserListener" public="false">
2121
<argument type="service" id="service_container" />
2222
</service>
2323
</services>

Resources/config/orm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<argument>%fos_user.model_manager_name%</argument>
1818
</service>
1919

20-
<service id="fos_user.user_listener" class="FOS\UserBundle\Entity\UserListener" public="false">
20+
<service id="fos_user.user_listener" class="FOS\UserBundle\Doctrine\Orm\OrmUserListener" public="false">
2121
<argument type="service" id="service_container" />
2222
</service>
2323
</services>

0 commit comments

Comments
 (0)