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.

пре 3 година
пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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->persist($entity);
  35. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT);
  36. return $this;
  37. }
  38. public function update(EntityInterface $entity): self
  39. {
  40. $this->persist($entity);
  41. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT);
  42. return $this;
  43. }
  44. public function delete(EntityInterface $entity): self
  45. {
  46. $this->remove($entity);
  47. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT);
  48. return $this;
  49. }
  50. public function flush($entity=null): self
  51. {
  52. $this->wrapped->flush($entity);
  53. return $this;
  54. }
  55. public function clear($objectName = null): self
  56. {
  57. $this->wrapped->clear($objectName);
  58. return $this;
  59. }
  60. public function refresh($object): self
  61. {
  62. $this->wrapped->refresh($object);
  63. return $this;
  64. }
  65. public function persist($entity)
  66. {
  67. $this->wrapped->persist($entity);
  68. }
  69. public function getClassMetadata($className)
  70. {
  71. return $this->wrapped->getClassMetadata($className);
  72. }
  73. public function getEntityName($className)
  74. {
  75. if (substr($className, -9) === 'Interface') {
  76. return $this->wrapped->getClassMetadata($className)->getName();
  77. } else {
  78. return $className;
  79. }
  80. }
  81. }