<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
monkeyscloud / monkeyslegion-notifications example snippets
use MonkeysLegion\Events\ListenerProvider;
use MonkeysLegion\Notifications\Attributes\AttributeProcessor;
use MonkeysLegion\Notifications\Channels\DatabaseChannel;
use MonkeysLegion\Notifications\Channels\MailChannel;
use MonkeysLegion\Notifications\NotificationListener;
use MonkeysLegion\Notifications\NotificationManager;
$manager = new NotificationManager(
events: $eventDispatcher, // optional
queue: $queueDispatcher // optional
);
$manager->extend('mail', fn () => new MailChannel($mailer, $renderer));
$manager->extend('database', fn () => new DatabaseChannel($queryBuilder, 'notifications'));
$processor = new AttributeProcessor($manager);
$listenerProvider = new ListenerProvider();
$listener = new NotificationListener(
manager: $manager,
processor: $processor,
dispatcher: $listenerProvider
);
namespace App\Notifications;
use MonkeysLegion\Notifications\Contracts\NotificationInterface;
use MonkeysLegion\Notifications\Messages\MailMessage;
use MonkeysLegion\Queue\Contracts\ShouldQueue; // Optional: for background sending
class InvoicePaid implements NotificationInterface, ShouldQueue
{
public function __construct(public float $amount) {}
public function via($notifiable): array
{
return ['mail', 'database'];
}
public function toMail($notifiable): MailMessage
{
return (new MailMessage)
->subject('Invoice Paid')
->line("You have paid an invoice of ${$this->amount}")
->action('View Invoice', 'https://example.com/invoice/1');
}
public function toDatabase($notifiable): array
{
return [
'amount' => $this->amount,
'message' => "Invoice of ${$this->amount} paid."
];
}
}
use MonkeysLegion\Notifications\Traits\Notifiable;
class User
{
use Notifiable;
public string $email = '[email protected]';
}
$user->notify(new InvoicePaid(99.99));
use MonkeysLegion\Notifications\Attributes\Notify;
use App\Notifications\LowBalanceWarning;
class Account
{
use Notifiable;
#[Notify(LowBalanceWarning::class)]
public float $balance = 10.00;
}
$notificationManager->extend('my-channel', function () {
return new MyCustomChannel(/* dependencies */);
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.