Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions contracts/binding/binding.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package binding

const (
Artisan = "goravel.artisan"
Auth = "goravel.auth"
Cache = "goravel.cache"
Config = "goravel.config"
Crypt = "goravel.crypt"
DB = "goravel.db"
Event = "goravel.event"
Gate = "goravel.gate"
Grpc = "goravel.grpc"
Hash = "goravel.hash"
Http = "goravel.http"
Lang = "goravel.lang"
Log = "goravel.log"
Mail = "goravel.mail"
Orm = "goravel.orm"
Process = "goravel.process"
Queue = "goravel.queue"
RateLimiter = "goravel.rate_limiter"
Route = "goravel.route"
Schedule = "goravel.schedule"
Schema = "goravel.schema"
Seeder = "goravel.seeder"
Session = "goravel.session"
Storage = "goravel.storage"
Testing = "goravel.testing"
Validation = "goravel.validation"
View = "goravel.view"
Artisan = "goravel.artisan"
Auth = "goravel.auth"
Cache = "goravel.cache"
Config = "goravel.config"
Crypt = "goravel.crypt"
DB = "goravel.db"
Event = "goravel.event"
Gate = "goravel.gate"
Grpc = "goravel.grpc"
Hash = "goravel.hash"
Http = "goravel.http"
Lang = "goravel.lang"
Log = "goravel.log"
Mail = "goravel.mail"
Orm = "goravel.orm"
Process = "goravel.process"
Queue = "goravel.queue"
RateLimiter = "goravel.rate_limiter"
Route = "goravel.route"
Schedule = "goravel.schedule"
Schema = "goravel.schema"
Seeder = "goravel.seeder"
Session = "goravel.session"
Storage = "goravel.storage"
Testing = "goravel.testing"
Validation = "goravel.validation"
View = "goravel.view"
Notification = "goravel.notification"
)

