1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace WorkflowBundle\EventListener;
- use Doctrine\Common\EventSubscriber;
- use Doctrine\ORM\Event\LifecycleEventArgs;
- use WorkflowBundle\Utils\DoctrineEvents;
- use WorkflowBundle\Utils\WorkFlowEntityClasses;
- class DoctrineEventSubscriber implements EventSubscriber
- {
- /**
- * @return array
- */
- public function getSubscribedEvents()
- {
- return DoctrineEvents::getConstants();
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function prePersist(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::PRE_PERSIST);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postPersist(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::POST_PERSIST);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function preUpdate(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::PRE_UPDATE);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postUpdate(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::POST_UPDATE);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function preRemove(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::PRE_REMOVE);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postRemove(LifecycleEventArgs $args)
- {
- $this->execute($args, DoctrineEvents::POST_REMOVE);
- }
- /**
- * @param LifecycleEventArgs $args
- * @param string $eventName
- */
- public function execute(LifecycleEventArgs $args, $eventName = DoctrineEvents::PRE_PERSIST)
- {
- $entity = $args->getEntity();
- $entityManager = $args->getEntityManager();
- $entityClass = get_class($entity);
-
- // la $entity esta dentro de las entidades con workflow
- if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
- $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
- $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
- foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
- $actions = $doctrine2WorkFlowAction->getActions();
- foreach ($actions as $action) {
- // $action->execute($entity); // ejecutar accion, template, etc
- }
- }
- }
- }
- }
|