Skip to content

Commit

Permalink
Feat: Default context (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek authored Nov 17, 2023
1 parent d000b47 commit a926da9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
16 changes: 16 additions & 0 deletions doc/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ If you use callables, it will be injected as well.
The `ETLState` object is also injected into all events.
Most of its properties are read-only, except `context`.

> [!TIP]
> Your executor can provide a default context (see example below).
```php
it('accepts a default context', function () {
// Given
$executor = (new EtlExecutor())->withContext(['foo' => 'bar']);

// When
$report = $executor->process([], context: ['bar' => 'baz']);

// Then
expect($report->context)->toBe(['foo' => 'bar', 'bar' => 'baz']);
});
```

Skipping items
--------------

Expand Down
11 changes: 10 additions & 1 deletion src/EtlExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ final class EtlExecutor implements EventDispatcherInterface

private EventDispatcher $eventDispatcher;

/**
* @param array<string, mixed> $context
*/
public function __construct(
public readonly ExtractorInterface $extractor = new IterableExtractor(),
public readonly TransformerInterface $transformer = new NullTransformer(),
public readonly LoaderInterface $loader = new InMemoryLoader(),
public readonly EtlConfiguration $options = new EtlConfiguration(),
public readonly ProcessorInterface $processor = new IterableProcessor(),
public readonly array $context = [],
) {
$this->listenerProvider = new PrioritizedListenerProvider();
$this->eventDispatcher = new EventDispatcher($this->listenerProvider);
Expand All @@ -74,7 +78,12 @@ public function __construct(
*/
public function process(mixed $source = null, mixed $destination = null, array $context = []): EtlState
{
$state = new EtlState(options: $this->options, source: $source, destination: $destination, context: $context);
$state = new EtlState(
options: $this->options,
source: $source,
destination: $destination,
context: [...$this->context, ...$context]
);

try {
$this->emit(InitEvent::class, $state);
Expand Down
8 changes: 8 additions & 0 deletions src/Internal/EtlBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,12 @@ public function withProcessor(ProcessorInterface $processor): self
{
return $this->cloneWith(['processor' => $processor]);
}

/**
* @param array<string, mixed> $context
*/
public function withContext(array $context): self
{
return $this->cloneWith(['context' => $context]);
}
}
11 changes: 11 additions & 0 deletions tests/Unit/EtlExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,14 @@ public function process(EtlExecutor $executor, EtlState $state, mixed $extracted
// When
$executor->process([]);
})->throws(ExtractException::class);

it('accepts a default context', function () {
// Given
$executor = (new EtlExecutor())->withContext(['foo' => 'bar']);

// When
$report = $executor->process([], context: ['bar' => 'baz']);

// Then
expect($report->context)->toBe(['foo' => 'bar', 'bar' => 'baz']);
});

0 comments on commit a926da9

Please sign in to comment.