Skip to content

Commit

Permalink
Deprecate DeclarationLocatorInterface; add DeclarationRegistryInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 18, 2024
1 parent df2d14e commit 6c148a8
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Bootloader/TemporalBridgeBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Spiral\TemporalBridge\Config\TemporalConfig;
use Spiral\TemporalBridge\DeclarationLocator;
use Spiral\TemporalBridge\DeclarationLocatorInterface;
use Spiral\TemporalBridge\DeclarationRegistryInterface;
use Spiral\TemporalBridge\Dispatcher;
use Spiral\TemporalBridge\WorkerFactory;
use Spiral\TemporalBridge\WorkerFactoryInterface;
Expand Down Expand Up @@ -63,8 +64,9 @@ public function defineSingletons(): array
rpc: Goridge::create(),
),
WorkerFactoryInterface::class => WorkerFactory::class,
DeclarationLocatorInterface::class => DeclarationRegistryInterface::class,

DeclarationLocatorInterface::class => static fn() => new DeclarationLocator(
DeclarationRegistryInterface::class => static fn() => new DeclarationLocator(
reader: new AttributeReader(),
),

Expand Down
7 changes: 6 additions & 1 deletion src/DeclarationLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#[Singleton]
#[TargetAttribute(WorkflowInterface::class)]
#[TargetAttribute(ActivityInterface::class)]
final class DeclarationLocator implements DeclarationLocatorInterface, TokenizationListenerInterface
final class DeclarationLocator implements DeclarationRegistryInterface, TokenizationListenerInterface
{
private array $declarations = [];

Expand All @@ -23,6 +23,11 @@ public function __construct(
) {
}

public function addDeclaration(\ReflectionClass|string $class): void
{
$this->listen($class instanceof \ReflectionClass ? $class : new \ReflectionClass($class));
}

public function getDeclarations(): iterable
{
foreach ($this->declarations as $type => $classes) {
Expand Down
3 changes: 3 additions & 0 deletions src/DeclarationLocatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Temporal\Activity\ActivityInterface;
use Temporal\Workflow\WorkflowInterface;

/**
* @deprecated Use {@see DeclarationRegistryInterface} instead.
*/
interface DeclarationLocatorInterface
{
/**
Expand Down
15 changes: 15 additions & 0 deletions src/DeclarationRegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Spiral\TemporalBridge;

interface DeclarationRegistryInterface extends DeclarationLocatorInterface
{
/**
* Add a new declaration to the registry.
*
* @param \ReflectionClass|class-string $class Workflow or activity class name or reflection.
*/
public function addDeclaration(\ReflectionClass|string $class): void;
}
77 changes: 74 additions & 3 deletions tests/src/DeclarationLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@

namespace Spiral\TemporalBridge\Tests;

use Mockery as m;
use Spiral\Attributes\AttributeReader;
use Spiral\TemporalBridge\DeclarationLocator;
use Spiral\Tokenizer\ClassesInterface;
use Temporal\Activity\ActivityInterface;
use Temporal\Workflow\WorkflowInterface;

final class DeclarationLocatorTest extends TestCase
{
private DeclarationLocator $locator;
private m\LegacyMockInterface|m\MockInterface $classes;

protected function setUp(): void
{
Expand Down Expand Up @@ -68,6 +65,80 @@ public function testWorkflowsShouldBeRegistered(): void
$this->assertSame(ActivityInterface::class, $result[3][0]);
$this->assertSame($activity2, $result[3][1]);
}

public function testAddDeclarationSkipsNonClasses(): void
{
$this->locator->addDeclaration(TestEnum::class);
$this->locator->addDeclaration(TestAbstractClass::class);
$this->locator->addDeclaration(TestInterface::class);
$this->locator->addDeclaration(new \ReflectionClass(TestEnum::class));
$this->locator->addDeclaration(new \ReflectionClass(TestAbstractClass::class));
$this->locator->addDeclaration(new \ReflectionClass(TestInterface::class));

$result = [];

foreach ($this->locator->getDeclarations() as $type => $class) {
$result[] = [$type, $class];
}

$this->assertCount(0, $result);
}

public function testAddDeclarationReflections(): void
{
$this->locator->addDeclaration($workflow1 = new \ReflectionClass(TestWorkflowClass::class));
$this->locator->addDeclaration($workflow2 = new \ReflectionClass(TestWorkflowClassWithInterface::class));
$this->locator->addDeclaration($activity1 = new \ReflectionClass(TestActivityClass::class));
$this->locator->addDeclaration($activity2 = new \ReflectionClass(TestActivityClassWithInterface::class));

$result = [];

foreach ($this->locator->getDeclarations() as $type => $class) {
$result[] = [$type, $class];
}

$this->assertCount(4, $result);

$this->assertSame(WorkflowInterface::class, $result[0][0]);
$this->assertSame($workflow1, $result[0][1]);

$this->assertSame(WorkflowInterface::class, $result[1][0]);
$this->assertSame($workflow2, $result[1][1]);

$this->assertSame(ActivityInterface::class, $result[2][0]);
$this->assertSame($activity1, $result[2][1]);

$this->assertSame(ActivityInterface::class, $result[3][0]);
$this->assertSame($activity2, $result[3][1]);
}

public function testAddDeclarationClassNames(): void
{
$this->locator->addDeclaration(TestWorkflowClass::class);
$this->locator->addDeclaration(TestWorkflowClassWithInterface::class);
$this->locator->addDeclaration(TestActivityClass::class);
$this->locator->addDeclaration(TestActivityClassWithInterface::class);

$result = [];

foreach ($this->locator->getDeclarations() as $type => $class) {
$result[] = [$type, $class];
}

$this->assertCount(4, $result);

$this->assertSame(WorkflowInterface::class, $result[0][0]);
$this->assertSame(TestWorkflowClass::class, $result[0][1]->getName());

$this->assertSame(WorkflowInterface::class, $result[1][0]);
$this->assertSame(TestWorkflowClassWithInterface::class, $result[1][1]->getName());

$this->assertSame(ActivityInterface::class, $result[2][0]);
$this->assertSame(TestActivityClass::class, $result[2][1]->getName());

$this->assertSame(ActivityInterface::class, $result[3][0]);
$this->assertSame(TestActivityClassWithInterface::class, $result[3][1]->getName());
}
}

enum TestEnum
Expand Down

0 comments on commit 6c148a8

Please sign in to comment.