Skip to content

Commit ab580c1

Browse files
committed
Merge pull request #1136 from dbu/move-listeners
consolidate doctrine listeners and move to Doctrine directory
2 parents f0a7276 + 16d963b commit ab580c1

File tree

10 files changed

+271
-236
lines changed

10 files changed

+271
-236
lines changed

CouchDocument/UserListener.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

Doctrine/AbstractUserListener.php

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

Doctrine/CouchDB/UserListener.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\CouchDB;
13+
14+
use Doctrine\ODM\CouchDB\Event;
15+
use Doctrine\ODM\CouchDB\Event\LifecycleEventArgs;
16+
use FOS\UserBundle\Model\UserInterface;
17+
use FOS\UserBundle\Doctrine\AbstractUserListener;
18+
19+
class UserListener extends AbstractUserListener
20+
{
21+
public function getSubscribedEvents()
22+
{
23+
return array(
24+
Event::prePersist,
25+
Event::preUpdate,
26+
);
27+
}
28+
29+
/**
30+
* @param LifecycleEventArgs $args
31+
*/
32+
public function prePersist($args)
33+
{
34+
$object = $args->getDocument();
35+
if ($object instanceof UserInterface) {
36+
$this->updateUserFields($object);
37+
}
38+
}
39+
40+
/**
41+
* @param LifecycleEventArgs $args
42+
*/
43+
public function preUpdate($args)
44+
{
45+
$object = $args->getDocument();
46+
if ($object instanceof UserInterface) {
47+
$this->updateUserFields($object);
48+
}
49+
}
50+
}

Doctrine/MongoDB/UserListener.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\ODM\MongoDB\Events;
15+
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
16+
use Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs;
17+
use FOS\UserBundle\Doctrine\AbstractUserListener;
18+
use FOS\UserBundle\Model\UserInterface;
19+
20+
/**
21+
* Doctrine MongoDB ODM listener updating the canonical fields and the password.
22+
*
23+
* @author Christophe Coevoet <[email protected]>
24+
*/
25+
class UserListener extends AbstractUserListener
26+
{
27+
public function getSubscribedEvents()
28+
{
29+
return array(
30+
Events::prePersist,
31+
Events::preUpdate,
32+
);
33+
}
34+
35+
/**
36+
* @param LifecycleEventArgs $args
37+
*/
38+
public function prePersist($args)
39+
{
40+
$object = $args->getDocument();
41+
if ($object instanceof UserInterface) {
42+
$this->updateUserFields($object);
43+
}
44+
}
45+
46+
/**
47+
* @param PreUpdateEventArgs $args
48+
*/
49+
public function preUpdate($args)
50+
{
51+
$object = $args->getDocument();
52+
if ($object instanceof UserInterface) {
53+
$this->updateUserFields($object);
54+
// We are doing a update, so we must force Doctrine to update the
55+
// changeset in case we changed something above
56+
$dm = $args->getDocumentManager();
57+
$uow = $dm->getUnitOfWork();
58+
$meta = $dm->getClassMetadata(get_class($object));
59+
$uow->recomputeSingleDocumentChangeSet($meta, $object);
60+
}
61+
}
62+
}

Doctrine/Orm/UserListener.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\AbstractUserListener;
19+
20+
/**
21+
* Doctrine ORM listener updating the canonical fields and the password.
22+
*
23+
* @author Christophe Coevoet <[email protected]>
24+
*/
25+
class UserListener extends AbstractUserListener
26+
{
27+
public function getSubscribedEvents()
28+
{
29+
return array(
30+
Events::prePersist,
31+
Events::preUpdate,
32+
);
33+
}
34+
35+
/**
36+
* @param LifecycleEventArgs $args
37+
*/
38+
public function prePersist($args)
39+
{
40+
$object = $args->getEntity();
41+
if ($object instanceof UserInterface) {
42+
$this->updateUserFields($object);
43+
}
44+
}
45+
46+
/**
47+
* @param PreUpdateEventArgs $args
48+
*/
49+
public function preUpdate($args)
50+
{
51+
$object = $args->getEntity();
52+
if ($object instanceof UserInterface) {
53+
$this->updateUserFields($object);
54+
// We are doing a update, so we must force Doctrine to update the
55+
// changeset in case we changed something above
56+
$em = $args->getEntityManager();
57+
$uow = $em->getUnitOfWork();
58+
$meta = $em->getClassMetadata(get_class($object));
59+
$uow->recomputeSingleEntityChangeSet($meta, $object);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)