Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Enable different entity managers for multiple log entities #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
namespace Xiidea\EasyAuditBundle\Logger;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Xiidea\EasyAuditBundle\Model\BaseAuditLog as AuditLog;
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
use Xiidea\EasyAuditBundle\Model\BaseAuditLog as AuditLog;

class Logger implements LoggerInterface
{
Expand Down Expand Up @@ -45,29 +44,14 @@ public function log(AuditLog $event = null)
$this->saveLog($event);
}

/**
* @return ObjectManager
*/
protected function getManager()
{
return $this->getDoctrine()->getManager();
}

/**
* @return \Doctrine\Common\Persistence\ManagerRegistry
*/
public function getDoctrine()
{
return $this->doctrine;
}

/**
* @param AuditLog $event
*/
protected function saveLog(AuditLog $event)
{
$this->getManager()->persist($event);
$this->getManager()->flush($event);
$manager = $this->doctrine->getManagerForClass($event);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getManagerForClass expect class name not an object

$manager->persist($event);
$manager->flush($event);
}

public function savePendingLogs()
Expand Down
26 changes: 5 additions & 21 deletions Resolver/DoctrineObjectEventResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

namespace Xiidea\EasyAuditBundle\Resolver;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\EventDispatcher\Event;
use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent;
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent;

/** Custom Event Resolver Example Class */
class DoctrineObjectEventResolver implements EventResolverInterface
Expand All @@ -32,9 +31,9 @@ class DoctrineObjectEventResolver implements EventResolverInterface
protected $identity = ['', ''];

/**
* @var ManagerRegistry
* @var \Doctrine\ORM\EntityManager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we also support ODM, we don't want direct dependency with ORM. You can see we remove the dependency in a earlier commit

*/
protected $doctrine;
protected $manager;

protected $changeSetGetterMethods = [
'getEntityChangeSet',
Expand Down Expand Up @@ -88,6 +87,7 @@ private function initialize(DoctrineObjectEvent $event, $eventName)
$this->eventName = $eventName;
$this->event = $event;
$this->entity = $event->getLifecycleEventArgs()->getObject();
$this->manager = $event->getLifecycleEventArgs()->getObjectManager();
$this->identity = $this->getSingleIdentity();
}

Expand Down Expand Up @@ -182,22 +182,6 @@ protected function getReflectionClassFromObject($object)
*/
protected function getUnitOfWork()
{
return $this->getDoctrine()->getManager()->getUnitOfWork();
}

/**
* @return ManagerRegistry|object
*/
protected function getDoctrine()
{
return $this->doctrine;
}

/**
* @param ManagerRegistry $doctrine
*/
public function setDoctrine($doctrine)
{
$this->doctrine = $doctrine;
return $this->manager->getUnitOfWork();
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"phpunit/phpunit": "^5.0 || ^6.0",
"friendsofsymfony/user-bundle": "^2.0",
"php-coveralls/php-coveralls": "^2.1",
"doctrine/common": ">=2.2 <3.0"
"doctrine/common": ">=2.2 <3.0",
"doctrine/orm": "^2.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No dependency should be with ORM

},
"autoload": {
"psr-4": {
Expand Down