VOOZH about

URL: https://deepwiki.com/hypervel/config/3-quick-start-guide

⇱ Quick Start Guide | hypervel/config | DeepWiki


Loading...
Menu

Quick Start Guide

This guide provides a concise walkthrough of basic configuration usage in the hypervel/config package. For installation instructions, see page 2. For detailed API documentation, see pages 5.1-5.3.


Prerequisites

Before using the configuration system, ensure the package is installed (see page 2). The global config() function will be automatically available after installation.

Sources: src/Functions.php1-32


Configuration Files Overview

Configuration values are stored in PHP files that return arrays. The standard location is the config/ directory:

config/
├── hyperf.php # Root configuration file
└── autoload/ # Auto-loaded configuration files
 ├── app.php
 ├── database.php
 └── ...

Example configuration file:


Configuration keys use dot notation to access nested values. For example, config('app.database.host') retrieves the host value from the nested array above.

Sources: Referenced in system architecture diagrams


The config() Function

The global config() function is the primary interface for configuration access. It is defined in src/Functions.php18-31 and operates in three modes based on the arguments provided.

Sources: src/Functions.php18-31

Three Usage Modes


Diagram: config() Function Execution Flow

This diagram shows how the config() function resolves to different code paths based on the argument type, mapping directly to the implementation in src/Functions.php and the underlying Repository methods.

Sources: src/Functions.php18-31 src/Repository.php41-48 src/Repository.php162-173

Mode 1: Get Repository Instance

Calling config() without arguments returns the Repository instance:


The function resolves the Repository from the DI container via ApplicationContext::getContainer()->get(ConfigContract::class).

Sources: src/Functions.php20-24

Mode 2: Get Configuration Value

Pass a string key to retrieve a specific configuration value:


The function uses dot notation to access nested arrays via Arr::get(). If the key doesn't exist, the default value is returned (or null if no default is specified).

Sources: src/Functions.php30 src/Repository.php41-48

Mode 3: Set Configuration Values

Pass an array to set one or more configuration values:


This calls Repository::set() internally. The changes persist for the current request lifecycle only.

Sources: src/Functions.php26-28 src/Repository.php162-173


Common Usage Patterns

Reading Configuration


Sources: src/Functions.php18-31 src/Repository.php33-36 src/Repository.php41-48

Setting Configuration at Runtime


Sources: src/Functions.php26-28 src/Repository.php162-173

Using Repository Methods Directly


Sources: src/Repository.php33-36 src/Repository.php41-48 src/Repository.php53-66 src/Repository.php202-205


Type-Safe Configuration Access

The Repository class provides type-safe getter methods that validate the configuration value type at runtime:


If the configuration value is not of the expected type, an InvalidArgumentException is thrown with a descriptive error message.

Sources: src/Repository.php73-84 src/Repository.php91-102 src/Repository.php127-138 src/Repository.php109-120 src/Repository.php146-157


Working with Array Configuration

The Repository provides methods for manipulating array-based configuration values:


Both prepend() and push() methods automatically retrieve the current array value, modify it, and call set() to update the configuration.

Sources: src/Repository.php178-185 src/Repository.php190-197


ArrayAccess Interface

The Repository implements PHP's ArrayAccess interface, allowing array-style access to configuration:


The ArrayAccess methods delegate to the standard has(), get(), and set() methods internally.

Sources: src/Repository.php220-254


Configuration Access Flow


Diagram: Configuration Access Code Flow

This diagram illustrates the complete flow from application code through the config() function to the underlying Repository operations, showing how all configuration access ultimately operates on the protected $items array through the Hyperf\Collection\Arr utility methods.

Sources: src/Functions.php18-31 src/Repository.php26-28 src/Repository.php33-48 src/Repository.php162-173


Usage Pattern Summary

PatternSyntaxReturnsUse Case
Get Repositoryconfig()Repository instanceAccess Repository methods directly
Get Valueconfig('key')mixedRetrieve single configuration value
Get with Defaultconfig('key', $default)mixedRetrieve value with fallback
Set Valuesconfig(['key' => 'val'])nullUpdate configuration at runtime

Sources: src/Functions.php10-31


Next Steps

After installation and basic configuration access, you may want to explore:

Sources: Referenced from table of contents structure