No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

EntityManager.php 3.4KB

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 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): void
  76. {
  77. $this->wrapped->clear($objectName);
  78. }
  79. public function refresh($object): self
  80. {
  81. $this->wrapped->refresh($object);
  82. return $this;
  83. }
  84. public function persist($entity):void
  85. {
  86. $this->wrapped->persist($entity);
  87. }
  88. public function getClassMetadata($className)
  89. {
  90. return $this->wrapped->getClassMetadata($className);
  91. }
  92. public function getEntityName($className)
  93. {
  94. if (substr($className, -9) === 'Interface') {
  95. return $this->wrapped->getClassMetadata($className)->getName();
  96. } else {
  97. return $className;
  98. }
  99. }
  100. }