77use DateInterval ;
88use Temporal \Client \WorkflowClientInterface ;
99use Temporal \Client \WorkflowOptions ;
10+ use Temporal \Client \WorkflowStubInterface ;
1011use Temporal \Common \IdReusePolicy ;
1112use Temporal \Internal \Client \WorkflowProxy ;
1213use Temporal \Internal \Workflow \ChildWorkflowProxy ;
1819use Temporal \Support \Internal \RetryOptions ;
1920use Temporal \Workflow ;
2021use Temporal \Workflow \ChildWorkflowCancellationType as ChildCancelType ;
22+ use Temporal \Workflow \ChildWorkflowStubInterface ;
2123use Temporal \Workflow \ParentClosePolicy ;
2224use Throwable ;
2325
@@ -28,7 +30,7 @@ final class WorkflowStub
2830 *
2931 * @template T of object
3032 *
31- * @param class-string<T> $workflow
33+ * @param class-string<T>|non-empty-string $type Workflow name or class name.
3234 * @param non-empty-string|null $taskQueue
3335 * @param int<0, max>|null $retryAttempts Maximum number of attempts. When exceeded the retries stop even
3436 * if not expired yet. If not set or set to 0, it means unlimited, and rely on activity
@@ -73,11 +75,11 @@ final class WorkflowStub
7375 * of list workflow.
7476 * @param list<mixed> $memo Specifies additional non-indexed information in result of list workflow.
7577 *
76- * @return T|WorkflowProxy
78+ * @return ($type is class-string ? T|WorkflowProxy : WorkflowStubInterface)
7779 */
7880 public static function workflow (
7981 WorkflowClientInterface $ workflowClient ,
80- string $ workflow ,
82+ string $ type ,
8183 ?string $ taskQueue = null ,
8284 ?int $ retryAttempts = null ,
8385 \DateInterval |string |int |null $ retryInitInterval = null ,
@@ -95,7 +97,8 @@ public static function workflow(
9597 array $ searchAttributes = [],
9698 array $ memo = [],
9799 ): object {
98- $ attributes = self ::readAttributes ($ workflow );
100+ $ isUntyped = !\class_exists ($ type ) && !\interface_exists ($ type );
101+ $ attributes = $ isUntyped ? new AttributeCollection ([]) : self ::readAttributes ($ type );
99102
100103 // Retry options
101104 $ retryOptions = RetryOptions::create (
@@ -109,6 +112,7 @@ public static function workflow(
109112
110113 $ options = WorkflowOptions::new ()->withRetryOptions ($ retryOptions );
111114 $ taskQueue ??= $ attributes ->first (TaskQueue::class)?->name;
115+ $ taskQueue === null or $ options = $ options ->withTaskQueue ($ taskQueue );
112116 // Start options
113117 $ startDelay === 0 or $ options = $ options ->withWorkflowStartDelay ($ startDelay );
114118 $ eagerStart and $ options = $ options ->withEagerStart (true );
@@ -125,15 +129,17 @@ public static function workflow(
125129 $ searchAttributes === [] or $ options = $ options ->withSearchAttributes ($ searchAttributes );
126130 $ memo === [] or $ options = $ options ->withMemo ($ memo );
127131
128- return $ workflowClient ->newWorkflowStub ($ workflow , $ options );
132+ return $ isUntyped
133+ ? $ workflowClient ->newUntypedWorkflowStub ($ type , $ options )
134+ : $ workflowClient ->newWorkflowStub ($ type , $ options );
129135 }
130136
131137 /**
132138 * Note: must be used in Workflow context only.
133139 *
134140 * @template T of object
135141 *
136- * @param class-string<T> $workflow
142+ * @param class-string<T>|non-empty-string $type Workflow name or class name.
137143 * @param non-empty-string|null $taskQueue Task queue to use for workflow tasks. It should match a task queue
138144 * specified when creating a {@see Worker} that hosts the workflow code.
139145 * @param non-empty-string|null $namespace Specify namespace in which workflow should be started.
@@ -174,10 +180,10 @@ public static function workflow(
174180 * of list workflow.
175181 * @param list<mixed> $memo Specifies additional non-indexed information in result of list workflow.
176182 *
177- * @return T|ChildWorkflowProxy
183+ * @return ($type is class-string ? T|ChildWorkflowProxy : ChildWorkflowStubInterface)
178184 */
179185 public static function childWorkflow (
180- string $ workflow ,
186+ string $ type ,
181187 ?string $ taskQueue = null ,
182188 ?string $ namespace = null ,
183189 ?int $ retryAttempts = null ,
@@ -196,7 +202,8 @@ public static function childWorkflow(
196202 array $ searchAttributes = [],
197203 array $ memo = [],
198204 ): object {
199- $ attributes = self ::readAttributes ($ workflow );
205+ $ isUntyped = !\class_exists ($ type ) && !\interface_exists ($ type );
206+ $ attributes = $ isUntyped ? new AttributeCollection ([]) : self ::readAttributes ($ type );
200207
201208 // Retry options
202209 $ retryOptions = RetryOptions::create (
@@ -232,7 +239,9 @@ public static function childWorkflow(
232239 $ searchAttributes === [] or $ options = $ options ->withSearchAttributes ($ searchAttributes );
233240 $ memo === [] or $ options = $ options ->withMemo ($ memo );
234241
235- return Workflow::newChildWorkflowStub ($ workflow , $ options );
242+ return $ isUntyped
243+ ? Workflow::newUntypedChildWorkflowStub ($ type , $ options )
244+ : Workflow::newChildWorkflowStub ($ type , $ options );
236245 }
237246
238247 /**
0 commit comments