Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

EntityManager.php 3.3KB

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