|
@@ -5,9 +5,25 @@ namespace WorkflowBundle\EventListener;
|
|
|
use Doctrine\Common\EventSubscriber;
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
|
use WorkflowBundle\Utils\DoctrineEvents;
|
|
|
+use WorkflowBundle\Utils\WorkFlowEntityClasses;
|
|
|
+use OldSound\RabbitMqBundle\RabbitMq\Producer;
|
|
|
|
|
|
class DoctrineEventSubscriber implements EventSubscriber
|
|
|
{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var Producer
|
|
|
+ */
|
|
|
+ private $producer;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Producer $producer
|
|
|
+ */
|
|
|
+ public function __construct(Producer $producer)
|
|
|
+ {
|
|
|
+ $this->producer = $producer;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* @return array
|
|
@@ -22,7 +38,7 @@ class DoctrineEventSubscriber implements EventSubscriber
|
|
|
*/
|
|
|
public function prePersist(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- $this->execute($args);
|
|
|
+ $this->execute($args, DoctrineEvents::PRE_PERSIST);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -30,7 +46,7 @@ class DoctrineEventSubscriber implements EventSubscriber
|
|
|
*/
|
|
|
public function postPersist(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- $this->execute($args);
|
|
|
+ $this->execute($args, DoctrineEvents::POST_PERSIST);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -38,7 +54,7 @@ class DoctrineEventSubscriber implements EventSubscriber
|
|
|
*/
|
|
|
public function preUpdate(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- $this->execute($args);
|
|
|
+ $this->execute($args, DoctrineEvents::PRE_UPDATE);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -46,7 +62,7 @@ class DoctrineEventSubscriber implements EventSubscriber
|
|
|
*/
|
|
|
public function postUpdate(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- $this->execute($args);
|
|
|
+ $this->execute($args, DoctrineEvents::POST_UPDATE);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -54,7 +70,7 @@ class DoctrineEventSubscriber implements EventSubscriber
|
|
|
*/
|
|
|
public function preRemove(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- $this->execute($args);
|
|
|
+ $this->execute($args, DoctrineEvents::PRE_REMOVE);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -62,20 +78,34 @@ class DoctrineEventSubscriber implements EventSubscriber
|
|
|
*/
|
|
|
public function postRemove(LifecycleEventArgs $args)
|
|
|
{
|
|
|
- $this->execute($args);
|
|
|
+ $this->execute($args, DoctrineEvents::POST_REMOVE);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param LifecycleEventArgs $args
|
|
|
+ * @param string $eventName
|
|
|
*/
|
|
|
- public function execute(LifecycleEventArgs $args, $eventName = 'prePersist')
|
|
|
+ public function execute(LifecycleEventArgs $args, $eventName = DoctrineEvents::PRE_PERSIST)
|
|
|
{
|
|
|
$entity = $args->getEntity();
|
|
|
$entityManager = $args->getEntityManager();
|
|
|
$entityClass = get_class($entity);
|
|
|
|
|
|
- $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
|
|
|
- $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
|
|
|
+ // 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) {
|
|
|
+ $msg = array(
|
|
|
+ 'id' => uniqid(),
|
|
|
+ 'cmd' => $action->getCmd($entity),
|
|
|
+ );
|
|
|
+ $this->producer->publish(serialize($msg));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|