public function createProcessEngine(Configuration $config, LoggerInterface $logger = NULL)
 {
     $conn = $this->connectionManager->getConnection($config->getString('connection', 'default'));
     $transactional = $config->getBoolean('transactional', true);
     $dispatcher = $this->container->get(EventDispatcher::class);
     $engine = new ProcessEngine($conn, $dispatcher, $this->factory, $transactional);
     $engine->setDelegateTaskFactory($this->taskFactory);
     $engine->registerExecutionInterceptor(new ScopeExecutionInterceptor($this->scope));
     $engine->setLogger($logger);
     $executor = new JobExecutor($engine, $this->scheduler);
     $this->container->eachMarked(function (JobHandler $handler, BindingInterface $binding) use($executor) {
         $executor->registerJobHandler($this->container->getBound($binding));
     });
     $engine->setJobExecutor($executor);
     $dispatcher->connect(function (AbstractProcessEvent $event) {
         $this->scope->enterContext($event->execution);
     });
     $dispatcher->connect(function (TaskExecutedEvent $event) {
         $query = $event->engine->getRuntimeService()->createExecutionQuery();
         $query->executionId($event->execution->getExecutionId());
         $definition = $query->findOne()->getProcessDefinition();
         $processKey = $definition->getKey();
         $taskKey = $event->execution->getActivityId();
         $this->container->eachMarked(function (TaskHandler $handler, BindingInterface $binding) use($event, $taskKey, $processKey) {
             if ($taskKey == $handler->taskKey) {
                 if ($handler->processKey === NULL || $handler->processKey == $processKey) {
                     $task = $this->container->getBound($binding);
                     if (!$task instanceof TaskHandlerInterface) {
                         throw new \RuntimeException('Invalid task handler implementation: ' . get_class($task));
                     }
                     $task->executeTask($event->execution);
                 }
             }
         });
     });
     return $engine;
 }
Esempio n. 2
0
 protected function setUp()
 {
     parent::setUp();
     $this->eventDispatcher = new EventDispatcher();
     $this->conn = static::createConnection('bpm_', $this->eventDispatcher);
     static::migrateDirectoryUp($this->conn, __DIR__ . '/../../migration');
     $logger = NULL;
     if (NULL !== ($logLevel = $this->getLogLevel())) {
         $stderr = fopen('php://stderr', 'wb');
         $levels = array_change_key_case(Logger::getLevels(), CASE_UPPER);
         $logger = new Logger('BPMN');
         $logger->pushHandler(new StreamHandler($stderr, $levels[strtoupper($logLevel)]));
         $logger->pushProcessor(new PsrLogMessageProcessor());
         fwrite($stderr, "\n");
         fwrite($stderr, sprintf("TEST CASE: %s\n", $this->getName()));
         // 			$this->conn$conn->setDebug(true);
         // 			$this->conn->setLogger($logger);
     }
     $this->messageHandlers = [];
     $this->serviceTaskHandlers = [];
     // Provide message handler subscriptions.
     $this->eventDispatcher->connect(function (MessageThrownEvent $event) {
         $def = $this->repositoryService->createProcessDefinitionQuery()->processDefinitionId($event->execution->getProcessDefinitionId())->findOne();
         $key = $def->getKey();
         $id = $event->execution->getActivityId();
         if (isset($this->messageHandlers[$key][$id])) {
             return $this->messageHandlers[$key][$id]($event);
         }
     });
     $this->eventDispatcher->connect(function (TaskExecutedEvent $event) {
         $execution = $this->runtimeService->createExecutionQuery()->executionId($event->execution->getExecutionId())->findOne();
         $key = $execution->getProcessDefinition()->getKey();
         $id = $event->execution->getActivityId();
         if (isset($this->serviceTaskHandlers[$key][$id])) {
             $this->serviceTaskHandlers[$key][$id]($event->execution);
         }
     });
     // Allow for assertions in expressions, e.g. #{ @test.assertEquals(2, processVariable) }
     $this->eventDispatcher->connect(function (CreateExpressionContextEvent $event) {
         $event->access->setVariable('@test', $this);
     });
     $this->delegateTasks = new DelegateTaskRegistry();
     $this->processEngine = new ProcessEngine($this->conn, $this->eventDispatcher, new ExpressionContextFactory());
     $this->processEngine->setDelegateTaskFactory($this->delegateTasks);
     $this->processEngine->setLogger($logger);
     $scheduler = new TestJobScheduler($this->processEngine);
     $this->jobExecutor = new JobExecutor($this->processEngine, $scheduler);
     $this->processEngine->setJobExecutor($this->jobExecutor);
     $this->repositoryService = $this->processEngine->getRepositoryService();
     $this->runtimeService = $this->processEngine->getRuntimeService();
     $this->taskService = $this->processEngine->getTaskService();
     $this->historyService = $this->processEngine->getHistoryService();
     $this->managementService = $this->processEngine->getManagementService();
     if ($this->typeInfo === NULL) {
         $this->typeInfo = new ReflectionTypeInfo(new \ReflectionClass(get_class($this)));
     }
     foreach ($this->typeInfo->getMethods() as $method) {
         if (!$method->isPublic() || $method->isStatic()) {
             continue;
         }
         foreach ($method->getAnnotations() as $anno) {
             if ($anno instanceof MessageHandler) {
                 $this->messageHandlers[$anno->processKey][$anno->value] = [$this, $method->getName()];
             }
             if ($anno instanceof ServiceTaskHandler) {
                 $this->serviceTaskHandlers[$anno->processKey][$anno->value] = [$this, $method->getName()];
             }
         }
     }
 }