Skip to content

Commit 316a47e

Browse files
committed
Use AttributeCollection instead of arrays
1 parent 0034c15 commit 316a47e

4 files changed

Lines changed: 41 additions & 22 deletions

File tree

src/Factory/ActivityStub.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Temporal\Internal\Workflow\ActivityProxy;
1010
use Temporal\Sugar\Attribute\RetryPolicy;
1111
use Temporal\Sugar\Attribute\TaskQueue;
12+
use Temporal\Sugar\Internal\Attribute\AttributeCollection;
1213
use Temporal\Sugar\Internal\Attribute\AttributeForActivity;
1314
use Temporal\Sugar\Internal\Attribute\AttributeReader;
1415
use Temporal\Sugar\Internal\RetryOptions;
@@ -82,12 +83,12 @@ public static function activity(
8283
retryMaxInterval: $retryMaxInterval,
8384
retryBackoff: $retryBackoff,
8485
nonRetryables: $nonRetryables,
85-
attribute: $attributes[RetryPolicy::class] ?? null,
86+
attribute: $attributes->first(RetryPolicy::class),
8687
);
8788

8889
$options = ActivityOptions::new()->withRetryOptions($retryOptions);
8990

90-
$taskQueue ??= isset($attributes[TaskQueue::class][0]) ? $attributes[TaskQueue::class][0]->name : null;
91+
$taskQueue ??= $attributes->first(TaskQueue::class)?->name;
9192
$taskQueue === null or $options = $options->withTaskQueue($taskQueue);
9293
// Timeouts
9394
$scheduleToStartTimeout === 0 or $options = $options->withScheduleToStartTimeout($scheduleToStartTimeout);
@@ -102,11 +103,10 @@ public static function activity(
102103
}
103104

104105
/**
105-
* @param class-string $class
106-
* @return array<class-string<AttributeForActivity>, AttributeForActivity>
106+
* @param class-string $class Activity class name.
107107
*/
108-
private static function readAttributes(string $class): array
108+
private static function readAttributes(string $class): AttributeCollection
109109
{
110-
return AttributeReader::fromClass($class, [AttributeForActivity::class]);
110+
return AttributeReader::collectionFromClass($class, [AttributeForActivity::class]);
111111
}
112112
}

