VOOZH about

URL: https://deepwiki.com/hypervel/notifications/3.1-channel-manager

⇱ Channel Manager | hypervel/notifications | DeepWiki


Loading...
Menu

Channel Manager

Purpose and Scope

The ChannelManager is the central dispatcher and factory for notification delivery channels in the hypervel/notifications package. It implements the Manager pattern to resolve, instantiate, and cache channel drivers while providing extensibility through custom driver registration. The manager also handles object pooling for expensive drivers and manages request-scoped configuration state through Hyperf's context system.

For information about how notifications are orchestrated and sent, see Notification Sender. For details on individual channel implementations, see Notification Channels.

Sources: src/ChannelManager.php1-233

System Role

The ChannelManager serves as the primary entry point for notification dispatch. It implements both DispatcherContract and FactoryContract interfaces, allowing it to send notifications directly or provide channel instances on demand.


Driver Resolution and Delegation Flow

The manager delegates notification sending to NotificationSender instances, injecting required dependencies from the container. The send() method at src/ChannelManager.php54-62 creates a sender with the bus dispatcher, event dispatcher, and current locale. The sendNow() method at src/ChannelManager.php67-75 provides an immediate delivery path with optional channel filtering.

Sources: src/ChannelManager.php54-83

Driver Resolution Strategy

The ChannelManager resolves channel drivers through a three-tier strategy with clear precedence. This design allows custom extensions to override built-in implementations while falling back to container resolution for simple cases.


Resolution Tiers

Tier 1: Custom Creators

Custom creators registered via extend() take highest precedence. The check at src/ChannelManager.php126 determines if a custom creator exists. If found, the manager calls it via callCustomCreator().


The extend() method at src/ChannelManager.php163-170 allows registration of custom drivers with optional pooling. When the $poolable parameter is true, the driver name is added to the poolables array, enabling automatic pool proxy wrapping.

Tier 2: Built-in Factory Methods

If no custom creator exists, the manager checks for a factory method named create{Driver}Driver using string manipulation at src/ChannelManager.php137 The method name is generated using Str::studly() to convert driver names like "slack" to "Slack".

Tier 3: Container Resolution

If no factory method exists, the manager attempts direct container resolution at src/ChannelManager.php140-142 This allows fully-qualified class names to be used as driver identifiers. If the class exists, it's resolved from the dependency injection container.

If all strategies fail, an InvalidArgumentException is thrown at src/ChannelManager.php144

Sources: src/ChannelManager.php122-156 src/ChannelManager.php163-170

Built-in Channel Drivers

The ChannelManager provides factory methods for four built-in channel types. Each factory method simply resolves the channel instance from the dependency injection container.

Driver NameFactory MethodChannel ClassLine Reference
databasecreateDatabaseDriver()DatabaseChannelsrc/ChannelManager.php88-91
broadcastcreateBroadcastDriver()BroadcastChannelsrc/ChannelManager.php96-99
mailcreateMailDriver()MailChannelsrc/ChannelManager.php104-107
slackcreateSlackDriver()SlackNotificationRouterChannelsrc/ChannelManager.php112-115

Default Driver

The default channel driver is "mail", set at src/ChannelManager.php29 This default can be overridden through context management (see Context Management section below).

Sources: src/ChannelManager.php29 src/ChannelManager.php88-115

Object Pooling Layer

The ChannelManager implements object pooling for expensive channel drivers to improve performance in high-throughput scenarios. Pooling is managed through the HasPoolProxy trait from the hypervel/object-pool package.


Pool Configuration

Three properties control pooling behavior:

Pool Proxy Creation

When a poolable driver is created, the manager wraps it in a pool proxy at src/ChannelManager.php128-132 or src/ChannelManager.php147-152 The proxy intercepts method calls and manages instance lifecycle, reusing instances across requests within the same worker process.

Custom Driver Pooling

Custom drivers can opt into pooling by passing $poolable = true to the extend() method at src/ChannelManager.php163 Pool configuration can be set using setPoolConfig() at src/ChannelManager.php177-182


Sources: src/ChannelManager.php17 src/ChannelManager.php24 src/ChannelManager.php39 src/ChannelManager.php44 src/ChannelManager.php49 src/ChannelManager.php163-170 src/ChannelManager.php177-190

Context Management

The ChannelManager uses Hyperf's context system to manage request-scoped configuration without global state mutation. This allows different execution contexts (HTTP requests, queue jobs, CLI commands) to maintain independent settings.


Default Channel Management

The default channel determines which driver is used when no explicit channel is specified. Three methods manage this setting:

Locale Management

Locale settings control notification internationalization. Two methods manage locale:

  • locale(string $locale) at src/ChannelManager.php219-224: Sets the default locale in context using the key "__notifications.defaultLocale". Returns $this for method chaining.
  • getLocale() at src/ChannelManager.php229-232: Retrieves the locale from context, falling back to the $locale property (default null).

Context Isolation

Context values are isolated per execution context. In a typical HTTP application:

  • Each request has its own context
  • Queue jobs have separate contexts
  • CLI commands have their own context

This isolation prevents configuration changes in one context from affecting others, making the system safe for concurrent execution.

Sources: src/ChannelManager.php29 src/ChannelManager.php34 src/ChannelManager.php195-232

Class Structure and Inheritance

The ChannelManager extends the abstract Manager class from hypervel/support and uses the HasPoolProxy trait for object pooling support.


Interface Implementations

  • DispatcherContract: Provides send() and sendNow() methods for notification dispatch.
  • FactoryContract: Provides channel() method for retrieving channel instances.

Trait Usage

  • HasPoolProxy: Adds object pooling capabilities, including createPoolProxy() and pool management methods.

Sources: src/ChannelManager.php22-24

Usage Patterns

Basic Channel Access


Sending Notifications


Changing Default Channel


Setting Locale


Registering Custom Channels


Sources: src/ChannelManager.php54-83 src/ChannelManager.php163-170 src/ChannelManager.php177-182 src/ChannelManager.php195-232