Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

EntityManager.php 2.9KB

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