type Relationship struct {
Expand Down
110 changes: 56 additions & 54 deletions contracts/facades/facades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,63 @@ package facades
import "github.com/goravel/framework/contracts/binding"

const (
Artisan = "Artisan"
Auth = "Auth"
Cache = "Cache"
Config = "Config"
Crypt = "Crypt"
DB = "DB"
Event = "Event"
Gate = "Gate"
Grpc = "Grpc"
Hash = "Hash"
Http = "Http"
Lang = "Lang"
Log = "Log"
Mail = "Mail"
Process = "Process"
Orm = "Orm"
Queue = "Queue"
RateLimiter = "RateLimiter"
Route = "Route"
Schedule = "Schedule"
Schema = "Schema"
Seeder = "Seeder"
Session = "Session"
Storage = "Storage"
Testing = "Testing"
Validation = "Validation"
View = "View"
Artisan = "Artisan"
Auth = "Auth"
Cache = "Cache"
Config = "Config"
Crypt = "Crypt"
DB = "DB"
Event = "Event"
Gate = "Gate"
Grpc = "Grpc"
Hash = "Hash"
Http = "Http"
Lang = "Lang"
Log = "Log"
Mail = "Mail"
Process = "Process"
Orm = "Orm"
Queue = "Queue"
RateLimiter = "RateLimiter"
Route = "Route"
Schedule = "Schedule"
Schema = "Schema"
Seeder = "Seeder"
Session = "Session"
Storage = "Storage"
Testing = "Testing"
Validation = "Validation"
View = "View"
Notification = "Notification"
)

var FacadeToBinding = map[string]string{
Artisan: binding.Artisan,
Auth: binding.Auth,
Cache: binding.Cache,
Config: binding.Config,
Crypt: binding.Crypt,
DB: binding.DB,
Event: binding.Event,
Gate: binding.Gate,
Grpc: binding.Grpc,
Hash: binding.Hash,
Http: binding.Http,
Lang: binding.Lang,
Log: binding.Log,
Mail: binding.Mail,
Orm: binding.Orm,
Process: binding.Process,
Queue: binding.Queue,
RateLimiter: binding.RateLimiter,
Route: binding.Route,
Schedule: binding.Schedule,
Schema: binding.Schema,
Seeder: binding.Seeder,
Session: binding.Session,
Storage: binding.Storage,
Testing: binding.Testing,
Validation: binding.Validation,
View: binding.View,
Artisan: binding.Artisan,
Auth: binding.Auth,
Cache: binding.Cache,
Config: binding.Config,
Crypt: binding.Crypt,
DB: binding.DB,
Event: binding.Event,
Gate: binding.Gate,
Grpc: binding.Grpc,
Hash: binding.Hash,
Http: binding.Http,
Lang: binding.Lang,
Log: binding.Log,
Mail: binding.Mail,
Orm: binding.Orm,
Process: binding.Process,
Queue: binding.Queue,
RateLimiter: binding.RateLimiter,
Route: binding.Route,
Schedule: binding.Schedule,
Schema: binding.Schema,
Seeder: binding.Seeder,
Session: binding.Session,
Storage: binding.Storage,
Testing: binding.Testing,
Validation: binding.Validation,
View: binding.View,
Notification: binding.Notification,
}
3 changes: 3 additions & 0 deletions contracts/foundation/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package foundation

import (
"context"
"github.com/goravel/framework/contracts/notification"

"github.com/goravel/framework/contracts/auth"
"github.com/goravel/framework/contracts/auth/access"
Expand Down Expand Up @@ -166,6 +167,8 @@ type Application interface {
MakeValidation() validation.Validation
// MakeView resolves the view instance.
MakeView() view.View
// MakeNotification resolves the notification instance.
MakeNotification() notification.Notification
// MakeSeeder resolves the seeder instance.
MakeSeeder() seeder.Facade
// MakeWith resolves the given type with the given parameters from the container.
Expand Down
20 changes: 20 additions & 0 deletions contracts/notification/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package notification

type Notification interface {
Send(notifiable Notifiable) error
}

type Notif interface {
// Via Return to the list of channel names
Via(notifiable Notifiable) []string
}

type Channel interface {
// Send sends the given notification to the given notifiable.
Send(notifiable Notifiable, notif interface{}) error
}

type Notifiable interface {
// RouteNotificationFor returns the route notification for the given channel.
RouteNotificationFor(channel string) any
}
5 changes: 5 additions & 0 deletions facades/facades.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package facades

import (
"context"
"github.com/goravel/framework/contracts/notification"

"github.com/goravel/framework/contracts/auth"
"github.com/goravel/framework/contracts/auth/access"
Expand Down Expand Up @@ -150,3 +151,7 @@ func Validation() validation.Validation {
func View() view.View {
return App().MakeView()
}

func Notification() notification.Notification {
return App().MakeNotification()
}
12 changes: 12 additions & 0 deletions foundation/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
contractshttpclient "github.com/goravel/framework/contracts/http/client"
contractslog "github.com/goravel/framework/contracts/log"
contractsmail "github.com/goravel/framework/contracts/mail"
contractsotification "github.com/goravel/framework/contracts/notification"
contractsprocess "github.com/goravel/framework/contracts/process"
contractsqueue "github.com/goravel/framework/contracts/queue"
contractsroute "github.com/goravel/framework/contracts/route"
Expand All @@ -35,6 +36,7 @@ import (
contractstranslation "github.com/goravel/framework/contracts/translation"
contractsvalidation "github.com/goravel/framework/contracts/validation"
contractsview "github.com/goravel/framework/contracts/view"

"github.com/goravel/framework/support/color"
)

Expand Down Expand Up @@ -386,6 +388,16 @@ func (r *Container) MakeView() contractsview.View {
return instance.(contractsview.View)
}

func (r *Container) MakeNotification() contractsotification.Notification {
instance, err := r.Make(facades.FacadeToBinding[facades.Notification])
if err != nil {
color.Errorln(err)
return nil
}

return instance.(contractsotification.Notification)
}

func (r *Container) MakeWith(key any, parameters map[string]any) (any, error) {
return r.make(key, parameters)
}
Expand Down
56 changes: 56 additions & 0 deletions notification/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package notification

import (
"fmt"
"github.com/goravel/framework/contracts/config"
contractsqueuedb "github.com/goravel/framework/contracts/database/db"
contractsmail "github.com/goravel/framework/contracts/mail"
"github.com/goravel/framework/contracts/notification"
contractsqueue "github.com/goravel/framework/contracts/queue"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/notification/channels"
)

type Application struct {
config config.Config
queue contractsqueue.Queue
db contractsqueuedb.DB
mail contractsmail.Mail
}

func NewApplication(config config.Config, queue contractsqueue.Queue, db contractsqueuedb.DB, mail contractsmail.Mail) (*Application, error) {
return &Application{
config: config,
queue: queue,
db: db,
mail: mail,
}, nil
}

// Send a notification.
func (r *Application) Send(notifiable notification.Notifiable, notif notification.Notif) error {
vias := notif.Via(notifiable)
if len(vias) == 0 {
return errors.New("no channels defined for notification")
}

for _, chName := range vias {
ch, ok := GetChannel(chName)
if !ok {
return fmt.Errorf("channel not registered: %s", chName)
}
if chName == "database" {
if databaseChannel, ok := ch.(*channels.DatabaseChannel); ok {
databaseChannel.SetDB(r.db)
}
} else if chName == "mail" {
if mailChannel, ok := ch.(*channels.MailChannel); ok {
mailChannel.SetMail(r.mail)
}
}
if err := ch.Send(notifiable, notif); err != nil {
return fmt.Errorf("channel %s send error: %w", chName, err)
}
}
return nil
}
Loading
Loading