From 553976f73b554e500dfd4ae2679356608ef7413c Mon Sep 17 00:00:00 2001 From: Luke Morcom Date: Fri, 29 Nov 2024 21:06:24 +0000 Subject: [PATCH 1/3] Add support for notification class overrides --- src/Events/LongWaitDetected.php | 5 ++- src/Horizon.php | 21 ++++++++++++ tests/Feature/NotificationOverridesTest.php | 36 +++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/NotificationOverridesTest.php diff --git a/src/Events/LongWaitDetected.php b/src/Events/LongWaitDetected.php index 1794fe6f..8b8b9e60 100644 --- a/src/Events/LongWaitDetected.php +++ b/src/Events/LongWaitDetected.php @@ -2,6 +2,7 @@ namespace Laravel\Horizon\Events; +use Laravel\Horizon\Horizon; use Laravel\Horizon\Notifications\LongWaitDetected as LongWaitDetectedNotification; class LongWaitDetected @@ -49,7 +50,9 @@ public function __construct($connection, $queue, $seconds) */ public function toNotification() { - return new LongWaitDetectedNotification( + $notificationClass = Horizon::$notificationOverrides[$this::class] ?: LongWaitDetectedNotification::class; + + return new $notificationClass( $this->connection, $this->queue, $this->seconds ); } diff --git a/src/Horizon.php b/src/Horizon.php index edea3c30..fdd1572d 100644 --- a/src/Horizon.php +++ b/src/Horizon.php @@ -54,6 +54,14 @@ class Horizon */ public static $useDarkTheme = false; + /** + * The custom notifications to use. + * + * @var array + */ + public static $notificationOverrides; + + /** * The database configuration methods. * @@ -224,4 +232,17 @@ public static function routeSmsNotificationsTo($number) return new static; } + + /** + * Specify any custom notification classes which should be used. + * + * @param array $overrides + * @return static + */ + public static function overrideNotifications($overrides) + { + static::$notificationOverrides = $overrides; + + return new static; + } } diff --git a/tests/Feature/NotificationOverridesTest.php b/tests/Feature/NotificationOverridesTest.php new file mode 100644 index 00000000..7edc6f6c --- /dev/null +++ b/tests/Feature/NotificationOverridesTest.php @@ -0,0 +1,36 @@ +line('This is a custom notification for a long wait.'); + } + }; + + Horizon::routeMailNotificationsTo('taylor@laravel.com'); + + Horizon::overrideNotifications([ + LongWaitDetected::class => get_class($customNotification) + ]); + + event(new LongWaitDetected('redis', 'test-queue-2', 60)); + + Notification::assertSentOnDemand(get_class($customNotification)); + } +} From afc55d28d342fa19300a0721292ffe46eb3b7815 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 2 Dec 2024 07:35:27 -0800 Subject: [PATCH 2/3] use container --- .../LongWaitDetectedNotification.php | 8 ++++++ src/Events/LongWaitDetected.php | 14 +++++------ src/Horizon.php | 21 ---------------- src/Notifications/LongWaitDetected.php | 3 ++- src/ServiceBindings.php | 3 +++ tests/Feature/NotificationOverridesTest.php | 25 ++++++++++--------- 6 files changed, 33 insertions(+), 41 deletions(-) create mode 100644 src/Contracts/LongWaitDetectedNotification.php diff --git a/src/Contracts/LongWaitDetectedNotification.php b/src/Contracts/LongWaitDetectedNotification.php new file mode 100644 index 00000000..48efa656 --- /dev/null +++ b/src/Contracts/LongWaitDetectedNotification.php @@ -0,0 +1,8 @@ +connection, $this->queue, $this->seconds - ); + return Container::getInstance()->make(LongWaitDetectedNotification::class, [ + 'connection' => $this->connection, + 'queue' => $this->queue, + 'seconds' => $this->seconds, + ]); } } diff --git a/src/Horizon.php b/src/Horizon.php index fdd1572d..edea3c30 100644 --- a/src/Horizon.php +++ b/src/Horizon.php @@ -54,14 +54,6 @@ class Horizon */ public static $useDarkTheme = false; - /** - * The custom notifications to use. - * - * @var array - */ - public static $notificationOverrides; - - /** * The database configuration methods. * @@ -232,17 +224,4 @@ public static function routeSmsNotificationsTo($number) return new static; } - - /** - * Specify any custom notification classes which should be used. - * - * @param array $overrides - * @return static - */ - public static function overrideNotifications($overrides) - { - static::$notificationOverrides = $overrides; - - return new static; - } } diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index 5d974c59..f724f777 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -10,9 +10,10 @@ use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock; use Illuminate\Notifications\Slack\SlackMessage as ChannelIdSlackMessage; use Illuminate\Support\Str; +use Laravel\Horizon\Contracts\LongWaitDetectedNotification; use Laravel\Horizon\Horizon; -class LongWaitDetected extends Notification +class LongWaitDetected extends Notification implements LongWaitDetectedNotification { use Queueable; diff --git a/src/ServiceBindings.php b/src/ServiceBindings.php index cff53682..054d08b0 100644 --- a/src/ServiceBindings.php +++ b/src/ServiceBindings.php @@ -27,5 +27,8 @@ trait ServiceBindings Contracts\SupervisorRepository::class => Repositories\RedisSupervisorRepository::class, Contracts\TagRepository::class => Repositories\RedisTagRepository::class, Contracts\WorkloadRepository::class => Repositories\RedisWorkloadRepository::class, + + // Notifications... + Contracts\LongWaitDetectedNotification::class => Notifications\LongWaitDetected::class, ]; } diff --git a/tests/Feature/NotificationOverridesTest.php b/tests/Feature/NotificationOverridesTest.php index 7edc6f6c..7a589daa 100644 --- a/tests/Feature/NotificationOverridesTest.php +++ b/tests/Feature/NotificationOverridesTest.php @@ -2,8 +2,10 @@ namespace Laravel\Horizon\Tests\Feature; +use Illuminate\Container\Container; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Support\Facades\Notification; +use Laravel\Horizon\Contracts\LongWaitDetectedNotification as LongWaitDetectedNotificationContract; use Laravel\Horizon\Events\LongWaitDetected; use Laravel\Horizon\Horizon; use Laravel\Horizon\Notifications\LongWaitDetected as LongWaitDetectedNotification; @@ -15,22 +17,21 @@ public function test_custom_notifications_are_sent_if_specified() { Notification::fake(); - $customNotification = new class('redis', 'test-queue-2', 60) extends LongWaitDetectedNotification { - public function toMail($notifiable) - { - return (new MailMessage) - ->line('This is a custom notification for a long wait.'); - } - }; - Horizon::routeMailNotificationsTo('taylor@laravel.com'); - Horizon::overrideNotifications([ - LongWaitDetected::class => get_class($customNotification) - ]); + Container::getInstance()->bind(LongWaitDetectedNotificationContract::class, CustomLongWaitDetectedNotification::class); event(new LongWaitDetected('redis', 'test-queue-2', 60)); - Notification::assertSentOnDemand(get_class($customNotification)); + Notification::assertSentOnDemand(CustomLongWaitDetectedNotification::class); + } +} + +class CustomLongWaitDetectedNotification extends LongWaitDetectedNotification implements LongWaitDetectedNotificationContract +{ + public function toMail($notifiable) + { + return (new MailMessage) + ->line('This is a custom notification for a long wait.'); } } From d33290036313b27f126b57044f7bf56b3616e340 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 2 Dec 2024 07:39:45 -0800 Subject: [PATCH 3/3] add test --- tests/Feature/NotificationOverridesTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/Feature/NotificationOverridesTest.php b/tests/Feature/NotificationOverridesTest.php index 7a589daa..6076d0d6 100644 --- a/tests/Feature/NotificationOverridesTest.php +++ b/tests/Feature/NotificationOverridesTest.php @@ -25,6 +25,17 @@ public function test_custom_notifications_are_sent_if_specified() Notification::assertSentOnDemand(CustomLongWaitDetectedNotification::class); } + + public function test_normal_notifications_are_sent_if_not_specified() + { + Notification::fake(); + + Horizon::routeMailNotificationsTo('taylor@laravel.com'); + + event(new LongWaitDetected('redis', 'test-queue-2', 60)); + + Notification::assertSentOnDemand(LongWaitDetectedNotification::class); + } } class CustomLongWaitDetectedNotification extends LongWaitDetectedNotification implements LongWaitDetectedNotificationContract