-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: extract dispatch and event helper functions
- Loading branch information
Showing
7 changed files
with
83 additions
and
15 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
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SwooleTW\Hyperf\Bus; | ||
|
||
use Closure; | ||
use Hyperf\Context\ApplicationContext; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use SwooleTW\Hyperf\Bus\Contracts\Dispatcher; | ||
use SwooleTW\Hyperf\Queue\CallQueuedClosure; | ||
|
||
/** | ||
* Dispatch a job to its appropriate handler. | ||
* | ||
* @param mixed $job | ||
* @return ($job is Closure ? PendingClosureDispatch : PendingDispatch) | ||
*/ | ||
function dispatch($job): PendingClosureDispatch|PendingDispatch | ||
{ | ||
return $job instanceof Closure | ||
? new PendingClosureDispatch(CallQueuedClosure::create($job)) | ||
: new PendingDispatch($job); | ||
} | ||
|
||
/** | ||
* Dispatch a command to its appropriate handler in the current process. | ||
* | ||
* Queueable jobs will be dispatched to the "sync" queue. | ||
*/ | ||
function dispatch_sync(mixed $job, mixed $handler = null): mixed | ||
{ | ||
return ApplicationContext::getContainer() | ||
->get(Dispatcher::class) | ||
->dispatchSync($job, $handler); | ||
} | ||
|
||
/** | ||
* Dispatch an event and call the listeners. | ||
* | ||
* @template T of object | ||
* | ||
* @param T $event | ||
* | ||
* @return T | ||
*/ | ||
function event(object $event) | ||
{ | ||
return ApplicationContext::getContainer() | ||
->get(EventDispatcherInterface::class) | ||
->dispatch($event); | ||
} |
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
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