DoctrineEventSubscriber.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace WorkflowBundle\EventListener;
  3. use Doctrine\Common\EventSubscriber;
  4. use Doctrine\ORM\Event\LifecycleEventArgs;
  5. use WorkflowBundle\Utils\DoctrineEvents;
  6. use WorkflowBundle\Utils\WorkFlowEntityClasses;
  7. class DoctrineEventSubscriber implements EventSubscriber
  8. {
  9. /**
  10. * @return array
  11. */
  12. public function getSubscribedEvents()
  13. {
  14. return DoctrineEvents::getConstants();
  15. }
  16. /**
  17. * @param LifecycleEventArgs $args
  18. */
  19. public function prePersist(LifecycleEventArgs $args)
  20. {
  21. $this->execute($args, DoctrineEvents::PRE_PERSIST);
  22. }
  23. /**
  24. * @param LifecycleEventArgs $args
  25. */
  26. public function postPersist(LifecycleEventArgs $args)
  27. {
  28. $this->execute($args, DoctrineEvents::POST_PERSIST);
  29. }
  30. /**
  31. * @param LifecycleEventArgs $args
  32. */
  33. public function preUpdate(LifecycleEventArgs $args)
  34. {
  35. $this->execute($args, DoctrineEvents::PRE_UPDATE);
  36. }
  37. /**
  38. * @param LifecycleEventArgs $args
  39. */
  40. public function postUpdate(LifecycleEventArgs $args)
  41. {
  42. $this->execute($args, DoctrineEvents::POST_UPDATE);
  43. }
  44. /**
  45. * @param LifecycleEventArgs $args
  46. */
  47. public function preRemove(LifecycleEventArgs $args)
  48. {
  49. $this->execute($args, DoctrineEvents::PRE_REMOVE);
  50. }
  51. /**
  52. * @param LifecycleEventArgs $args
  53. */
  54. public function postRemove(LifecycleEventArgs $args)
  55. {
  56. $this->execute($args, DoctrineEvents::POST_REMOVE);
  57. }
  58. /**
  59. * @param LifecycleEventArgs $args
  60. * @param string $eventName
  61. */
  62. public function execute(LifecycleEventArgs $args, $eventName = DoctrineEvents::PRE_PERSIST)
  63. {
  64. $entity = $args->getEntity();
  65. $entityManager = $args->getEntityManager();
  66. $entityClass = get_class($entity);
  67. // la $entity esta dentro de las entidades con workflow
  68. if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
  69. $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
  70. $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
  71. foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
  72. $actions = $doctrine2WorkFlowAction->getActions();
  73. foreach ($actions as $action) {
  74. // $action->execute($entity); // ejecutar accion, template, etc
  75. }
  76. }
  77. }
  78. }
  79. }