VOOZH about

URL: https://deepwiki.com/hypervel/redis/8.1-hyperf-integration

⇱ Hyperf Integration | hypervel/redis | DeepWiki


Loading...
Menu

Hyperf Integration

Purpose and Scope

This document explains how the Hypervel Redis library integrates with the Hyperf framework through its ConfigProvider class. It covers the dependency injection override mechanism, the bootstrap registration process, and how the custom RedisPool implementation replaces Hyperf's default pool to enable Laravel-style Redis API transformations.

For information about the Laravel-style transformations enabled by this integration, see Laravel-Style Redis Connection. For configuration of multiple Redis pools, see Multi-Pool Configuration.


Overview

The Hypervel Redis library integrates with Hyperf using a non-invasive approach that leverages Hyperf's built-in configuration provider system. The integration consists of two primary components:

ComponentFilePurpose
ConfigProvidersrc/ConfigProvider.phpRegisters dependency overrides with Hyperf's DI container
RedisPoolsrc/RedisPool.phpCustom pool implementation that creates RedisConnection instances

The integration strategy centers on overriding Hyperf's default RedisPool class without requiring any modifications to the Hyperf framework itself. This ensures compatibility across Hyperf versions and allows the library to be added or removed cleanly.

Sources: src/ConfigProvider.php1-19 src/RedisPool.php1-16


ConfigProvider Class

Class Structure and Invocation

The ConfigProvider class is the entry point for Hyperf framework integration. It follows Hyperf's configuration provider pattern by implementing the __invoke() method:

namespace: Hypervel\Redis
class: ConfigProvider
method: __invoke(): array

The class is automatically discovered by Hyperf during application bootstrap through Composer's PSR-4 autoloading mechanism and the extra.hyperf.config section in the library's composer.json.

Sources: src/ConfigProvider.php9-18

Dependency Override Registration

The __invoke() method returns a configuration array with a single key: dependencies. This array defines dependency injection bindings that override Hyperf's default implementations:

'dependencies' => [
 HyperfRedisPool::class => RedisPool::class,
]

This configuration tells Hyperf's DI container: "Whenever any code requests an instance of Hyperf\Redis\Pool\RedisPool, provide an instance of Hypervel\Redis\RedisPool instead."

Key aspects of this override:

AspectDescription
ScopeGlobal - affects all Redis pool instantiations
Original ClassHyperf\Redis\Pool\RedisPool
Replacement ClassHypervel\Redis\RedisPool
InheritanceReplacement extends original, maintaining interface compatibility
ImpactChanges connection creation behavior throughout the application

Sources: src/ConfigProvider.php14-15 src/RedisPool.php10


Bootstrap Integration Process

Discovery and Registration Flow

The following diagram illustrates how Hyperf discovers and registers the ConfigProvider during application startup:


Sources: src/ConfigProvider.php11-18

Configuration Provider Pattern

The configuration provider pattern used by Hyperf allows libraries to self-register their services, middleware, listeners, and dependency overrides. The ConfigProvider returns a structured array that Hyperf processes during bootstrap.

Standard ConfigProvider array structure:

[
 'dependencies' => [...], // DI bindings
 'commands' => [...], // CLI commands
 'annotations' => [...], // Annotation scanning
 'listeners' => [...], // Event listeners
 'publish' => [...], // Publishable assets
]

The Hypervel Redis library uses only the dependencies key to register its single override, keeping the integration minimal and focused.

Sources: src/ConfigProvider.php13-16


Dependency Override Mechanism

Code Entity Mapping

This diagram maps the dependency override mechanism to specific code entities:


Sources: src/ConfigProvider.php14-15 src/RedisPool.php12-15

Override Impact

The dependency override affects three primary instantiation paths:

1. Direct Container Resolution

When code explicitly resolves HyperfRedisPool::class from the container:


2. Constructor Injection

When classes type-hint HyperfRedisPool in their constructors:


3. Hyperf's Pool Factory

The most important impact is on Hyperf's PoolFactory::make() method, which is used internally to create pool instances. When the factory creates a pool, the DI container resolves the binding and instantiates Hypervel\Redis\RedisPool instead of the default.

Sources: src/RedisPool.php10


Custom RedisPool Implementation

Inheritance and Compatibility

