VOOZH about

URL: https://deepwiki.com/hypervel/cache/5.1-framework-integration

⇱ Framework Integration | hypervel/cache | DeepWiki


Loading...
Menu

Framework Integration

Purpose and Scope

This document covers how the hypervel/cache package integrates with the Hyperf framework through its registration and bootstrapping mechanisms. It explains the package's entry points, dependency injection configuration, and how various components are wired together during application startup.

For information about the specific console commands available after registration, see Console Commands. For details about the event system and available events, see Event System. For the global helper function API, see Helper Functions.

Package Structure and Registration

The package uses Hyperf's standard configuration provider pattern to register all its components with the framework container.

Composer Package Definition

The package is defined in composer.json1-53 with the following key integration points:

ConfigurationValuePurpose
Package Namehypervel/cacheComposer identifier
TypelibraryPackage classification
Autoload PSR-4Hypervel\Cache\src/Class autoloading namespace
Autoload Filessrc/Functions.phpGlobal helper functions
PHP Version^8.2Minimum PHP requirement
Hyperf Config HookHypervel\Cache\ConfigProviderFramework registration entry point

The extra.hyperf.config section at composer.json46-48 tells Hyperf to invoke the ConfigProvider class during application bootstrap.

Sources: composer.json1-53

ConfigProvider Registration Entry Point

The ConfigProvider class at src/ConfigProvider.php14-41 serves as the central registration point for all framework integrations. When invoked by Hyperf during bootstrap, it returns an array defining:

  • dependencies: Container bindings for dependency injection
  • listeners: Event listeners to register
  • commands: Console commands to make available
  • publish: Configuration files that can be published to the application

Sources: composer.json46-48 src/ConfigProvider.php14-41

Dependency Injection Bindings

The ConfigProvider registers two key container bindings at src/ConfigProvider.php19-22:

Factory Interface Binding


This binding maps the Hypervel\Cache\Contracts\Factory interface to the concrete CacheManager implementation. Applications can type-hint Factory in constructors or use make(Factory::class) to receive a CacheManager instance.

Sources: src/ConfigProvider.php20

Store Interface Binding


This binding resolves the Hypervel\Cache\Contracts\Store interface to the default cache driver configured in the application. It:

  1. Retrieves the CacheManager from the container
  2. Calls driver() with no arguments to get the default store
  3. Returns the store instance (e.g., RedisStore, SwooleStore)

The default driver is determined by the CACHE_DRIVER environment variable or the cache.default configuration value.

Sources: src/ConfigProvider.php21 publish/cache.php22

Dependency Injection Flow


Sources: src/ConfigProvider.php19-22

Event Listener Registration

The package registers event listeners at src/ConfigProvider.php23-26 to hook into framework lifecycle events.

Registered Listeners

Listener ClassEventPurpose
CreateSwooleTableBeforeServerStartInitialize Swoole tables before server starts
CreateTimerFramework timing eventsCreate interval timers for cache maintenance

CreateSwooleTable Listener

The CreateSwooleTable listener at src/Listeners/CreateSwooleTable.php10-25 listens for the BeforeServerStart event. When triggered:

  1. It iterates through all cache stores configured in the application
  2. For each store using the swoole driver, it retrieves the configured table name
  3. It calls SwooleTableManager::get() to initialize the Swoole table with the specified configuration

This ensures that Swoole shared memory tables are created before any workers start, as Swoole tables must be initialized in the master process before forking workers.


Sources: src/ConfigProvider.php23-26 src/Listeners/CreateSwooleTable.php10-25

Console Command Registration

The ConfigProvider registers console commands at src/ConfigProvider.php27-30 to provide administrative tools for cache management.

Registered Commands

Command ClassCommand NamePurpose
ClearCommandcache:clearFlush all or specific cache stores
PruneDbExpiredCommandcache:prune-db-expiredRemove expired entries from database cache

These commands become available via the Hyperf CLI after the package is installed. For detailed documentation of each command's functionality and options, see Console Commands.

Command Registration Flow

The registration process makes commands discoverable to the Hyperf console kernel:

  1. ConfigProvider returns command class names in the commands array
  2. Hyperf's console component reads this array during bootstrap
  3. Commands are instantiated and registered with the console kernel
  4. Commands become available via php bin/hyperf.php <command-name>

Sources: src/ConfigProvider.php27-30 src/Console/PruneDbExpiredCommand.php1-66

Configuration Publishing

The package provides a publishable configuration file through the publish section at src/ConfigProvider.php31-38

Publication Configuration

PropertyValueDescription
idconfigPublication identifier
description"The config for cache."Human-readable description
source__DIR__ . '/../publish/cache.php'Source file location
destinationBASE_PATH . '/config/autoload/cache.php'Target location in application

Publishing Process

Users publish the configuration file by running:


This copies publish/cache.php1-111 to the application's config/autoload/ directory, where it can be customized.

Configuration Structure

The published configuration file provides:

SectionLinesPurpose
Default Driverpublish/cache.php22Sets cache.default from CACHE_DRIVER env var
Store Configurationspublish/cache.php37-83Defines settings for each driver type
Swoole Tablespublish/cache.php91-97Configures shared memory tables
Cache Prefixpublish/cache.php110Application-specific key prefix from CACHE_PREFIX env var

Sources: src/ConfigProvider.php31-38 publish/cache.php1-111

Global Functions Autoloading

The package automatically loads global helper functions through Composer's files autoloading at composer.json27-29 This includes src/Functions.php which provides the cache() helper function for convenient cache access throughout the application.

The global function is available immediately after Composer's autoloader runs, before any framework-specific initialization. For detailed documentation of the helper function's usage patterns, see Helper Functions.

Sources: composer.json27-29

Complete Bootstrap Flow

The following diagram illustrates the complete integration flow from package installation to runtime usage:


Sources: composer.json1-53 src/ConfigProvider.php14-41 src/Listeners/CreateSwooleTable.php10-25

Environment Variable Configuration

The package respects several environment variables that control cache behavior. These are referenced in publish/cache.php1-111 and can be set in the application's .env file:

Environment VariableConfiguration KeyDefaultPurpose
CACHE_DRIVERcache.defaultarrayDefault cache store to use
CACHE_PREFIXcache.prefix{app_name}_cacheKey prefix to avoid collisions
DB_CACHE_CONNECTIONcache.stores.database.connectionDB_CONNECTIONDatabase connection for cache
DB_CACHE_TABLEcache.stores.database.tablecacheTable name for cache entries
DB_CACHE_LOCK_CONNECTIONcache.stores.database.lock_connectionnullSeparate connection for locks
DB_CACHE_LOCK_TABLEcache.stores.database.lock_tablecache_locksTable name for lock entries

Sources: publish/cache.php22 publish/cache.php76-80 publish/cache.php110

Integration with Hyperf Configuration System

The package follows Hyperf's configuration conventions:

  1. Autoload Configuration: Configuration files in config/autoload/ are automatically loaded and merged
  2. Environment Variable Support: Uses env() helper for environment-aware configuration
  3. Configuration Provider Pattern: Implements the standard Hyperf configuration provider interface
  4. Service Registration: Uses the container's dependencies array for service bindings

This allows the cache package to integrate seamlessly with Hyperf's configuration and dependency injection systems without requiring manual registration or bootstrap code in the application.

Sources: src/ConfigProvider.php14-41 publish/cache.php1-111