-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AttributeReader helper and TaskQueue attribute
- Loading branch information
Showing
11 changed files
with
248 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class TaskQueue | ||
{ | ||
/** | ||
* @param non-empty-string $name Task queue name. | ||
*/ | ||
public function __construct( | ||
public readonly string $name, | ||
) { | ||
} | ||
} |
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,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Internal; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class AttributeReader | ||
{ | ||
/** | ||
* @param class-string $class | ||
* @param list<class-string> $attributes | ||
* | ||
* @return array<class-string, list<object>> | ||
*/ | ||
public static function fromClass( | ||
string $class, | ||
array $attributes, | ||
bool $merge = true, | ||
bool $inheritance = true, | ||
bool $interfaces = true, | ||
): array { | ||
$reflection = new \ReflectionClass($class); | ||
if ($reflection->isInternal()) { | ||
return []; | ||
} | ||
|
||
/** @var array<class-string, array<class-string, list<object>>> $cache */ | ||
static $cache = []; | ||
|
||
$result = $cache[$class] ?? self::initAttributes($reflection, $attributes); | ||
|
||
if (!$inheritance) { | ||
return $result; | ||
} | ||
|
||
if ($parent = $reflection->getParentClass()) { | ||
$attrs = self::fromClass($parent->getName(), $attributes, $merge, true, false); | ||
$result = $merge | ||
? \array_merge_recursive($result, $attrs) | ||
: $result + $attrs; | ||
} | ||
|
||
if (!$interfaces) { | ||
return $result; | ||
} | ||
|
||
foreach (self::sortInterfaces($reflection) as $interface) { | ||
$attrs = self::fromClass($interface, $attributes, $merge, false, false); | ||
$result = $merge | ||
? \array_merge_recursive($result, $attrs) | ||
: $result + $attrs; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param \ReflectionClass $reflection | ||
* @param array<class-string> $filter | ||
* @return array<class-string, list<object>> | ||
*/ | ||
private static function initAttributes(\ReflectionClass $reflection, array $filter): array | ||
{ | ||
$result = []; | ||
foreach ($reflection->getAttributes() as $attribute) { | ||
$instance = $attribute->newInstance(); | ||
foreach ($filter as $attributeClass) { | ||
if ($instance instanceof $attributeClass) { | ||
$result[$attributeClass][] = $instance; | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
private static function sortInterfaces(\ReflectionClass $class): array | ||
{ | ||
$result = []; | ||
foreach ($class->getInterfaces() as $reflection) { | ||
$result[$reflection->getName()] = \count($reflection->getInterfaces()); | ||
} | ||
|
||
\arsort($result); | ||
|
||
return \array_keys($result); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Tests\Stub\Attributed; | ||
|
||
use Temporal\Sugar\Attribute\TaskQueue; | ||
|
||
#[TaskQueue(name: 'test-queue-abstract')] | ||
abstract class AbstractAttributed implements InterfaceAttributed | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Tests\Stub\Attributed; | ||
|
||
use Temporal\Sugar\Attribute\TaskQueue; | ||
|
||
#[TaskQueue(name: 'test-queue-extended')] | ||
final class ExtendedAttributed extends AbstractAttributed | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Tests\Stub\Attributed; | ||
|
||
use Temporal\Sugar\Attribute\TaskQueue; | ||
|
||
#[TaskQueue(name: 'test-queue-interface')] | ||
interface InterfaceAttributed extends ParentInterfaceAttributed | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Tests\Stub\Attributed; | ||
|
||
use Temporal\Sugar\Attribute\TaskQueue; | ||
|
||
#[TaskQueue(name: 'test-queue-parent-interface')] | ||
interface ParentInterfaceAttributed extends ParentParentInterfaceAttributed | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Tests\Stub\Attributed; | ||
|
||
use Temporal\Sugar\Attribute\TaskQueue; | ||
|
||
#[TaskQueue(name: 'test-queue-parent-parent-interface')] | ||
interface ParentParentInterfaceAttributed | ||
{ | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Tests\Stub\Attributed; | ||
|
||
use Temporal\Sugar\Attribute\TaskQueue; | ||
|
||
#[TaskQueue(name: 'test-queue')] | ||
final class SimpleClass | ||
{ | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Temporal\Sugar\Tests\Unit\Internal; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Temporal\Sugar\Attribute\TaskQueue; | ||
use Temporal\Sugar\Internal\AttributeReader; | ||
|
||
class AttributeReaderTest extends TestCase | ||
{ | ||
public function testFromClass(): void | ||
{ | ||
$result = AttributeReader::fromClass( | ||
\Temporal\Sugar\Tests\Stub\Attributed\SimpleClass::class, | ||
[TaskQueue::class] | ||
); | ||
|
||
$this->assertArrayHasKey(TaskQueue::class, $result); | ||
$this->assertIsArray($result[TaskQueue::class]); | ||
$this->assertCount(1, $result[TaskQueue::class]); | ||
$this->assertInstanceOf(TaskQueue::class, $result[TaskQueue::class][0]); | ||
$this->assertEquals('test-queue', $result[TaskQueue::class][0]->name); | ||
} | ||
|
||
public function testFromExtendedClassWithInheritanceWithMerge(): void | ||
{ | ||
$result = AttributeReader::fromClass( | ||
\Temporal\Sugar\Tests\Stub\Attributed\ExtendedAttributed::class, | ||
[TaskQueue::class], | ||
merge: true, | ||
); | ||
|
||
$this->assertArrayHasKey(TaskQueue::class, $result); | ||
$this->assertIsArray($result[TaskQueue::class]); | ||
$this->assertCount(5, $result[TaskQueue::class]); | ||
$this->assertInstanceOf(TaskQueue::class, $result[TaskQueue::class][0]); | ||
$this->assertInstanceOf(TaskQueue::class, $result[TaskQueue::class][1]); | ||
$this->assertInstanceOf(TaskQueue::class, $result[TaskQueue::class][3]); | ||
$this->assertEquals('test-queue-extended', $result[TaskQueue::class][0]->name); | ||
$this->assertEquals('test-queue-abstract', $result[TaskQueue::class][1]->name); | ||
$this->assertEquals('test-queue-interface', $result[TaskQueue::class][2]->name); | ||
$this->assertEquals('test-queue-parent-interface', $result[TaskQueue::class][3]->name); | ||
$this->assertEquals('test-queue-parent-parent-interface', $result[TaskQueue::class][4]->name); | ||
} | ||
|
||
public function testFromInterfaceWithInheritance(): void | ||
{ | ||
$result = AttributeReader::fromClass( | ||
\Temporal\Sugar\Tests\Stub\Attributed\InterfaceAttributed::class, | ||
[TaskQueue::class], | ||
merge: true, | ||
); | ||
|
||
$this->assertArrayHasKey(TaskQueue::class, $result); | ||
$this->assertIsArray($result[TaskQueue::class]); | ||
$this->assertCount(3, $result[TaskQueue::class]); | ||
$this->assertInstanceOf(TaskQueue::class, $result[TaskQueue::class][0]); | ||
$this->assertInstanceOf(TaskQueue::class, $result[TaskQueue::class][1]); | ||
$this->assertInstanceOf(TaskQueue::class, $result[TaskQueue::class][2]); | ||
$this->assertEquals('test-queue-interface', $result[TaskQueue::class][0]->name); | ||
$this->assertEquals('test-queue-parent-interface', $result[TaskQueue::class][1]->name); | ||
$this->assertEquals('test-queue-parent-parent-interface', $result[TaskQueue::class][2]->name); | ||
} | ||
} |