The Hypervel\Redis\RedisPool class extends Hyperf\Redis\Pool\RedisPool, ensuring full compatibility with Hyperf's pooling infrastructure:

Inheritance chain:
Hypervel\Redis\RedisPool 
 extends Hyperf\Redis\Pool\RedisPool
 extends Hyperf\Pool\SimplePool\Pool
 implements Hyperf\Contract\PoolInterface

This inheritance strategy means:

  • All parent class methods remain available
  • The pool integrates seamlessly with Hyperf's connection pooling system
  • Only the connection creation behavior is customized

Sources: src/RedisPool.php10

Connection Creation Override

The sole customization in RedisPool is the createConnection() method:


This method override is the critical integration point that enables Laravel-style API transformations. Instead of creating Hyperf's default RedisConnection, it instantiates Hypervel\Redis\RedisConnection, which provides the transformation layer.

Method signature analysis:

ElementValueNotes
VisibilityprotectedCalled internally by parent pool
Return typeConnectionInterfaceHyperf's connection interface
ParametersNoneUses inherited properties
Dependencies$this->container, $this, $this->configPassed to connection constructor

Sources: src/RedisPool.php12-15


Runtime Behavior

Connection Lifecycle with Custom Pool

The following diagram shows how connections are created and managed at runtime after the ConfigProvider has registered the override:


Sources: src/RedisPool.php12-15

Integration Points Summary

The ConfigProvider enables Laravel-style Redis operations through a chain of integration points:

LayerComponentRole
BootstrapConfigProvider::__invoke()Registers dependency override
ContainerDI ContainerResolves HyperfRedisPoolRedisPool
PoolingRedisPool::createConnection()Creates RedisConnection instances
TransformationRedisConnectionProvides Laravel-style API (see Laravel-Style Redis Connection)
Executionphpredis extensionExecutes native Redis commands

This layered approach ensures that:

  1. No framework modifications are required
  2. The integration is transparent to application code
  3. All Hyperf pooling features remain functional
  4. Laravel-style transformations are consistently applied

Sources: src/ConfigProvider.php1-19 src/RedisPool.php1-16


Import Statements and Namespace Resolution

ConfigProvider Dependencies

The ConfigProvider class has minimal dependencies:


The import uses an alias (HyperfRedisPool) to distinguish Hyperf's pool class from Hypervel's RedisPool class within the same namespace. This aliasing is necessary because both classes share the same short name.

Sources: src/ConfigProvider.php5-7

RedisPool Dependencies

The RedisPool class requires two imports:


  • ConnectionInterface: Return type for createConnection() method
  • HyperfRedisPool: Parent class reference

Sources: src/RedisPool.php7-8


Integration Verification

Confirming the Override is Active

To verify that the ConfigProvider has successfully registered the override, you can inspect the DI container at runtime:


Connection Type Verification

To verify that RedisConnection instances are being created:


Sources: src/RedisPool.php12-15


Design Rationale

Why Override at the Pool Level

The library overrides RedisPool rather than RedisConnection directly because:

  1. Hyperf's architecture: Hyperf instantiates pools, which then create connections
  2. Centralized control: Pool override affects all connection creation paths
  3. Inheritance compatibility: Extending the pool maintains all Hyperf pooling features
  4. Minimal surface area: Only one dependency override is required

Alternative Approaches Not Used

ApproachReason Not Used
Direct connection overrideHyperf doesn't resolve connections through DI container
Factory overrideWould require more extensive changes to Hyperf's instantiation flow
Wrapper patternWould add unnecessary complexity and performance overhead
Event-based replacementWould be less reliable and harder to debug

The chosen approach—override at the pool level—provides the cleanest integration with minimal complexity.

Sources: src/ConfigProvider.php14-15 src/RedisPool.php10-15


Summary

The Hyperf integration is achieved through two simple but strategically placed classes:

  1. ConfigProvider (src/ConfigProvider.php): Registers a single dependency override during application bootstrap
  2. RedisPool (src/RedisPool.php): Extends Hyperf's pool to create RedisConnection instances

This integration enables Laravel-style Redis API transformations (documented in Laravel-Style Redis Connection) throughout the application without requiring any modifications to the Hyperf framework itself. The approach is non-invasive, maintainable, and compatible across Hyperf versions.

Sources: src/ConfigProvider.php1-19 src/RedisPool.php1-16

Refresh this wiki

On this page