Skip to content

Commit 7fb08c7

Browse files
committed
Added actions state management
1 parent 830d3f5 commit 7fb08c7

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/StateOutput/Action.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kiboko\Component\State\StateOutput;
4+
5+
use Symfony\Component\Console\Output\ConsoleOutput;
6+
use Symfony\Component\Console\Output\ConsoleSectionOutput;
7+
8+
final class Action
9+
{
10+
/** @var array<string, callable> */
11+
private array $metrics = [];
12+
private ConsoleSectionOutput $section;
13+
14+
public function __construct(
15+
private ConsoleOutput $output,
16+
string $index,
17+
string $label,
18+
)
19+
{
20+
$this->section = $this->output->section();
21+
$this->section->writeln('');
22+
$this->section->writeln(sprintf('<fg=green> % 2s. %-50s</>', $index, $label));
23+
}
24+
25+
public function addMetric(string $label, callable $callback): self
26+
{
27+
$this->metrics[$label] = $callback;
28+
29+
return $this;
30+
}
31+
32+
public function update(): void
33+
{
34+
$this->section
35+
->writeln(' ' . implode(', ', array_map(
36+
fn(string $label, callable $callback) => sprintf('%s <fg=cyan>%d</>', $label, ($callback)()),
37+
array_keys($this->metrics),
38+
array_values($this->metrics),
39+
)));
40+
}
41+
}

src/StateOutput/Workflow.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
final class Workflow
1010
{
11-
/** @var list<Pipeline> */
12-
private array $pipelines = [];
11+
/** @var list<Pipeline|Action> */
12+
private array $jobs = [];
1313
private string $index = 'A';
1414

1515
public function __construct(
@@ -19,13 +19,18 @@ public function __construct(
1919

2020
public function withPipeline(string $label): Pipeline
2121
{
22-
return $this->pipelines[] = new Pipeline($this->output, $this->index++, $label);
22+
return $this->jobs[] = new Pipeline($this->output, $this->index++, $label);
23+
}
24+
25+
public function withAction(string $label): Action
26+
{
27+
return $this->jobs[] = new Action($this->output, $this->index++, $label);
2328
}
2429

2530
public function update(): void
2631
{
27-
foreach ($this->pipelines as $pipeline) {
28-
$pipeline->update();
32+
foreach ($this->jobs as $job) {
33+
$job->update();
2934
}
3035
}
3136
}

0 commit comments

Comments
 (0)