Skip to content

Commit

Permalink
feat: Update ChannelManager to support dynamic channel registration (…
Browse files Browse the repository at this point in the history
…#644)

* feat: Update ChannelManager to support dynamic channel registration

* feat: Refactor ChannelManager to improve channel registration

* feat: Add replace parameter to ChannelManager register method

* feat: Update ChannelManager to handle dynamic channel registration

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored May 24, 2024
1 parent 87e23b8 commit 952aab5
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Notification;

use Closure;
use FriendsOfHyperf\Notification\Contract\Channel;
use FriendsOfHyperf\Notification\Contract\Dispatcher;
use Hyperf\Contract\TranslatorInterface;
Expand Down Expand Up @@ -47,11 +48,26 @@ public function send(mixed $notifiables, Notification $notification): void
}

/**
* @param class-string<Channel> $class
* @param class-string<Channel>|Channel|Closure():Channel $class
*/
public function register(string $name, string $class): void
public function register(string $name, string|Channel|Closure $class, bool $replace = false): void
{
$this->channels[$name] = $this->container->get($class);
if (! $replace && isset($this->channels[$name])) {
throw new InvalidArgumentException("Channel [{$name}] is already defined.");
}

$instance = match (true) {
$class instanceof Closure => $class(),
$class instanceof Channel => $class,
is_string($class) && is_a($class, Channel::class, true) => $this->container->get($class),
default => null,
};

if (! $instance instanceof Channel) {
throw new InvalidArgumentException('Invalid channel.');
}

$this->channels[$name] = $instance;
}

/**
Expand Down

0 comments on commit 952aab5

Please sign in to comment.