VOOZH about

URL: https://deepwiki.com/hypervel/config/9.4-configuration-callbacks

⇱ Configuration Callbacks | hypervel/config | DeepWiki


Loading...
Menu

Configuration Callbacks

Purpose and Scope

This document describes the configuration callback mechanism provided by the Repository class that allows applications to register reactive handlers that execute automatically after configuration values are modified. This enables patterns such as cache invalidation, event broadcasting, or configuration validation that need to respond to configuration changes at runtime.

For information about basic configuration reading and writing operations, see Repository Class. For information about array manipulation operations that also trigger callbacks, see Type-Safe Configuration Access.

Sources: src/Repository.php1-255


Overview

The callback mechanism is implemented through a static closure property in the Repository class that, when registered, is automatically invoked after any call to the set() method. This provides a centralized hook point for reacting to configuration changes without requiring modifications to calling code.

The system supports a single global callback that receives information about all modified keys, making it suitable for operations that need to track all configuration changes in one location.

Sources: src/Repository.php19-21 src/Repository.php207-213


Callback Registration

The afterSettingCallback Method

The Repository class provides the afterSettingCallback() method to register or clear the callback:


This method accepts either a Closure to register a new callback or null to clear any existing callback. The callback is stored in the static property $afterSettingCallback.

Key Characteristics:

PropertyDescription
ScopeStatic - shared across all Repository instances
StorageRepository::$afterSettingCallback property
SignatureClosure or null
RegistrationVia afterSettingCallback() method
ReplacementEach call replaces the previous callback

Sources: src/Repository.php19-21 src/Repository.php207-213


Callback Execution Flow

Trigger Points

The registered callback is invoked automatically by the set() method after modifying configuration values. All operations that modify configuration ultimately flow through set(), ensuring consistent callback behavior.


Diagram: Configuration Callback Execution Flow

Sources: src/Repository.php162-173 src/Repository.php176-197 src/Repository.php238-243


Callback Signature and Parameters

Parameter Structure

The callback receives a single parameter: an associative array containing all keys that were modified during the set() operation. The array structure is:


Parameter Details:

AspectDescription
Parameter Count1
Parameter Typearray<string, mixed>
Key FormatDot-notation configuration keys
Value FormatThe new values that were set
Batch SupportMultiple keys if set() was called with an array

Implementation in set() Method

The callback invocation occurs after all values have been set using Arr::set():


Sources: src/Repository.php164-172


Code Entity Mapping

Callback-Related Components


Diagram: Callback Component Structure

Sources: src/Repository.php14-255


Use Cases and Patterns

Cache Invalidation

The most common use case is invalidating caches when configuration changes:


Configuration Change Logging

Track all configuration modifications for audit purposes:


Event Broadcasting

Broadcast configuration changes to other parts of the system:


Validation

Perform post-modification validation:


Sources: src/Repository.php207-213


Implementation Characteristics

Static Storage Implications

The callback is stored in a static property, which has specific implications:

CharacteristicImplication
Shared StateAll Repository instances share the same callback
PersistenceCallback persists across multiple repository instances
Single CallbackOnly one callback can be registered at a time
Global ScopeAffects all configuration changes application-wide

Thread Safety Considerations

Since Hyperf applications may run in Swoole coroutine contexts:

  • The static callback is shared across all coroutines
  • Setting the callback affects all concurrent requests
  • Consider whether per-request callbacks are needed for your use case
  • Use coroutine-safe patterns when accessing resources in callbacks

Sources: src/Repository.php19-21


Callback Triggering Methods

Direct Modification Methods


Diagram: Methods That Trigger Callbacks

Methods Summary:

MethodLinesTriggers CallbackNotes
set()162-173YesPrimary modification method
prepend()178-185YesCalls set() internally
push()190-197YesCalls set() internally
offsetSet()241-243YesArrayAccess interface, calls set()
get()41-48NoRead-only operation
has()33-36NoRead-only operation
offsetUnset()251-254YesSets value to null via set()

Sources: src/Repository.php162-173 src/Repository.php176-197 src/Repository.php238-254


Clearing the Callback

Removing a Registered Callback

To stop callback execution, pass null to the afterSettingCallback() method:


After clearing, subsequent set() operations will not trigger any callback until a new one is registered.

Conditional Execution

The callback is only invoked if it is not null:


This check occurs on every set() operation, ensuring efficient no-op behavior when no callback is registered.

Sources: src/Repository.php170-172 src/Repository.php207-213


Integration with ArrayAccess

Callback Triggering via Array Syntax

The Repository class implements ArrayAccess, allowing array-style configuration modification:


The offsetSet() method delegates to set(), ensuring callbacks are triggered:


Similarly, offsetUnset() triggers callbacks by calling set() with null:


Sources: src/Repository.php238-254


Batch Operations

Multiple Keys in Single Call

When set() is called with an array of keys, the callback receives all modified keys:


This enables efficient batch processing in the callback rather than handling each key individually.

Internal Implementation

The set() method processes all keys in a loop before invoking the callback:


Sources: src/Repository.php164-172


Limitations and Considerations

Single Callback Constraint

Only one callback can be registered at a time. Registering a new callback replaces the previous one:


Workaround Pattern:

To support multiple behaviors, compose them within a single callback:


No Read Callbacks

The callback system only triggers on write operations (set() and related methods). Read operations (get(), has()) do not trigger callbacks.

Exception Handling

Exceptions thrown within the callback will propagate to the caller of set():


Consider implementing error handling within the callback to prevent disrupting configuration updates.

Sources: src/Repository.php19-21 src/Repository.php162-173 src/Repository.php207-213


Summary

The configuration callback mechanism provides a centralized, reactive pattern for responding to configuration changes. Key points:

AspectDetails
RegistrationVia afterSettingCallback(?Closure $callback)
StorageStatic property shared across all instances
TriggerAutomatic on set(), prepend(), push(), offsetSet(), offsetUnset()
ParametersArray of modified keys and their new values
LimitationSingle callback only; replaces previous registration
Use CasesCache invalidation, logging, event broadcasting, validation

The mechanism integrates seamlessly with all configuration modification operations, including array access syntax, ensuring consistent behavior regardless of how configuration is updated.

Sources: src/Repository.php1-255

Refresh this wiki

On this page