定制 solophp/configs 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

solophp/configs

最新稳定版本:v2.0.0

Composer 安装命令:

composer require solophp/configs

包简介

Simple and flexible configuration management library with dot notation support

README 文档

README

Latest Version on Packagist License PHP Version

Solo Configs is a simple PHP package for managing configuration settings in your application. It provides easy access to configuration values using dot notation and supports default values if a key is not found.

Requirements

  • PHP 8.1 or higher

Installation

You can install the package via Composer:

composer require solophp/configs

Usage

Create an instance of Configs by passing an array of configurations. You can then retrieve values using dot notation.

Basic Usage

use Solo\Configs\Configs;

// Create a new instance with your configurations
$configs = new Configs([
    'database' => [
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'secret',
    ],
    'app' => [
        'name' => 'My Application',
        'debug' => true,
        'settings' => [
            'timezone' => 'UTC'
        ]
    ]
]);

// Get values using dot notation
$dbHost = $configs->get('database.host');          // 'localhost'
$appName = $configs->get('app.name');             // 'My Application'
$timezone = $configs->get('app.settings.timezone'); // 'UTC'

// Using default values
$cache = $configs->get('cache.enabled', false);    // false (using default)

// Get entire sections
$dbConfig = $configs->get('database');    // Returns entire database array
$allConfigs = $configs->get();           // Returns all configurations

// Note: Magic method access is limited in PHP due to property name restrictions
// It's recommended to use the get() method directly for best compatibility

Configuration Structure

Your configuration array can be as deeply nested as needed:

$configs = new Configs([
    'session' => [
        'lifetime' => 86400,
        'options' => [
            'secure' => true,
            'httponly' => true,
            'samesite' => 'Lax'
        ]
    ],
    'cache' => [
        'driver' => 'redis',
        'connection' => [
            'host' => 'localhost',
            'port' => 6379
        ]
    ]
]);

API Reference

Methods

Constructor

public function __construct(array $configs)

Creates a new Configs instance.

Parameters:

  • array $configs: Configuration array

Get Configuration Value

public function get(string $key = '', mixed $default = null): mixed

Retrieves a configuration value using dot notation.

Parameters:

  • string $key: Configuration key using dot notation (optional)
  • mixed $default: Default value if key not found

Returns: Configuration value or default if not found

Magic Property Access

public function __get(string $key): mixed

Magic method for property access (limited functionality in PHP).

Parameters:

  • string $key: Configuration key

Returns: Configuration value or null if not found

Note: Due to PHP limitations with property names containing special characters, it's recommended to use the get() method directly.

Development

Running Tests

composer test

Code Style

Check code style:

composer cs

Fix code style:

composer cs-fix

License

This package is open-source and available under the MIT License.

统计信息

  • 总下载量: 75
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-16