VOOZH about

URL: https://deepwiki.com/hypervel/config/5.2-repository-class

⇱ Repository Class | hypervel/config | DeepWiki


Loading...
Menu

Repository Class

The Repository class is the central configuration storage mechanism in the hypervel/config package. It acts as the runtime container for all merged configuration data and provides the primary API for reading and writing configuration values. This class implements both the Hypervel\Config\Contracts\Repository interface and PHP's ArrayAccess interface, enabling flexible access patterns.

For information about accessing the Repository through the global helper, see Configuration Function. For details on type-safe configuration retrieval methods, see Type-Safe Configuration Access.

Class Structure and Responsibilities

The Repository class src/Repository.php14 serves as a facade over a simple associative array, delegating array operations to Hyperf\Collection\Arr for dot-notation key access. It provides:

  • Configuration Storage: Maintains configuration data in a protected $items array
  • Read Operations: Methods for retrieving configuration values with default fallbacks
  • Write Operations: Methods for setting and modifying configuration at runtime
  • Type Safety: Specialized getter methods that validate return types
  • Array Manipulation: Convenience methods for array-based configuration
  • Array Access: PHP ArrayAccess implementation for array-like syntax
  • Extension Points: Macroable trait and callback mechanisms

Sources: src/Repository.php1-255

Configuration Storage Mechanism

The Repository stores all configuration data in a protected $items array src/Repository.php26 This array is populated by ConfigFactory during application bootstrap with the merged configuration from all sources (providers, files, etc.).

Internal Data Structure

The configuration data is stored as a nested associative array. Keys can be accessed using dot notation (e.g., app.name) which is translated to nested array access by the Hyperf\Collection\Arr utility.


Sources: src/Repository.php26 src/Repository.php35 src/Repository.php47 src/Repository.php167

Reading Configuration

The Repository provides multiple methods for retrieving configuration values, all delegating to Hyperf\Collection\Arr::get() for dot-notation support.

has() Method

The has() method src/Repository.php33-36 checks if a configuration key exists:


Behavior: Returns true if the key exists (even if the value is null), false otherwise. Uses Arr::has() internally src/Repository.php35

get() Method

The get() method src/Repository.php41-48 is the primary retrieval method:


Key Features:

  • Single Key: When $key is a string, retrieves the value at that key path
  • Multiple Keys: When $key is an array, delegates to getMany() src/Repository.php43-44
  • Default Value: Returns $default if the key doesn't exist src/Repository.php47
  • Dot Notation: Supports nested keys like 'database.connections.mysql.host'

getMany() Method

The getMany() method src/Repository.php53-66 retrieves multiple configuration values at once:


Input Formats:

  • With Defaults: ['key1' => 'default1', 'key2' => 'default2']
  • Without Defaults: ['key1', 'key2'] (numeric keys, defaults to null)

The method normalizes the input src/Repository.php58-60 and returns an associative array mapping keys to their values src/Repository.php62

all() Method

The all() method src/Repository.php202-205 returns the entire configuration array:


Use Case: Useful for debugging or when full configuration export is needed.


Sources: src/Repository.php33-66 src/Repository.php202-205

Writing Configuration

Configuration values can be modified at runtime using the set() method and specialized array manipulation methods.

set() Method

The set() method src/Repository.php162-173 writes configuration values:


Supported Formats:

  • Single Value: set('app.name', 'NewApp')
  • Multiple Values: set(['app.name' => 'NewApp', 'app.env' => 'local'])

Process:

  1. Normalizes input to key-value pairs src/Repository.php164
  2. Iterates through each pair src/Repository.php166
  3. Uses Arr::set() to update nested values src/Repository.php167
  4. Triggers the afterSettingCallback if registered src/Repository.php170-172

Array Manipulation Methods

prepend() Method

The prepend() method src/Repository.php178-185 adds a value to the beginning of an array configuration:


