2323use Temporal \Exception \Failure \CanceledFailure ;
2424use Temporal \Exception \OutOfContextException ;
2525use Temporal \Internal \Support \Facade ;
26- use Temporal \Internal \Workflow \ScopeContext ;
2726use Temporal \Workflow \ActivityStubInterface ;
2827use Temporal \Workflow \CancellationScopeInterface ;
2928use Temporal \Workflow \ChildWorkflowOptions ;
4544 *
4645 * This is main class you can use in your workflow code.
4746 *
48- * @method static ScopeContext getCurrentContext() Get current workflow context.
49- *
5047 * @psalm-import-type TypeEnum from Type
5148 * @psalm-import-type DateIntervalValue from DateInterval
5249 * @see DateInterval
53- *
54- * @template-extends Facade<ScopedContextInterface>
5550 */
5651final class Workflow extends Facade
5752{
5853 public const DEFAULT_VERSION = -1 ;
5954
55+ /**
56+ * Get the current Workflow context.
57+ * @throws OutOfContextException
58+ */
59+ public static function getCurrentContext (): WorkflowContextInterface
60+ {
61+ $ ctx = parent ::getCurrentContext ();
62+ $ ctx instanceof WorkflowContextInterface or throw new OutOfContextException (
63+ 'The Workflow facade can be used only inside workflow code. ' ,
64+ );
65+ return $ ctx ;
66+ }
67+
6068 /**
6169 * Returns current datetime.
6270 *
@@ -192,7 +200,9 @@ public static function getInput(): ValuesInterface
192200 */
193201 public static function async (callable $ task ): CancellationScopeInterface
194202 {
195- return self ::getCurrentContext ()->async ($ task );
203+ $ ctx = self ::getCurrentContext ();
204+ \assert ($ ctx instanceof ScopedContextInterface);
205+ return $ ctx ->async ($ task );
196206 }
197207
198208 /**
@@ -244,7 +254,9 @@ public static function async(callable $task): CancellationScopeInterface
244254 */
245255 public static function asyncDetached (callable $ task ): CancellationScopeInterface
246256 {
247- return self ::getCurrentContext ()->asyncDetached ($ task );
257+ $ ctx = self ::getCurrentContext ();
258+ \assert ($ ctx instanceof ScopedContextInterface);
259+ return $ ctx ->asyncDetached ($ task );
248260 }
249261
250262 /**
@@ -352,7 +364,9 @@ public static function registerQuery(
352364 callable $ handler ,
353365 string $ description = '' ,
354366 ): ScopedContextInterface {
355- return self ::getCurrentContext ()->registerQuery ($ queryType , $ handler , $ description );
367+ $ ctx = self ::getCurrentContext ();
368+ \assert ($ ctx instanceof ScopedContextInterface);
369+ return $ ctx ->registerQuery ($ queryType , $ handler , $ description );
356370 }
357371
358372 /**
@@ -372,7 +386,9 @@ public static function registerQuery(
372386 */
373387 public static function registerSignal (string $ name , callable $ handler , string $ description = '' ): ScopedContextInterface
374388 {
375- return self ::getCurrentContext ()->registerSignal ($ name , $ handler , $ description );
389+ $ ctx = self ::getCurrentContext ();
390+ \assert ($ ctx instanceof ScopedContextInterface);
391+ return $ ctx ->registerSignal ($ name , $ handler , $ description );
376392 }
377393
378394 /**
@@ -491,7 +507,9 @@ public static function registerUpdate(
491507 ?callable $ validator = null ,
492508 string $ description = '' ,
493509 ): ScopedContextInterface {
494- return self ::getCurrentContext ()->registerUpdate ($ name , $ handler , $ validator , $ description );
510+ $ ctx = self ::getCurrentContext ();
511+ \assert ($ ctx instanceof ScopedContextInterface);
512+ return $ ctx ->registerUpdate ($ name , $ handler , $ validator , $ description );
495513 }
496514
497515 /**
@@ -995,7 +1013,6 @@ public static function getStackTrace(): string
9951013 */
9961014 public static function allHandlersFinished (): bool
9971015 {
998- /** @var ScopedContextInterface $context */
9991016 $ context = self ::getCurrentContext ();
10001017
10011018 return $ context ->allHandlersFinished ();
@@ -1081,7 +1098,6 @@ public static function upsertTypedSearchAttributes(SearchAttributeUpdate ...$upd
10811098 */
10821099 public static function uuid (): PromiseInterface
10831100 {
1084- /** @var ScopedContextInterface $context */
10851101 $ context = self ::getCurrentContext ();
10861102
10871103 return $ context ->uuid ();
@@ -1094,7 +1110,6 @@ public static function uuid(): PromiseInterface
10941110 */
10951111 public static function uuid4 (): PromiseInterface
10961112 {
1097- /** @var ScopedContextInterface $context */
10981113 $ context = self ::getCurrentContext ();
10991114
11001115 return $ context ->uuid4 ();
@@ -1111,7 +1126,6 @@ public static function uuid4(): PromiseInterface
11111126 */
11121127 public static function uuid7 (?\DateTimeInterface $ dateTime = null ): PromiseInterface
11131128 {
1114- /** @var ScopedContextInterface $context */
11151129 $ context = self ::getCurrentContext ();
11161130
11171131 return $ context ->uuid7 ($ dateTime );
0 commit comments