|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Lc\ShopBundle\Manager; |
|
|
namespace Lc\ShopBundle\Manager; |
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager as DoctrineEntityManager; |
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
|
use Lc\ShopBundle\Context\UserInterface; |
|
|
use Lc\ShopBundle\Event\EntityManager\EntityManagerEvent; |
|
|
use Lc\ShopBundle\Event\EntityManager\EntityManagerEvent; |
|
|
//use Lc\ShopBundle\IModel\EntityInterface; |
|
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
|
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* class EntityManager. |
|
|
* class EntityManager. |
|
|
|
|
|
|
|
|
class EntityManager |
|
|
class EntityManager |
|
|
{ |
|
|
{ |
|
|
protected $eventDispatcher; |
|
|
protected $eventDispatcher; |
|
|
|
|
|
|
|
|
protected $entityManager; |
|
|
protected $entityManager; |
|
|
|
|
|
protected $security; |
|
|
|
|
|
protected $userSystem = null; |
|
|
|
|
|
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, EntityManagerInterface $entityManager) |
|
|
|
|
|
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, EntityManagerInterface $entityManager, Security $security) |
|
|
{ |
|
|
{ |
|
|
$this->eventDispatcher = $eventDispatcher; |
|
|
$this->eventDispatcher = $eventDispatcher; |
|
|
$this->entityManager = $entityManager; |
|
|
$this->entityManager = $entityManager; |
|
|
|
|
|
$this->security = $security; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getUserSystem() |
|
|
|
|
|
{ |
|
|
|
|
|
if ($this->userSystem === null) { |
|
|
|
|
|
$this->userSystem = $this->entityManager->getRepository($this->getEntityName(UserInterface::class))->findOneByDevAlias('system'); |
|
|
|
|
|
} |
|
|
|
|
|
return $this->userSystem; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getRepository($className) |
|
|
public function getRepository($className) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function create($entity): self |
|
|
public function create($entity): self |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
if ($this->security->getUser() === null) { |
|
|
|
|
|
$entity->setUpdatedBy($this->getUserSystem()); |
|
|
|
|
|
$entity->setCreadtedBy($this->getUserSystem()); |
|
|
|
|
|
} |
|
|
$this->persist($entity); |
|
|
$this->persist($entity); |
|
|
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT); |
|
|
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT); |
|
|
|
|
|
|
|
|
return $this; |
|
|
return $this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function update($entity): self |
|
|
public function update($entity): self |
|
|
{ |
|
|
{ |
|
|
|
|
|
if ($this->security->getUser() === null) { |
|
|
|
|
|
$entity->setUpdatedBy($this->getUserSystem()); |
|
|
|
|
|
} |
|
|
$this->persist($entity); |
|
|
$this->persist($entity); |
|
|
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT); |
|
|
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
{ |
|
|
if (substr($className, -9) === 'Interface') { |
|
|
if (substr($className, -9) === 'Interface') { |
|
|
return $this->entityManager->getClassMetadata($className)->getName(); |
|
|
return $this->entityManager->getClassMetadata($className)->getName(); |
|
|
}else{ |
|
|
|
|
|
|
|
|
} else { |
|
|
return $className; |
|
|
return $className; |
|
|
} |
|
|
} |
|
|
|
|
|
|