44
55namespace PHPWorkflow \State \ExecutionLog ;
66
7+ use PHPWorkflow \State \ExecutionLog \OutputFormat \OutputFormat ;
78use PHPWorkflow \State \WorkflowState ;
89use PHPWorkflow \Step \WorkflowStep ;
910
@@ -15,7 +16,7 @@ class ExecutionLog
1516
1617 /** @var Step[][] */
1718 private array $ stages = [];
18- /** @var string [] Collect additional debug info concerning the current step */
19+ /** @var StepInfo [] Collect additional debug info concerning the current step */
1920 private array $ stepInfo = [];
2021 /** @var string[][] Collect all warnings which occurred during the workflow execution */
2122 private array $ warnings = [];
@@ -30,32 +31,22 @@ public function __construct(WorkflowState $workflowState)
3031 $ this ->workflowState = $ workflowState ;
3132 }
3233
33- public function addStep (int $ stage , string $ step , string $ state , ?string $ reason ): void {
34+ public function addStep (int $ stage , Describable $ step , string $ state , ?string $ reason ): void {
3435 $ stage = $ this ->mapStage ($ stage );
3536
3637 $ this ->stages [$ stage ][] = new Step ($ step , $ state , $ reason , $ this ->stepInfo , $ this ->warningsDuringStep );
3738 $ this ->stepInfo = [];
3839 $ this ->warningsDuringStep = 0 ;
3940 }
4041
41- public function __toString (): string
42+ public function debug ( OutputFormat $ formatter )
4243 {
43- $ debug = "Process log for workflow ' {$ this ->workflowState ->getWorkflowName ()}': \n" ;
44-
45- foreach ($ this ->stages as $ stage => $ steps ) {
46- $ debug .= "$ stage: \n" ;
47-
48- foreach ($ steps as $ step ) {
49- $ debug .= ' - ' . $ step . "\n" ;
50- }
51- }
52-
53- return trim ($ debug );
44+ return $ formatter ->format ($ this ->workflowState ->getWorkflowName (), $ this ->stages );
5445 }
5546
56- public function attachStepInfo (string $ info ): void
47+ public function attachStepInfo (string $ info, array $ context = [] ): void
5748 {
58- $ this ->stepInfo [] = $ info ;
49+ $ this ->stepInfo [] = new StepInfo ( $ info, $ context ) ;
5950 }
6051
6152 public function addWarning (string $ message , bool $ workflowReportWarning = false ): void
@@ -74,11 +65,11 @@ public function startExecution(): void
7465
7566 public function stopExecution (): void
7667 {
77- $ this ->attachStepInfo (" Execution time: " . number_format (1000 * (microtime (true ) - $ this ->startAt ), 5 ) . 'ms ' );
68+ $ this ->attachStepInfo (' Execution time: ' . number_format (1000 * (microtime (true ) - $ this ->startAt ), 5 ) . 'ms ' );
7869
7970 if ($ this ->warnings ) {
8071 $ warnings = sprintf (
81- " Got %s warning%s during the execution: " ,
72+ ' Got %s warning%s during the execution: ' ,
8273 $ amount = count ($ this ->warnings , COUNT_RECURSIVE ) - count ($ this ->warnings ),
8374 $ amount > 1 ? 's ' : '' ,
8475 );
@@ -87,7 +78,7 @@ public function stopExecution(): void
8778 $ warnings .= implode (
8879 '' ,
8980 array_map (
90- fn (string $ warning ): string => sprintf ("\n %s: %s" , $ stage , $ warning ),
81+ fn (string $ warning ): string => sprintf (PHP_EOL . ' %s: %s ' , $ stage , $ warning ),
9182 $ stageWarnings ,
9283 ),
9384 );
@@ -107,7 +98,7 @@ private function mapStage(int $stage): string
10798 case WorkflowState::STAGE_ON_ERROR : return 'On Error ' ;
10899 case WorkflowState::STAGE_ON_SUCCESS : return 'On Success ' ;
109100 case WorkflowState::STAGE_AFTER : return 'After ' ;
110- case WorkflowState::STAGE_SUMMARY : return "\n Summary " ;
101+ case WorkflowState::STAGE_SUMMARY : return PHP_EOL . ' Summary ' ;
111102 }
112103 }
113104
0 commit comments