VOOZH about

URL: https://deepwiki.com/hypervel/support/3.4-manager-pattern

⇱ Manager Pattern | hypervel/support | DeepWiki


Loading...
Menu

Manager Pattern

The Manager Pattern provides a driver-based extensibility system for managing multiple implementations of the same service. It enables lazy instantiation, driver caching, custom driver registration, and automatic method forwarding. This pattern is used throughout the support package for services like databases, caches, queues, and Redis connections that require multiple backend implementations.

For information about how managers integrate with facades, see Facade System. For information about how managers are registered with the container, see Service Provider System. For the overall architecture context, see Architecture Overview.


Purpose and Design Goals

The Manager pattern addresses the need for services that support multiple backend implementations ("drivers") while maintaining a consistent API. Key design goals include:

GoalImplementation
Driver AbstractionAbstract base class manages driver lifecycle
Lazy LoadingDrivers created only when first accessed
Instance CachingDriver instances reused across calls
ExtensibilityCustom drivers registered via closures
Convention-basedDriver creation follows naming convention
Transparent APIMethod calls forwarded to default driver

The Manager pattern is essential for any service that needs to support multiple backends (e.g., Redis, Memcached, File cache) without changing application code.

Sources: src/Manager.php13-157


Class Architecture

Manager Base Class Structure


Sources: src/Manager.php13-37

Key Properties

PropertyTypePurpose
$containerContainerInterfaceHyperf DI container for dependency resolution
$configConfigInterfaceConfiguration repository for driver settings
$customCreatorsarrayMap of driver names to custom creator closures
$driversarrayCache of instantiated driver instances

Sources: src/Manager.php14-28


Driver Resolution Flow

Resolution Process


Sources: src/Manager.php49-84

Driver Method

The driver() method is the primary entry point for driver resolution:


Resolution Logic:

  1. Use provided driver name or fall back to getDefaultDriver()
  2. Check if driver already instantiated in $drivers cache
  3. If not cached, call createDriver() to instantiate
  4. Cache the driver instance for subsequent calls
  5. Return the driver

Sources: src/Manager.php49-61

Driver Creation Methods

Driver creation follows a convention-based approach with fallback to custom creators:

MethodPurposeExample
createDriver(string $driver)Main creation logic with conventionsCalled internally by driver()
create{Driver}Driver()Convention-based methodcreateRedisDriver(), createMemcachedDriver()
callCustomCreator(string $driver)Execute registered custom creatorUsed for extended drivers

Convention: For driver name "redis", the manager looks for method createRedisDriver() (using Str::studly() conversion).

Sources: src/Manager.php68-92


Custom Driver Registration

Extending with Custom Drivers


Sources: src/Manager.php99-104

Extension API


Parameters:

  • $driver: Name of the custom driver
  • $callback: Closure that receives ContainerInterface and returns driver instance

Example Usage Pattern:


Sources: src/Manager.php99-104

Custom Creator Execution

The callCustomCreator() method invokes the registered closure:


The closure receives the DI container, enabling full access to application services for driver construction.

Sources: src/Manager.php89-92


Manager Subclass Implementation

Concrete Manager Example


Implementation Requirements

To create a concrete manager, subclasses must:

  1. Implement getDefaultDriver() - Returns the default driver name
  2. Implement driver creation methods - Following naming convention create{Driver}Driver()
  3. Access configuration - Use $this->config for driver-specific settings
  4. Use container - Leverage $this->container for dependency injection

Sources: src/Manager.php42-43


Dynamic Method Forwarding

Magic Method Call

The Manager implements __call() to forward method calls to the default driver:


Sources: src/Manager.php153-156

Implementation


This enables transparent API usage:


Sources: src/Manager.php153-156


Integration with Hyperf Framework

Container and Configuration Integration


Sources: src/Manager.php33-37

Constructor Injection


The manager receives the DI container and resolves the configuration interface. This provides:

  • Access to container for driver dependency injection
  • Configuration reading for driver-specific settings
  • Service resolution in custom driver creators

Sources: src/Manager.php33-37


Cache Management

Driver Cache Operations

MethodPurposeUse Case
getDrivers()Retrieve all cached driversDebugging, inspection
forgetDrivers()Clear entire driver cacheTesting, reconfiguration
setContainer()Replace container instanceTesting, rebinding

Sources: src/Manager.php109-144

Clearing Driver Cache


This method clears all cached driver instances, forcing re-creation on next access. Useful for:

  • Testing scenarios requiring fresh instances
  • Runtime configuration changes
  • Memory management in long-running processes

Sources: src/Manager.php139-144


Usage Patterns in Application Code

Basic Driver Access


Pattern Examples

Default Driver:


Named Driver:


Custom Driver:


Sources: src/Manager.php49-156


Manager Pattern in System Architecture

Managers in the Hypervel Stack


Sources: src/Manager.php13-157


Key Characteristics Summary

CharacteristicDescription
Abstract BaseManagers extend Manager abstract class
Lazy CreationDrivers instantiated only when accessed
Instance CachingDrivers cached in $drivers array
Convention-basedDriver methods follow create{Name}Driver() pattern
ExtensibleCustom drivers via extend() method
Container-awareFull access to DI container
Config-awareConfiguration reading via ConfigInterface
Transparent APIMethod forwarding via __call() magic method
StatelessDriver state managed by driver instances

Sources: src/Manager.php13-157