Skip to content

Commit

Permalink
chore: extract dispatch and event helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
albertcht committed Dec 2, 2024
1 parent eb037bc commit 812fad3
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 15 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"files": [
"src/auth/src/Functions.php",
"src/bus/src/Functions.php",
"src/cache/src/Functions.php",
"src/config/src/Functions.php",
"src/event/src/Functions.php",
Expand Down
5 changes: 4 additions & 1 deletion src/bus/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
"autoload": {
"psr-4": {
"SwooleTW\\Hyperf\\Bus\\": "src/"
}
},
"files": [
"src/Functions.php"
]
},
"extra": {
"hyperf": {
Expand Down
52 changes: 52 additions & 0 deletions src/bus/src/Functions.php
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);
}
1 change: 0 additions & 1 deletion src/event/src/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ protected function createQueuedHandlerCallable(object|string $class, string $met
{
return function () use ($class, $method) {
$arguments = array_map(function ($a) {
// dd(debug_backtrace(2));
return is_object($a) ? clone $a : $a;
}, func_get_args());

Expand Down
26 changes: 21 additions & 5 deletions src/event/src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@
namespace SwooleTW\Hyperf\Event;

use Closure;
use Hyperf\Context\ApplicationContext;
use Psr\EventDispatcher\EventDispatcherInterface;

if (! function_exists('SwooleTW\Hyperf\Event\queueable')) {
function queueable(Closure $closure): QueuedClosure
{
return new QueuedClosure($closure);
}
/**
* 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);
}

function queueable(Closure $closure): QueuedClosure
{
return new QueuedClosure($closure);
}
2 changes: 2 additions & 0 deletions src/event/src/QueuedClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Illuminate\Events\CallQueuedListener;
use Laravel\SerializableClosure\SerializableClosure;

use function SwooleTW\Hyperf\Bus\dispatch;

class QueuedClosure
{
/**
Expand Down
11 changes: 3 additions & 8 deletions src/foundation/src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Hyperf\ViewEngine\Contract\FactoryInterface;
use Hyperf\ViewEngine\Contract\ViewInterface;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use SwooleTW\Hyperf\Auth\Contracts\Gate;
Expand All @@ -26,7 +25,6 @@
use SwooleTW\Hyperf\HttpMessage\Exceptions\HttpException;
use SwooleTW\Hyperf\HttpMessage\Exceptions\HttpResponseException;
use SwooleTW\Hyperf\HttpMessage\Exceptions\NotFoundHttpException;
use SwooleTW\Hyperf\Queue\CallQueuedClosure;
use SwooleTW\Hyperf\Router\UrlGenerator;
use SwooleTW\Hyperf\Support\Contracts\Responsable;

Expand Down Expand Up @@ -308,9 +306,7 @@ function app(?string $abstract = null, array $parameters = [])
*/
function dispatch($job): PendingClosureDispatch|PendingDispatch
{
return $job instanceof Closure
? new PendingClosureDispatch(CallQueuedClosure::create($job))
: new PendingDispatch($job);
return \SwooleTW\Hyperf\Bus\dispatch($job);
}
}

Expand All @@ -322,8 +318,7 @@ function dispatch($job): PendingClosureDispatch|PendingDispatch
*/
function dispatch_sync(mixed $job, mixed $handler = null): mixed
{
return app(Dispatcher::class)
->dispatchSync($job, $handler);
return \SwooleTW\Hyperf\Bus\dispatch_sync($job, $handler);
}
}

Expand All @@ -339,7 +334,7 @@ function dispatch_sync(mixed $job, mixed $handler = null): mixed
*/
function event(object $event)
{
return app(EventDispatcherInterface::class)->dispatch($event);
return \SwooleTW\Hyperf\Event\event($event);
}
}

Expand Down

0 comments on commit 812fad3

Please sign in to comment.