-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #514 from patchlevel/trace
projector accessor & experimental trace feature
- Loading branch information
Showing
27 changed files
with
1,052 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Projection\Projector; | ||
|
||
use Closure; | ||
use Patchlevel\EventSourcing\Attribute\Subscribe; | ||
use Patchlevel\EventSourcing\EventBus\Message; | ||
use Patchlevel\EventSourcing\Metadata\Projector\ProjectorMetadata; | ||
use Patchlevel\EventSourcing\Projection\Projection\RunMode; | ||
|
||
use function array_key_exists; | ||
use function array_map; | ||
use function array_merge; | ||
|
||
final class MetadataProjectorAccessor implements ProjectorAccessor | ||
{ | ||
/** @var array<class-string, list<Closure(Message):void>> */ | ||
private array $subscribeCache = []; | ||
|
||
public function __construct( | ||
private readonly object $projector, | ||
private readonly ProjectorMetadata $metadata, | ||
) { | ||
} | ||
|
||
public function id(): string | ||
{ | ||
return $this->metadata->id; | ||
} | ||
|
||
public function group(): string | ||
{ | ||
return $this->metadata->group; | ||
} | ||
|
||
public function runMode(): RunMode | ||
{ | ||
return $this->metadata->runMode; | ||
} | ||
|
||
public function setupMethod(): Closure|null | ||
{ | ||
$method = $this->metadata->setupMethod; | ||
|
||
if ($method === null) { | ||
return null; | ||
} | ||
|
||
return $this->projector->$method(...); | ||
} | ||
|
||
public function teardownMethod(): Closure|null | ||
{ | ||
$method = $this->metadata->teardownMethod; | ||
|
||
if ($method === null) { | ||
return null; | ||
} | ||
|
||
return $this->projector->$method(...); | ||
} | ||
|
||
/** | ||
* @param class-string $eventClass | ||
* | ||
* @return list<Closure(Message):void> | ||
*/ | ||
public function subscribeMethods(string $eventClass): array | ||
{ | ||
if (array_key_exists($eventClass, $this->subscribeCache)) { | ||
return $this->subscribeCache[$eventClass]; | ||
} | ||
|
||
$methods = array_merge( | ||
$this->metadata->subscribeMethods[$eventClass] ?? [], | ||
$this->metadata->subscribeMethods[Subscribe::ALL] ?? [], | ||
); | ||
|
||
$this->subscribeCache[$eventClass] = array_map( | ||
/** @return Closure(Message):void */ | ||
fn (string $method) => $this->projector->$method(...), | ||
$methods, | ||
); | ||
|
||
return $this->subscribeCache[$eventClass]; | ||
} | ||
} |
Oops, something went wrong.