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.

EntityManager.php 2.9KB

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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): self
  36. {
  37. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT);
  38. $this->persist($entity);
  39. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_CREATE_EVENT);
  40. return $this;
  41. }
  42. public function update(EntityInterface $entity): self
  43. {
  44. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT);
  45. $this->persist($entity);
  46. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_UPDATE_EVENT);
  47. return $this;
  48. }
  49. public function delete(EntityInterface $entity): self
  50. {
  51. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT);
  52. $this->remove($entity);
  53. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_DELETE_EVENT);
  54. return $this;
  55. }
  56. public function flush($entity = null): self
  57. {
  58. $this->wrapped->flush($entity);
  59. return $this;
  60. }
  61. public function clear($objectName = null): self
  62. {
  63. $this->wrapped->clear($objectName);
  64. return $this;
  65. }
  66. public function refresh($object): self
  67. {
  68. $this->wrapped->refresh($object);
  69. return $this;
  70. }
  71. public function persist($entity)
  72. {
  73. $this->wrapped->persist($entity);
  74. }
  75. public function getClassMetadata($className)
  76. {
  77. return $this->wrapped->getClassMetadata($className);
  78. }
  79. public function getEntityName($className)
  80. {
  81. if (substr($className, -9) === 'Interface') {
  82. return $this->wrapped->getClassMetadata($className)->getName();
  83. } else {
  84. return $className;
  85. }
  86. }
  87. }