src/Factory/WorkflowStub.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Temporal\Internal\Client\WorkflowProxy;
1212
use Temporal\Internal\Workflow\ChildWorkflowProxy;
1313
use Temporal\Sugar\Attribute\RetryPolicy;
14+
use Temporal\Sugar\Attribute\TaskQueue;
15+
use Temporal\Sugar\Internal\Attribute\AttributeCollection;
1416
use Temporal\Sugar\Internal\Attribute\AttributeForWorkflow;
1517
use Temporal\Sugar\Internal\Attribute\AttributeReader;
1618
use Temporal\Sugar\Internal\RetryOptions;
@@ -28,7 +30,7 @@ final class WorkflowStub
2830
*
2931
* @param class-string<T> $workflow
3032
* @param non-empty-string|null $taskQueue
31-
* @param int<0, max> $retryAttempts Maximum number of attempts. When exceeded the retries stop even
33+
* @param int<0, max>|null $retryAttempts Maximum number of attempts. When exceeded the retries stop even
3234
* if not expired yet. If not set or set to 0, it means unlimited, and rely on activity
3335
* {@see ActivityOptions::$scheduleToCloseTimeout} to stop.
3436
* @param DateInterval|string|int|null $retryInitInterval Backoff interval for the first retry.
@@ -77,7 +79,7 @@ public static function workflow(
7779
WorkflowClientInterface $workflowClient,
7880
string $workflow,
7981
?string $taskQueue = null,
80-
int $retryAttempts = 0,
82+
?int $retryAttempts = null,
8183
\DateInterval|string|int|null $retryInitInterval = null,
8284
\DateInterval|string|int|null $retryMaxInterval = null,
8385
?float $retryBackoff = null,
@@ -102,11 +104,11 @@ public static function workflow(
102104
retryMaxInterval: $retryMaxInterval,
103105
retryBackoff: $retryBackoff,
104106
nonRetryables: $nonRetryables,
105-
attribute: $attributes[RetryPolicy::class] ?? null,
107+
attribute: $attributes->first(RetryPolicy::class),
106108
);
107109

108110
$options = WorkflowOptions::new()->withRetryOptions($retryOptions);
109-
$taskQueue === null or $options = $options->withTaskQueue($taskQueue);
111+
$taskQueue ??= $attributes->first(TaskQueue::class)?->name;
110112
// Start options
111113
$startDelay === 0 or $options = $options->withWorkflowStartDelay($startDelay);
112114
$eagerStart and $options = $options->withEagerStart(true);
@@ -135,7 +137,7 @@ public static function workflow(
135137
* @param non-empty-string|null $taskQueue Task queue to use for workflow tasks. It should match a task queue
136138
* specified when creating a {@see Worker} that hosts the workflow code.
137139
* @param non-empty-string|null $namespace Specify namespace in which workflow should be started.
138-
* @param int<0, max> $retryAttempts Maximum number of attempts. When exceeded the retries stop even
140+
* @param int<0, max>|null $retryAttempts Maximum number of attempts. When exceeded the retries stop even
139141
* if not expired yet. If not set or set to 0, it means unlimited, and rely on activity
140142
* {@see ActivityOptions::$scheduleToCloseTimeout} to stop.
141143
* @param DateInterval|string|int|null $retryInitInterval Backoff interval for the first retry.
@@ -178,7 +180,7 @@ public static function childWorkflow(
178180
string $workflow,
179181
?string $taskQueue = null,
180182
?string $namespace = null,
181-
int $retryAttempts = 0,
183+
?int $retryAttempts = null,
182184
\DateInterval|string|int|null $retryInitInterval = null,
183185
\DateInterval|string|int|null $retryMaxInterval = null,
184186
?float $retryBackoff = null,
@@ -194,17 +196,21 @@ public static function childWorkflow(
194196
array $searchAttributes = [],
195197
array $memo = [],
196198
): object {
199+
$attributes = self::readAttributes($workflow);
200+
197201
// Retry options
198202
$retryOptions = RetryOptions::create(
199203
$retryAttempts,
200204
$retryInitInterval,
201205
$retryMaxInterval,
202206
$retryBackoff,
203207
$nonRetryables,
208+
attribute: $attributes->first(RetryPolicy::class),
204209
);
205210

206211
$options = Workflow\ChildWorkflowOptions::new()->withRetryOptions($retryOptions);
207212

213+
$taskQueue ??= $attributes->first(TaskQueue::class)?->name;
208214
$taskQueue === null or $options = $options->withTaskQueue($taskQueue);
209215
$namespace === null or $options = $options->withNamespace($namespace);
210216
// Start and close options
@@ -230,11 +236,10 @@ public static function childWorkflow(
230236
}
231237

232238
/**
233-
* @param class-string $class
234-
* @return array<class-string<AttributeForWorkflow>, AttributeForWorkflow>
239+
* @param class-string $class Workflow class name.
235240
*/
236-
private static function readAttributes(string $class): array
241+
private static function readAttributes(string $class): AttributeCollection
237242
{
238-
return AttributeReader::fromClass($class, [AttributeForWorkflow::class]);
243+
return AttributeReader::collectionFromClass($class, [AttributeForWorkflow::class]);
239244
}
240245
}

src/Internal/Attribute/AttributeReader.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,27 @@
99
*/
1010
final class AttributeReader
1111
{
12+
/**
13+
* @param class-string $class
14+
* @param list<class-string> $attributes
15+
*/
16+
public static function collectionFromClass(
17+
string $class,
18+
array $attributes,
19+
bool $merge = true,
20+
bool $inheritance = true,
21+
bool $interfaces = true,
22+
): AttributeCollection {
23+
return new AttributeCollection(self::arrayFromClass($class, $attributes, $merge, $inheritance, $interfaces));
24+
}
25+
1226
/**
1327
* @param class-string $class
1428
* @param list<class-string> $attributes
1529
*
1630
* @return array<class-string, list<object>>
1731
*/
18-
public static function fromClass(
32+
public static function arrayFromClass(
1933
string $class,
2034
array $attributes,
2135
bool $merge = true,
@@ -34,7 +48,7 @@ public static function fromClass(
3448
}
3549

3650
if ($parent = $reflection->getParentClass()) {
37-
$attrs = self::fromClass($parent->getName(), $attributes, $merge, true, false);
51+
$attrs = self::arrayFromClass($parent->getName(), $attributes, $merge, true, false);
3852
$result = $merge
3953
? \array_merge_recursive($result, $attrs)
4054
: $result + $attrs;
@@ -45,7 +59,7 @@ public static function fromClass(
4559
}
4660

4761
foreach (self::sortInterfaces($reflection) as $interface) {
48-
$attrs = self::fromClass($interface, $attributes, $merge, false, false);
62+
$attrs = self::arrayFromClass($interface, $attributes, $merge, false, false);
4963
$result = $merge
5064
? \array_merge_recursive($result, $attrs)
5165
: $result + $attrs;

tests/Unit/Internal/AttributeReaderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AttributeReaderTest extends TestCase
1212
{
1313
public function testFromClass(): void
1414
{
15-
$result = AttributeReader::fromClass(
15+
$result = AttributeReader::arrayFromClass(
1616
Stub\Attributed\SimpleClass::class,
1717
[TaskQueue::class]
1818
);
@@ -26,7 +26,7 @@ public function testFromClass(): void
2626

2727
public function testFromExtendedClassWithInheritanceWithMerge(): void
2828
{
29-
$result = AttributeReader::fromClass(
29+
$result = AttributeReader::arrayFromClass(
3030
Stub\Attributed\ExtendedAttributed::class,
3131
[TaskQueue::class],
3232
merge: true,
@@ -47,7 +47,7 @@ public function testFromExtendedClassWithInheritanceWithMerge(): void
4747

4848
public function testFromInterfaceWithInheritance(): void
4949
{
50-
$result = AttributeReader::fromClass(
50+
$result = AttributeReader::arrayFromClass(
5151
Stub\Attributed\InterfaceAttributed::class,
5252
[TaskQueue::class],
5353
merge: true,

0 commit comments

Comments
 (0)