You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.4KB

  1. <?php
  2. namespace Lc\SovBundle\Doctrine;
  3. use Doctrine\ORM\Decorator\EntityManagerDecorator;
  4. use Doctrine\ORM\EntityManager as DoctrineEntityManager;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  8. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. /**
  11. * class EntityManager.
  12. *
  13. * @author La clic !!!!
  14. */
  15. class EntityManager extends EntityManagerDecorator
  16. {
  17. protected EventDispatcherInterface $eventDispatcher;
  18. protected DoctrineEntityManager $entityManager;
  19. public function __construct(EntityManagerInterface $wrapped, EventDispatcherInterface $eventDispatcher)
  20. {
  21. $this->eventDispatcher = $eventDispatcher;
  22. parent::__construct($wrapped);
  23. }
  24. public function createQueryBuilder()
  25. {
  26. return new QueryBuilder($this);
  27. }
  28. public function getRepository($className)
  29. {
  30. return $this->wrapped->getRepository($this->getEntityName($className));
  31. }
  32. public function new($className)
  33. {
  34. $entityName = $this->getEntityName($className);
  35. return new $entityName;
  36. }
  37. public function create(EntityInterface $entity, bool $dispatchEvent = true): self
  38. {
  39. if($dispatchEvent) {
  40. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT);
  41. }
  42. $this->persist($entity);
  43. if($dispatchEvent) {
  44. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_CREATE_EVENT);
  45. }
  46. return $this;
  47. }
  48. public function update(EntityInterface $entity, bool $dispatchEvent = true): self
  49. {
  50. if($dispatchEvent){
  51. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT);
  52. }
  53. $this->persist($entity);
  54. if($dispatchEvent) {
  55. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_UPDATE_EVENT);
  56. }
  57. return $this;
  58. }
  59. public function delete(EntityInterface $entity, bool $dispatchEvent = true): self
  60. {
  61. if($dispatchEvent) {
  62. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT);
  63. }
  64. $this->remove($entity);
  65. if($dispatchEvent) {
  66. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_DELETE_EVENT);
  67. }
  68. return $this;
  69. }
  70. public function flush($entity = null): self
  71. {
  72. $this->wrapped->flush($entity);
  73. return $this;
  74. }
  75. public function clear($objectName = null): self
  76. {
  77. $this->wrapped->clear($objectName);
  78. return $this;
  79. }
  80. public function refresh($object): self
  81. {
  82. $this->wrapped->refresh($object);
  83. return $this;
  84. }
  85. public function persist($entity)
  86. {
  87. $this->wrapped->persist($entity);
  88. }
  89. public function getClassMetadata($className)
  90. {
  91. return $this->wrapped->getClassMetadata($className);
  92. }
  93. public function getEntityName($className)
  94. {
  95. if (substr($className, -9) === 'Interface') {
  96. return $this->wrapped->getClassMetadata($className)->getName();
  97. } else {
  98. return $className;
  99. }
  100. }
  101. }