gate = $gate; $this->logs = $logs; $this->compiler = $compiler; } /** * @param PipelineStepInterface[] $steps */ public function run( array $steps, SubmissionContext $ctx, ?array $schema = null ): PipelineResult { $events = array(); try { // Evaluate conditional logic before executing steps if ( $schema && $this->compiler ) { $this->compiler->evaluateConditions( $schema, $ctx ); } $logic = new ConditionalLogic(); $logicValues = $ctx->getMeta( 'logic_payload' ); $logicValues = is_array( $logicValues ) ? $logicValues : $ctx->payload; foreach ( $steps as $step ) { $ts = time(); // "Only when" conditions on an action (an add-on feature): an // email or webhook whose conditions are not met is skipped. // Saving always runs. Without an add-on the conditions are // ignored and the action runs. $stepSettings = $step->settings(); if ( $step->action()->id() !== 'save' && is_array( $stepSettings ) && ConditionalLogic::hasLogic( $stepSettings['logic'] ?? null, 'action' ) && ! $logic->logicPasses( $stepSettings['logic'], $logicValues, array(), 'action' ) ) { $this->logEvent( $ctx, $step, 'skipped', 'conditions_not_met', $ts ); continue; } // Log start $this->logEvent( $ctx, $step, 'started', null, $ts ); // Expose current step id/settings in context for action implementations $ctx->setMeta( 'current_step_id', $step->id() ); $ctx->setMeta( 'current_step_settings', $step->settings() ); try { $step->action()->handle( $ctx ); } catch ( Throwable $e ) { // Hard failure: log and rethrow to be handled by outer catch $this->logEvent( $ctx, $step, 'failed', $e->getMessage(), time() ); throw $e; } // Check for soft failures recorded by action implementations $fails = $ctx->getMeta( 'action_failures', array() ); $found = false; if ( is_array( $fails ) ) { $remaining = array(); foreach ( $fails as $f ) { if ( isset( $f['action'] ) && $f['action'] === $step->action()->id() ) { $found = true; $reason = $f['reason'] ?? 'failure'; $this->logEvent( $ctx, $step, 'failed', (string) ( $f['response'] ?? $reason ), time() ); // don't add to remaining } else { $remaining[] = $f; } } // update remaining failures back into context to avoid duplicate logging $ctx->setMeta( 'action_failures', $remaining ); } if ( ! $found ) { $this->logEvent( $ctx, $step, 'succeeded', null, time() ); } // Clear step-specific transient meta $ctx->setMeta( 'current_step_id', null ); $ctx->setMeta( 'current_step_settings', null ); } return new PipelineResult( true, $events, null ); } catch ( Throwable $e ) { $events[] = array( 'type' => 'pipeline.error', 'message' => $e->getMessage(), 'ts' => time(), ); return new PipelineResult( false, $events, $e->getMessage() ); } } private function logEvent( SubmissionContext $ctx, PipelineStepInterface $step, string $status, ?string $error, int $ts ): void { $submissionId = $ctx->getMeta( 'submission_id' ); if ( empty( $submissionId ) || ! is_int( $submissionId ) ) { return; // nothing to persist } $schemaVersion = $ctx->getMeta( 'schema_version' ); // Capture action-specific output data set by the action's handle() method. $stepData = $ctx->getMeta( 'step_output_data', array() ); if ( ! is_array( $stepData ) ) { $stepData = array(); } // Clear after reading so it doesn't bleed into the next step. $ctx->setMeta( 'step_output_data', null ); $evt = new PipelineEvent( $submissionId, is_int( $schemaVersion ) ? $schemaVersion : null, $step->id(), $step->action()->id(), $status, $error, $ts, $stepData ); $level = $status === 'succeeded' || $status === 'started' || $status === 'skipped' ? 'info' : 'error'; $this->logs->create( array( 'submission_id' => $submissionId, 'level' => $level, 'message' => sprintf( 'Step %s: %s', $step->id(), $status ), 'context' => $evt->toArray(), ) ); } }