Process:

  1. Retrieves existing array (defaults to empty array) src/Repository.php180
  2. Prepends value using array_unshift() src/Repository.php182
  3. Sets the modified array back src/Repository.php184

push() Method

The push() method src/Repository.php190-197 appends a value to an array configuration:


Process:

  1. Retrieves existing array (defaults to empty array) src/Repository.php192
  2. Appends value using array assignment src/Repository.php194
  3. Sets the modified array back src/Repository.php196

Sources: src/Repository.php162-197

ArrayAccess Implementation

The Repository implements PHP's ArrayAccess interface src/Repository.php14 enabling array-like syntax for configuration access.

ArrayAccess Methods

MethodImplementationDescription
offsetExists($key)src/Repository.php220-223Delegates to has($key)
offsetGet($key)src/Repository.php230-233Delegates to get($key)
offsetSet($key, $value)src/Repository.php241-244Delegates to set($key, $value)
offsetUnset($key)src/Repository.php251-254Delegates to set($key, null)

Usage Examples


Note: The offsetUnset() implementation sets the value to null rather than removing the key from the array src/Repository.php253


Sources: src/Repository.php220-254

Callback Mechanism

The Repository supports registering a callback that executes after any set() operation. This enables reactive patterns such as cache invalidation, event broadcasting, or logging.

afterSettingCallback Property

The callback is stored in a static property src/Repository.php21:


Static Scope: The callback is shared across all Repository instances, meaning only one callback can be active globally.

Registering a Callback

Use the afterSettingCallback() method src/Repository.php210-213 to register or clear the callback:


Parameters:

  • $callback: A Closure that accepts an array of key-value pairs that were set
  • Pass null to clear the callback

Callback Invocation

When set() is called, the callback is invoked after all values have been updated src/Repository.php170-172:


The callback receives the array of keys that were set, in the format ['key' => 'value', ...].

Usage Pattern


Sources: src/Repository.php21 src/Repository.php170-173 src/Repository.php210-213

Macroable Trait

The Repository uses the Hyperf\Macroable\Macroable trait src/Repository.php16 enabling dynamic method registration at runtime.

Capabilities

The Macroable trait allows external code to add custom methods to the Repository class without modifying its source:


Use Cases

  • Domain-Specific Accessors: Add methods like getDatabaseConfig() or getCacheConfig()
  • Computed Values: Create methods that derive values from multiple configuration keys
  • Validation: Add methods that validate configuration state
  • Serialization: Custom export formats beyond all()

Sources: src/Repository.php16

Complete Method Reference

Read Operations

MethodReturn TypeDescription
has(string $key)boolCheck if key exists
get(array|string $key, mixed $default = null)mixedRetrieve value(s) with default
getMany(array $keys)arrayRetrieve multiple values
all()arrayGet entire configuration array
string(string $key, mixed $default = null)stringType-safe string retrieval
integer(string $key, mixed $default = null)intType-safe integer retrieval
float(string $key, mixed $default = null)floatType-safe float retrieval
boolean(string $key, mixed $default = null)boolType-safe boolean retrieval
array(string $key, mixed $default = null)arrayType-safe array retrieval

Write Operations

MethodReturn TypeDescription
set(array|string $key, mixed $value = null)voidSet value(s)
prepend(string $key, mixed $value)voidPrepend to array configuration
push(string $key, mixed $value)voidAppend to array configuration

ArrayAccess Implementation

MethodReturn TypeDescription
offsetExists(mixed $key)boolArray existence check
offsetGet(mixed $key)mixedArray read access
offsetSet(mixed $key, mixed $value)voidArray write access
offsetUnset(mixed $key)voidArray unset (sets to null)

Utility Methods

MethodReturn TypeDescription
afterSettingCallback(?Closure $callback)voidRegister post-set callback

Sources: src/Repository.php1-255

Integration with Configuration System

The Repository is created by ConfigFactory during application bootstrap and registered with the dependency injection container as the implementation of Hypervel\Config\Contracts\Repository.


Sources: src/Repository.php14 src/Repository.php26