Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
namespace Illuminate\Notifications;
class Action
{
/**
* The action text.
*
* @var string
*/
public $text;
/**
* The action URL.
*
* @var string
*/
public $url;
/**
* Create a new action instance.
*
* @param string $text
* @param string $url
* @return void
*/
public function __construct($text, $url)
{
$this->url = $url;
$this->text = $text;
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Contracts\Notifications\Dispatcher;
use InvalidArgumentException;
class AnonymousNotifiable
{
/**
* All of the notification routing information.
*
* @var array
*/
public $routes = [];
/**
* Add routing information to the target.
*
* @param string $channel
* @param mixed $route
* @return $this
*/
public function route($channel, $route)
{
if ($channel === 'database') {
throw new InvalidArgumentException('The database channel does not support on-demand notifications.');
}
$this->routes[$channel] = $route;
return $this;
}
/**
* Send the given notification.
*
* @param mixed $notification
* @return void
*/
public function notify($notification)
{
app(Dispatcher::class)->send($this, $notification);
}
/**
* Send the given notification immediately.
*
* @param mixed $notification
* @return void
*/
public function notifyNow($notification)
{
app(Dispatcher::class)->sendNow($this, $notification);
}
/**
* Get the notification routing information for the given driver.
*
* @param string $driver
* @return mixed
*/
public function routeNotificationFor($driver)
{
return $this->routes[$driver] ?? null;
}
/**
* Get the value of the notifiable's primary key.
*
* @return mixed
*/
public function getKey()
{
//
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Contracts\Bus\Dispatcher as Bus;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Notifications\Dispatcher as DispatcherContract;
use Illuminate\Contracts\Notifications\Factory as FactoryContract;
use Illuminate\Support\Manager;
use InvalidArgumentException;
class ChannelManager extends Manager implements DispatcherContract, FactoryContract
{
/**
* The default channel used to deliver messages.
*
* @var string
*/
protected $defaultChannel = 'mail';
/**
* The locale used when sending notifications.
*
* @var string|null
*/
protected $locale;
/**
* Send the given notification to the given notifiable entities.
*
* @param \Illuminate\Support\Collection|array|mixed $notifiables
* @param mixed $notification
* @return void
*/
public function send($notifiables, $notification)
{
return (new NotificationSender(
$this, $this->container->make(Bus::class), $this->container->make(Dispatcher::class), $this->locale)
)->send($notifiables, $notification);
}
/**
* Send the given notification immediately.
*
* @param \Illuminate\Support\Collection|array|mixed $notifiables
* @param mixed $notification
* @param array|null $channels
* @return void
*/
public function sendNow($notifiables, $notification, array $channels = null)
{
return (new NotificationSender(
$this, $this->container->make(Bus::class), $this->container->make(Dispatcher::class), $this->locale)
)->sendNow($notifiables, $notification, $channels);
}
/**
* Get a channel instance.
*
* @param string|null $name
* @return mixed
*/
public function channel($name = null)
{
return $this->driver($name);
}
/**
* Create an instance of the database driver.
*
* @return \Illuminate\Notifications\Channels\DatabaseChannel
*/
protected function createDatabaseDriver()
{
return $this->container->make(Channels\DatabaseChannel::class);
}
/**
* Create an instance of the broadcast driver.
*
* @return \Illuminate\Notifications\Channels\BroadcastChannel
*/
protected function createBroadcastDriver()
{
return $this->container->make(Channels\BroadcastChannel::class);
}
/**
* Create an instance of the mail driver.
*
* @return \Illuminate\Notifications\Channels\MailChannel
*/
protected function createMailDriver()
{
return $this->container->make(Channels\MailChannel::class);
}
/**
* Create a new driver instance.
*
* @param string $driver
* @return mixed
*
* @throws \InvalidArgumentException
*/
protected function createDriver($driver)
{
try {
return parent::createDriver($driver);
} catch (InvalidArgumentException $e) {
if (class_exists($driver)) {
return $this->container->make($driver);
}
throw $e;
}
}
/**
* Get the default channel driver name.
*
* @return string
*/
public function getDefaultDriver()
{
return $this->defaultChannel;
}
/**
* Get the default channel driver name.
*
* @return string
*/
public function deliversVia()
{
return $this->getDefaultDriver();
}
/**
* Set the default channel driver name.
*
* @param string $channel
* @return void
*/
public function deliverVia($channel)
{
$this->defaultChannel = $channel;
}
/**
* Set the locale of notifications.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;
return $this;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Illuminate\Notifications\Channels;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Events\BroadcastNotificationCreated;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;
use RuntimeException;
class BroadcastChannel
{
/**
* The event dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;
/**
* Create a new database channel.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function __construct(Dispatcher $events)
{
$this->events = $events;
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array|null
*/
public function send($notifiable, Notification $notification)
{
$message = $this->getData($notifiable, $notification);
$event = new BroadcastNotificationCreated(
$notifiable, $notification, is_array($message) ? $message : $message->data
);
if ($message instanceof BroadcastMessage) {
$event->onConnection($message->connection)
->onQueue($message->queue);
}
return $this->events->dispatch($event);
}
/**
* Get the data for the notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return mixed
*
* @throws \RuntimeException
*/
protected function getData($notifiable, Notification $notification)
{
if (method_exists($notification, 'toBroadcast')) {
return $notification->toBroadcast($notifiable);
}
if (method_exists($notification, 'toArray')) {
return $notification->toArray($notifiable);
}
throw new RuntimeException('Notification is missing toArray method.');
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Illuminate\Notifications\Channels;
use Illuminate\Notifications\Notification;
use RuntimeException;
class DatabaseChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Illuminate\Database\Eloquent\Model
*/
public function send($notifiable, Notification $notification)
{
return $notifiable->routeNotificationFor('database', $notification)->create(
$this->buildPayload($notifiable, $notification)
);
}
/**
* Get the data for the notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*
* @throws \RuntimeException
*/
protected function getData($notifiable, Notification $notification)
{
if (method_exists($notification, 'toDatabase')) {
return is_array($data = $notification->toDatabase($notifiable))
? $data : $data->data;
}
if (method_exists($notification, 'toArray')) {
return $notification->toArray($notifiable);
}
throw new RuntimeException('Notification is missing toDatabase / toArray method.');
}
/**
* Build an array payload for the DatabaseNotification Model.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return array
*/
protected function buildPayload($notifiable, Notification $notification)
{
return [
'id' => $notification->id,
'type' => get_class($notification),
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
];
}
}

View File

@@ -0,0 +1,252 @@
<?php
namespace Illuminate\Notifications\Channels;
use Illuminate\Contracts\Mail\Factory as MailFactory;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Markdown;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class MailChannel
{
/**
* The mailer implementation.
*
* @var \Illuminate\Contracts\Mail\Factory
*/
protected $mailer;
/**
* The markdown implementation.
*
* @var \Illuminate\Mail\Markdown
*/
protected $markdown;
/**
* Create a new mail channel instance.
*
* @param \Illuminate\Contracts\Mail\Factory $mailer
* @param \Illuminate\Mail\Markdown $markdown
* @return void
*/
public function __construct(MailFactory $mailer, Markdown $markdown)
{
$this->mailer = $mailer;
$this->markdown = $markdown;
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toMail($notifiable);
if (! $notifiable->routeNotificationFor('mail', $notification) &&
! $message instanceof Mailable) {
return;
}
if ($message instanceof Mailable) {
return $message->send($this->mailer);
}
$this->mailer->mailer($message->mailer ?? null)->send(
$this->buildView($message),
array_merge($message->data(), $this->additionalMessageData($notification)),
$this->messageBuilder($notifiable, $notification, $message)
);
}
/**
* Get the mailer Closure for the message.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return \Closure
*/
protected function messageBuilder($notifiable, $notification, $message)
{
return function ($mailMessage) use ($notifiable, $notification, $message) {
$this->buildMessage($mailMessage, $notifiable, $notification, $message);
};
}
/**
* Build the notification's view.
*
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return string|array
*/
protected function buildView($message)
{
if ($message->view) {
return $message->view;
}
if (property_exists($message, 'theme') && ! is_null($message->theme)) {
$this->markdown->theme($message->theme);
}
return [
'html' => $this->markdown->render($message->markdown, $message->data()),
'text' => $this->markdown->renderText($message->markdown, $message->data()),
];
}
/**
* Get additional meta-data to pass along with the view data.
*
* @param \Illuminate\Notifications\Notification $notification
* @return array
*/
protected function additionalMessageData($notification)
{
return [
'__laravel_notification_id' => $notification->id,
'__laravel_notification' => get_class($notification),
'__laravel_notification_queued' => in_array(
ShouldQueue::class,
class_implements($notification)
),
];
}
/**
* Build the mail message.
*
* @param \Illuminate\Mail\Message $mailMessage
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return void
*/
protected function buildMessage($mailMessage, $notifiable, $notification, $message)
{
$this->addressMessage($mailMessage, $notifiable, $notification, $message);
$mailMessage->subject($message->subject ?: Str::title(
Str::snake(class_basename($notification), ' ')
));
$this->addAttachments($mailMessage, $message);
if (! is_null($message->priority)) {
$mailMessage->setPriority($message->priority);
}
$this->runCallbacks($mailMessage, $message);
}
/**
* Address the mail message.
*
* @param \Illuminate\Mail\Message $mailMessage
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return void
*/
protected function addressMessage($mailMessage, $notifiable, $notification, $message)
{
$this->addSender($mailMessage, $message);
$mailMessage->to($this->getRecipients($notifiable, $notification, $message));
if (! empty($message->cc)) {
foreach ($message->cc as $cc) {
$mailMessage->cc($cc[0], Arr::get($cc, 1));
}
}
if (! empty($message->bcc)) {
foreach ($message->bcc as $bcc) {
$mailMessage->bcc($bcc[0], Arr::get($bcc, 1));
}
}
}
/**
* Add the "from" and "reply to" addresses to the message.
*
* @param \Illuminate\Mail\Message $mailMessage
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return void
*/
protected function addSender($mailMessage, $message)
{
if (! empty($message->from)) {
$mailMessage->from($message->from[0], Arr::get($message->from, 1));
}
if (! empty($message->replyTo)) {
foreach ($message->replyTo as $replyTo) {
$mailMessage->replyTo($replyTo[0], Arr::get($replyTo, 1));
}
}
}
/**
* Get the recipients of the given message.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return mixed
*/
protected function getRecipients($notifiable, $notification, $message)
{
if (is_string($recipients = $notifiable->routeNotificationFor('mail', $notification))) {
$recipients = [$recipients];
}
return collect($recipients)->mapWithKeys(function ($recipient, $email) {
return is_numeric($email)
? [$email => (is_string($recipient) ? $recipient : $recipient->email)]
: [$email => $recipient];
})->all();
}
/**
* Add the attachments to the message.
*
* @param \Illuminate\Mail\Message $mailMessage
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return void
*/
protected function addAttachments($mailMessage, $message)
{
foreach ($message->attachments as $attachment) {
$mailMessage->attach($attachment['file'], $attachment['options']);
}
foreach ($message->rawAttachments as $attachment) {
$mailMessage->attachData($attachment['data'], $attachment['name'], $attachment['options']);
}
}
/**
* Run the callbacks for the message.
*
* @param \Illuminate\Mail\Message $mailMessage
* @param \Illuminate\Notifications\Messages\MailMessage $message
* @return $this
*/
protected function runCallbacks($mailMessage, $message)
{
foreach ($message->callbacks as $callback) {
$callback($mailMessage->getSwiftMessage());
}
return $this;
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace Illuminate\Notifications\Console;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
class NotificationTableCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'notifications:table';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a migration for the notifications table';
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* @var \Illuminate\Support\Composer
*/
protected $composer;
/**
* Create a new notifications table command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Support\Composer $composer
* @return void
*/
public function __construct(Filesystem $files, Composer $composer)
{
parent::__construct();
$this->files = $files;
$this->composer = $composer;
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$fullPath = $this->createBaseMigration();
$this->files->put($fullPath, $this->files->get(__DIR__.'/stubs/notifications.stub'));
$this->info('Migration created successfully!');
$this->composer->dumpAutoloads();
}
/**
* Create a base migration file for the notifications.
*
* @return string
*/
protected function createBaseMigration()
{
$name = 'create_notifications_table';
$path = $this->laravel->databasePath().'/migrations';
return $this->laravel['migration.creator']->create($name, $path);
}
}

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Database\Eloquent\Model;
class DatabaseNotification extends Model
{
/**
* The "type" of the primary key ID.
*
* @var string
*/
protected $keyType = 'string';
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'notifications';
/**
* The guarded attributes on the model.
*
* @var array
*/
protected $guarded = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
'read_at' => 'datetime',
];
/**
* Get the notifiable entity that the notification belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function notifiable()
{
return $this->morphTo();
}
/**
* Mark the notification as read.
*
* @return void
*/
public function markAsRead()
{
if (is_null($this->read_at)) {
$this->forceFill(['read_at' => $this->freshTimestamp()])->save();
}
}
/**
* Mark the notification as unread.
*
* @return void
*/
public function markAsUnread()
{
if (! is_null($this->read_at)) {
$this->forceFill(['read_at' => null])->save();
}
}
/**
* Determine if a notification has been read.
*
* @return bool
*/
public function read()
{
return $this->read_at !== null;
}
/**
* Determine if a notification has not been read.
*
* @return bool
*/
public function unread()
{
return $this->read_at === null;
}
/**
* Create a new database notification collection instance.
*
* @param array $models
* @return \Illuminate\Notifications\DatabaseNotificationCollection
*/
public function newCollection(array $models = [])
{
return new DatabaseNotificationCollection($models);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Database\Eloquent\Collection;
class DatabaseNotificationCollection extends Collection
{
/**
* Mark all notifications as read.
*
* @return void
*/
public function markAsRead()
{
$this->each->markAsRead();
}
/**
* Mark all notifications as unread.
*
* @return void
*/
public function markAsUnread()
{
$this->each->markAsUnread();
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace Illuminate\Notifications\Events;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class BroadcastNotificationCreated implements ShouldBroadcast
{
use Queueable, SerializesModels;
/**
* The notifiable entity who received the notification.
*
* @var mixed
*/
public $notifiable;
/**
* The notification instance.
*
* @var \Illuminate\Notifications\Notification
*/
public $notification;
/**
* The notification data.
*
* @var array
*/
public $data = [];
/**
* Create a new event instance.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param array $data
* @return void
*/
public function __construct($notifiable, $notification, $data)
{
$this->data = $data;
$this->notifiable = $notifiable;
$this->notification = $notification;
}
/**
* Get the channels the event should broadcast on.
*
* @return array
*/
public function broadcastOn()
{
$channels = $this->notification->broadcastOn();
if (! empty($channels)) {
return $channels;
}
if (is_string($channels = $this->channelName())) {
return [new PrivateChannel($channels)];
}
return collect($channels)->map(function ($channel) {
return new PrivateChannel($channel);
})->all();
}
/**
* Get the broadcast channel name for the event.
*
* @return array|string
*/
protected function channelName()
{
if (method_exists($this->notifiable, 'receivesBroadcastNotificationsOn')) {
return $this->notifiable->receivesBroadcastNotificationsOn($this->notification);
}
$class = str_replace('\\', '.', get_class($this->notifiable));
return $class.'.'.$this->notifiable->getKey();
}
/**
* Get the data that should be sent with the broadcasted event.
*
* @return array
*/
public function broadcastWith()
{
return array_merge($this->data, [
'id' => $this->notification->id,
'type' => $this->broadcastType(),
]);
}
/**
* Get the type of the notification being broadcast.
*
* @return string
*/
public function broadcastType()
{
return method_exists($this->notification, 'broadcastType')
? $this->notification->broadcastType()
: get_class($this->notification);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Illuminate\Notifications\Events;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
class NotificationFailed
{
use Queueable, SerializesModels;
/**
* The notifiable entity who received the notification.
*
* @var mixed
*/
public $notifiable;
/**
* The notification instance.
*
* @var \Illuminate\Notifications\Notification
*/
public $notification;
/**
* The channel name.
*
* @var string
*/
public $channel;
/**
* The data needed to process this failure.
*
* @var array
*/
public $data = [];
/**
* Create a new event instance.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param string $channel
* @param array $data
* @return void
*/
public function __construct($notifiable, $notification, $channel, $data = [])
{
$this->data = $data;
$this->channel = $channel;
$this->notifiable = $notifiable;
$this->notification = $notification;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Illuminate\Notifications\Events;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
class NotificationSending
{
use Queueable, SerializesModels;
/**
* The notifiable entity who received the notification.
*
* @var mixed
*/
public $notifiable;
/**
* The notification instance.
*
* @var \Illuminate\Notifications\Notification
*/
public $notification;
/**
* The channel name.
*
* @var string
*/
public $channel;
/**
* Create a new event instance.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param string $channel
* @return void
*/
public function __construct($notifiable, $notification, $channel)
{
$this->channel = $channel;
$this->notifiable = $notifiable;
$this->notification = $notification;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Illuminate\Notifications\Events;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
class NotificationSent
{
use Queueable, SerializesModels;
/**
* The notifiable entity who received the notification.
*
* @var mixed
*/
public $notifiable;
/**
* The notification instance.
*
* @var \Illuminate\Notifications\Notification
*/
public $notification;
/**
* The channel name.
*
* @var string
*/
public $channel;
/**
* The channel's response.
*
* @var mixed
*/
public $response;
/**
* Create a new event instance.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param string $channel
* @param mixed $response
* @return void
*/
public function __construct($notifiable, $notification, $channel, $response = null)
{
$this->channel = $channel;
$this->response = $response;
$this->notifiable = $notifiable;
$this->notification = $notification;
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Illuminate\Notifications;
trait HasDatabaseNotifications
{
/**
* Get the entity's notifications.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function notifications()
{
return $this->morphMany(DatabaseNotification::class, 'notifiable')->orderBy('created_at', 'desc');
}
/**
* Get the entity's read notifications.
*
* @return \Illuminate\Database\Query\Builder
*/
public function readNotifications()
{
return $this->notifications()->whereNotNull('read_at');
}
/**
* Get the entity's unread notifications.
*
* @return \Illuminate\Database\Query\Builder
*/
public function unreadNotifications()
{
return $this->notifications()->whereNull('read_at');
}
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,41 @@
<?php
namespace Illuminate\Notifications\Messages;
use Illuminate\Bus\Queueable;
class BroadcastMessage
{
use Queueable;
/**
* The data for the notification.
*
* @var array
*/
public $data;
/**
* Create a new message instance.
*
* @param array $data
* @return void
*/
public function __construct(array $data)
{
$this->data = $data;
}
/**
* Set the message data.
*
* @param array $data
* @return $this
*/
public function data($data)
{
$this->data = $data;
return $this;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Illuminate\Notifications\Messages;
class DatabaseMessage
{
/**
* The data that should be stored with the notification.
*
* @var array
*/
public $data = [];
/**
* Create a new database message.
*
* @param array $data
* @return void
*/
public function __construct(array $data = [])
{
$this->data = $data;
}
}

View File

@@ -0,0 +1,335 @@
<?php
namespace Illuminate\Notifications\Messages;
use Illuminate\Container\Container;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Mail\Markdown;
use Traversable;
class MailMessage extends SimpleMessage implements Renderable
{
/**
* The view to be rendered.
*
* @var array|string
*/
public $view;
/**
* The view data for the message.
*
* @var array
*/
public $viewData = [];
/**
* The Markdown template to render (if applicable).
*
* @var string|null
*/
public $markdown = 'notifications::email';
/**
* The current theme being used when generating emails.
*
* @var string|null
*/
public $theme;
/**
* The "from" information for the message.
*
* @var array
*/
public $from = [];
/**
* The "reply to" information for the message.
*
* @var array
*/
public $replyTo = [];
/**
* The "cc" information for the message.
*
* @var array
*/
public $cc = [];
/**
* The "bcc" information for the message.
*
* @var array
*/
public $bcc = [];
/**
* The attachments for the message.
*
* @var array
*/
public $attachments = [];
/**
* The raw attachments for the message.
*
* @var array
*/
public $rawAttachments = [];
/**
* Priority level of the message.
*
* @var int
*/
public $priority;
/**
* The callbacks for the message.
*
* @var array
*/
public $callbacks = [];
/**
* Set the view for the mail message.
*
* @param array|string $view
* @param array $data
* @return $this
*/
public function view($view, array $data = [])
{
$this->view = $view;
$this->viewData = $data;
$this->markdown = null;
return $this;
}
/**
* Set the Markdown template for the notification.
*
* @param string $view
* @param array $data
* @return $this
*/
public function markdown($view, array $data = [])
{
$this->markdown = $view;
$this->viewData = $data;
$this->view = null;
return $this;
}
/**
* Set the default markdown template.
*
* @param string $template
* @return $this
*/
public function template($template)
{
$this->markdown = $template;
return $this;
}
/**
* Set the theme to use with the Markdown template.
*
* @param string $theme
* @return $this
*/
public function theme($theme)
{
$this->theme = $theme;
return $this;
}
/**
* Set the from address for the mail message.
*
* @param string $address
* @param string|null $name
* @return $this
*/
public function from($address, $name = null)
{
$this->from = [$address, $name];
return $this;
}
/**
* Set the "reply to" address of the message.
*
* @param array|string $address
* @param string|null $name
* @return $this
*/
public function replyTo($address, $name = null)
{
if ($this->arrayOfAddresses($address)) {
$this->replyTo += $this->parseAddresses($address);
} else {
$this->replyTo[] = [$address, $name];
}
return $this;
}
/**
* Set the cc address for the mail message.
*
* @param array|string $address
* @param string|null $name
* @return $this
*/
public function cc($address, $name = null)
{
if ($this->arrayOfAddresses($address)) {
$this->cc += $this->parseAddresses($address);
} else {
$this->cc[] = [$address, $name];
}
return $this;
}
/**
* Set the bcc address for the mail message.
*
* @param array|string $address
* @param string|null $name
* @return $this
*/
public function bcc($address, $name = null)
{
if ($this->arrayOfAddresses($address)) {
$this->bcc += $this->parseAddresses($address);
} else {
$this->bcc[] = [$address, $name];
}
return $this;
}
/**
* Attach a file to the message.
*
* @param string $file
* @param array $options
* @return $this
*/
public function attach($file, array $options = [])
{
$this->attachments[] = compact('file', 'options');
return $this;
}
/**
* Attach in-memory data as an attachment.
*
* @param string $data
* @param string $name
* @param array $options
* @return $this
*/
public function attachData($data, $name, array $options = [])
{
$this->rawAttachments[] = compact('data', 'name', 'options');
return $this;
}
/**
* Set the priority of this message.
*
* The value is an integer where 1 is the highest priority and 5 is the lowest.
*
* @param int $level
* @return $this
*/
public function priority($level)
{
$this->priority = $level;
return $this;
}
/**
* Get the data array for the mail message.
*
* @return array
*/
public function data()
{
return array_merge($this->toArray(), $this->viewData);
}
/**
* Parse the multi-address array into the necessary format.
*
* @param array $value
* @return array
*/
protected function parseAddresses($value)
{
return collect($value)->map(function ($address, $name) {
return [$address, is_numeric($name) ? null : $name];
})->values()->all();
}
/**
* Determine if the given "address" is actually an array of addresses.
*
* @param mixed $address
* @return bool
*/
protected function arrayOfAddresses($address)
{
return is_array($address) ||
$address instanceof Arrayable ||
$address instanceof Traversable;
}
/**
* Render the mail notification message into an HTML string.
*
* @return string
*/
public function render()
{
if (isset($this->view)) {
return Container::getInstance()->make('mailer')->render(
$this->view, $this->data()
);
}
return Container::getInstance()
->make(Markdown::class)
->render($this->markdown, $this->data());
}
/**
* Register a callback to be called with the Swift message instance.
*
* @param callable $callback
* @return $this
*/
public function withSwiftMessage($callback)
{
$this->callbacks[] = $callback;
return $this;
}
}

View File

@@ -0,0 +1,245 @@
<?php
namespace Illuminate\Notifications\Messages;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Notifications\Action;
class SimpleMessage
{
/**
* The "level" of the notification (info, success, error).
*
* @var string
*/
public $level = 'info';
/**
* The subject of the notification.
*
* @var string
*/
public $subject;
/**
* The notification's greeting.
*
* @var string
*/
public $greeting;
/**
* The notification's salutation.
*
* @var string
*/
public $salutation;
/**
* The "intro" lines of the notification.
*
* @var array
*/
public $introLines = [];
/**
* The "outro" lines of the notification.
*
* @var array
*/
public $outroLines = [];
/**
* The text / label for the action.
*
* @var string
*/
public $actionText;
/**
* The action URL.
*
* @var string
*/
public $actionUrl;
/**
* The name of the mailer that should send the notification.
*
* @var string
*/
public $mailer;
/**
* Indicate that the notification gives information about a successful operation.
*
* @return $this
*/
public function success()
{
$this->level = 'success';
return $this;
}
/**
* Indicate that the notification gives information about an error.
*
* @return $this
*/
public function error()
{
$this->level = 'error';
return $this;
}
/**
* Set the "level" of the notification (success, error, etc.).
*
* @param string $level
* @return $this
*/
public function level($level)
{
$this->level = $level;
return $this;
}
/**
* Set the subject of the notification.
*
* @param string $subject
* @return $this
*/
public function subject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* Set the greeting of the notification.
*
* @param string $greeting
* @return $this
*/
public function greeting($greeting)
{
$this->greeting = $greeting;
return $this;
}
/**
* Set the salutation of the notification.
*
* @param string $salutation
* @return $this
*/
public function salutation($salutation)
{
$this->salutation = $salutation;
return $this;
}
/**
* Add a line of text to the notification.
*
* @param mixed $line
* @return $this
*/
public function line($line)
{
return $this->with($line);
}
/**
* Add a line of text to the notification.
*
* @param mixed $line
* @return $this
*/
public function with($line)
{
if ($line instanceof Action) {
$this->action($line->text, $line->url);
} elseif (! $this->actionText) {
$this->introLines[] = $this->formatLine($line);
} else {
$this->outroLines[] = $this->formatLine($line);
}
return $this;
}
/**
* Format the given line of text.
*
* @param \Illuminate\Contracts\Support\Htmlable|string|array $line
* @return \Illuminate\Contracts\Support\Htmlable|string
*/
protected function formatLine($line)
{
if ($line instanceof Htmlable) {
return $line;
}
if (is_array($line)) {
return implode(' ', array_map('trim', $line));
}
return trim(implode(' ', array_map('trim', preg_split('/\\r\\n|\\r|\\n/', $line))));
}
/**
* Configure the "call to action" button.
*
* @param string $text
* @param string $url
* @return $this
*/
public function action($text, $url)
{
$this->actionText = $text;
$this->actionUrl = $url;
return $this;
}
/**
* Set the name of the mailer that should send the notification.
*
* @param string $mailer
* @return $this
*/
public function mailer($mailer)
{
$this->mailer = $mailer;
return $this;
}
/**
* Get an array representation of the message.
*
* @return array
*/
public function toArray()
{
return [
'level' => $this->level,
'subject' => $this->subject,
'greeting' => $this->greeting,
'salutation' => $this->salutation,
'introLines' => $this->introLines,
'outroLines' => $this->outroLines,
'actionText' => $this->actionText,
'actionUrl' => $this->actionUrl,
'displayableActionUrl' => str_replace(['mailto:', 'tel:'], '', $this->actionUrl),
];
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Illuminate\Notifications;
trait Notifiable
{
use HasDatabaseNotifications, RoutesNotifications;
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Queue\SerializesModels;
class Notification
{
use SerializesModels;
/**
* The unique identifier for the notification.
*
* @var string
*/
public $id;
/**
* The locale to be used when sending the notification.
*
* @var string|null
*/
public $locale;
/**
* Get the channels the event should broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
/**
* Set the locale to send this notification in.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;
return $this;
}
}

View File

@@ -0,0 +1,232 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Collection as ModelCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Events\NotificationSending;
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Localizable;
class NotificationSender
{
use Localizable;
/**
* The notification manager instance.
*
* @var \Illuminate\Notifications\ChannelManager
*/
protected $manager;
/**
* The Bus dispatcher instance.
*
* @var \Illuminate\Contracts\Bus\Dispatcher
*/
protected $bus;
/**
* The event dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;
/**
* The locale to be used when sending notifications.
*
* @var string|null
*/
protected $locale;
/**
* Create a new notification sender instance.
*
* @param \Illuminate\Notifications\ChannelManager $manager
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @param string|null $locale
* @return void
*/
public function __construct($manager, $bus, $events, $locale = null)
{
$this->bus = $bus;
$this->events = $events;
$this->locale = $locale;
$this->manager = $manager;
}
/**
* Send the given notification to the given notifiable entities.
*
* @param \Illuminate\Support\Collection|array|mixed $notifiables
* @param mixed $notification
* @return void
*/
public function send($notifiables, $notification)
{
$notifiables = $this->formatNotifiables($notifiables);
if ($notification instanceof ShouldQueue) {
return $this->queueNotification($notifiables, $notification);
}
return $this->sendNow($notifiables, $notification);
}
/**
* Send the given notification immediately.
*
* @param \Illuminate\Support\Collection|array|mixed $notifiables
* @param mixed $notification
* @param array|null $channels
* @return void
*/
public function sendNow($notifiables, $notification, array $channels = null)
{
$notifiables = $this->formatNotifiables($notifiables);
$original = clone $notification;
foreach ($notifiables as $notifiable) {
if (empty($viaChannels = $channels ?: $notification->via($notifiable))) {
continue;
}
$this->withLocale($this->preferredLocale($notifiable, $notification), function () use ($viaChannels, $notifiable, $original) {
$notificationId = Str::uuid()->toString();
foreach ((array) $viaChannels as $channel) {
if (! ($notifiable instanceof AnonymousNotifiable && $channel === 'database')) {
$this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel);
}
}
});
}
}
/**
* Get the notifiable's preferred locale for the notification.
*
* @param mixed $notifiable
* @param mixed $notification
* @return string|null
*/
protected function preferredLocale($notifiable, $notification)
{
return $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
if ($notifiable instanceof HasLocalePreference) {
return $notifiable->preferredLocale();
}
});
}
/**
* Send the given notification to the given notifiable via a channel.
*
* @param mixed $notifiable
* @param string $id
* @param mixed $notification
* @param string $channel
* @return void
*/
protected function sendToNotifiable($notifiable, $id, $notification, $channel)
{
if (! $notification->id) {
$notification->id = $id;
}
if (! $this->shouldSendNotification($notifiable, $notification, $channel)) {
return;
}
$response = $this->manager->driver($channel)->send($notifiable, $notification);
$this->events->dispatch(
new NotificationSent($notifiable, $notification, $channel, $response)
);
}
/**
* Determines if the notification can be sent.
*
* @param mixed $notifiable
* @param mixed $notification
* @param string $channel
* @return bool
*/
protected function shouldSendNotification($notifiable, $notification, $channel)
{
return $this->events->until(
new NotificationSending($notifiable, $notification, $channel)
) !== false;
}
/**
* Queue the given notification instances.
*
* @param mixed $notifiables
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
protected function queueNotification($notifiables, $notification)
{
$notifiables = $this->formatNotifiables($notifiables);
$original = clone $notification;
foreach ($notifiables as $notifiable) {
$notificationId = Str::uuid()->toString();
foreach ((array) $original->via($notifiable) as $channel) {
$notification = clone $original;
$notification->id = $notificationId;
if (! is_null($this->locale)) {
$notification->locale = $this->locale;
}
$queue = $notification->queue;
if (method_exists($notification, 'viaQueues')) {
$queue = $notification->viaQueues()[$channel] ?? null;
}
$this->bus->dispatch(
(new SendQueuedNotifications($notifiable, $notification, [$channel]))
->onConnection($notification->connection)
->onQueue($queue)
->delay($notification->delay)
->through(
array_merge(
method_exists($notification, 'middleware') ? $notification->middleware() : [],
$notification->middleware ?? []
)
)
);
}
}
}
/**
* Format the notifiables into a Collection / array if necessary.
*
* @param mixed $notifiables
* @return \Illuminate\Database\Eloquent\Collection|array
*/
protected function formatNotifiables($notifiables)
{
if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
return $notifiables instanceof Model
? new ModelCollection([$notifiables]) : [$notifiables];
}
return $notifiables;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Contracts\Notifications\Dispatcher as DispatcherContract;
use Illuminate\Contracts\Notifications\Factory as FactoryContract;
use Illuminate\Support\ServiceProvider;
class NotificationServiceProvider extends ServiceProvider
{
/**
* Boot the application services.
*
* @return void
*/
public function boot()
{
$this->loadViewsFrom(__DIR__.'/resources/views', 'notifications');
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/notifications'),
], 'laravel-notifications');
}
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(ChannelManager::class, function ($app) {
return new ChannelManager($app);
});
$this->app->alias(
ChannelManager::class, DispatcherContract::class
);
$this->app->alias(
ChannelManager::class, FactoryContract::class
);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Contracts\Notifications\Dispatcher;
use Illuminate\Support\Str;
trait RoutesNotifications
{
/**
* Send the given notification.
*
* @param mixed $instance
* @return void
*/
public function notify($instance)
{
app(Dispatcher::class)->send($this, $instance);
}
/**
* Send the given notification immediately.
*
* @param mixed $instance
* @param array|null $channels
* @return void
*/
public function notifyNow($instance, array $channels = null)
{
app(Dispatcher::class)->sendNow($this, $instance, $channels);
}
/**
* Get the notification routing information for the given driver.
*
* @param string $driver
* @param \Illuminate\Notifications\Notification|null $notification
* @return mixed
*/
public function routeNotificationFor($driver, $notification = null)
{
if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
return $this->{$method}($notification);
}
switch ($driver) {
case 'database':
return $this->notifications();
case 'mail':
return $this->email;
}
}
}

View File

@@ -0,0 +1,158 @@
<?php
namespace Illuminate\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
class SendQueuedNotifications implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
/**
* The notifiable entities that should receive the notification.
*
* @var \Illuminate\Support\Collection
*/
public $notifiables;
/**
* The notification to be sent.
*
* @var \Illuminate\Notifications\Notification
*/
public $notification;
/**
* All of the channels to send the notification to.
*
* @var array
*/
public $channels;
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries;
/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout;
/**
* Create a new job instance.
*
* @param \Illuminate\Notifications\Notifiable|\Illuminate\Support\Collection $notifiables
* @param \Illuminate\Notifications\Notification $notification
* @param array|null $channels
* @return void
*/
public function __construct($notifiables, $notification, array $channels = null)
{
$this->channels = $channels;
$this->notification = $notification;
$this->notifiables = $this->wrapNotifiables($notifiables);
$this->tries = property_exists($notification, 'tries') ? $notification->tries : null;
$this->timeout = property_exists($notification, 'timeout') ? $notification->timeout : null;
}
/**
* Wrap the notifiable(s) in a collection.
*
* @param \Illuminate\Notifications\Notifiable|\Illuminate\Support\Collection $notifiables
* @return \Illuminate\Support\Collection
*/
protected function wrapNotifiables($notifiables)
{
if ($notifiables instanceof Collection) {
return $notifiables;
} elseif ($notifiables instanceof Model) {
return EloquentCollection::wrap($notifiables);
}
return Collection::wrap($notifiables);
}
/**
* Send the notifications.
*
* @param \Illuminate\Notifications\ChannelManager $manager
* @return void
*/
public function handle(ChannelManager $manager)
{
$manager->sendNow($this->notifiables, $this->notification, $this->channels);
}
/**
* Get the display name for the queued job.
*
* @return string
*/
public function displayName()
{
return get_class($this->notification);
}
/**
* Call the failed method on the notification instance.
*
* @param \Throwable $e
* @return void
*/
public function failed($e)
{
if (method_exists($this->notification, 'failed')) {
$this->notification->failed($e);
}
}
/**
* Get the retry delay for the notification.
*
* @return mixed
*/
public function retryAfter()
{
if (! method_exists($this->notification, 'retryAfter') && ! isset($this->notification->retryAfter)) {
return;
}
return $this->notification->retryAfter ?? $this->notification->retryAfter();
}
/**
* Get the expiration for the notification.
*
* @return mixed
*/
public function retryUntil()
{
if (! method_exists($this->notification, 'retryUntil') && ! isset($this->notification->timeoutAt)) {
return;
}
return $this->notification->timeoutAt ?? $this->notification->retryUntil();
}
/**
* Prepare the instance for cloning.
*
* @return void
*/
public function __clone()
{
$this->notifiables = clone $this->notifiables;
$this->notification = clone $this->notification;
}
}

View File

@@ -0,0 +1,44 @@
{
"name": "illuminate/notifications",
"description": "The Illuminate Notifications package.",
"license": "MIT",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"require": {
"php": "^7.2.5|^8.0",
"illuminate/broadcasting": "^7.0",
"illuminate/bus": "^7.0",
"illuminate/container": "^7.0",
"illuminate/contracts": "^7.0",
"illuminate/filesystem": "^7.0",
"illuminate/mail": "^7.0",
"illuminate/queue": "^7.0",
"illuminate/support": "^7.0"
},
"autoload": {
"psr-4": {
"Illuminate\\Notifications\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "7.x-dev"
}
},
"suggest": {
"illuminate/database": "Required to use the database transport (^7.0)."
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}

View File

@@ -0,0 +1,62 @@
@component('mail::message')
{{-- Greeting --}}
@if (! empty($greeting))
# {{ $greeting }}
@else
@if ($level === 'error')
# @lang('Whoops!')
@else
# @lang('Hello!')
@endif
@endif
{{-- Intro Lines --}}
@foreach ($introLines as $line)
{{ $line }}
@endforeach
{{-- Action Button --}}
@isset($actionText)
<?php
switch ($level) {
case 'success':
case 'error':
$color = $level;
break;
default:
$color = 'primary';
}
?>
@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent
@endisset
{{-- Outro Lines --}}
@foreach ($outroLines as $line)
{{ $line }}
@endforeach
{{-- Salutation --}}
@if (! empty($salutation))
{{ $salutation }}
@else
@lang('Regards'),<br>
{{ config('app.name') }}
@endif
{{-- Subcopy --}}
@isset($actionText)
@slot('subcopy')
@lang(
"If youre having trouble clicking the \":actionText\" button, copy and paste the URL below\n".
'into your web browser:',
[
'actionText' => $actionText,
]
) <span class="break-all">[{{ $displayableActionUrl }}]({{ $actionUrl }})</span>
@endslot
@endisset
@endcomponent