定制 byjg/config 二次开发

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

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

byjg/config

Composer 安装命令:

composer require byjg/config

包简介

A very basic and minimalist PSR-11 implementation for config management and dependency injection.

README 文档

README

Sponsor Build Status Opensource ByJG GitHub source GitHub license GitHub release

This is a basic and minimalist implementation of PSR-11 for config management and dependency injection.

Features

  • PSR-11 Compatible: Implements PSR-11 Container Interface for standardized dependency injection
  • Auto-Initialization: Automatically loads configuration from a bootstrap file with zero setup
  • Static Facade: Clean, Laravel-style static API for accessing configuration anywhere
  • Environment-Based Configuration: Easily switch between development, production, and custom environments
  • Multiple Configuration Formats: Support for both PHP arrays and .env files
  • Dependency Injection: Simple API for defining and resolving dependencies
  • Type Conversion: Built-in parsers for converting configuration values to specific types
  • Caching Support: Optional caching of configuration values for improved performance
  • Environment Inheritance: Environments can inherit from one another to reduce configuration duplication
  • Abstract & Final Environments: Design flexible environment hierarchies with constraints
  • Performance Optimizations: Configure caching modes for different use cases

Quick Start with Auto-Initialization

The fastest way to get started is using the auto-initialization feature:

<?php
// 1. Create config/ConfigBootstrap.php
use ByJG\Config\ConfigInitializeInterface;
use ByJG\Config\Definition;
use ByJG\Config\Environment;

return new class implements ConfigInitializeInterface {
    public function loadDefinition(?string $env = null): Definition {
        $dev = new Environment('dev');
        $prod = new Environment('prod', [$dev]);

        return (new Definition())
            ->addEnvironment($dev)
            ->addEnvironment($prod);
    }
};
<?php
// 2. Create your config file (config/config-dev.php)
return [
    'database' => [
        'host' => 'localhost',
        'port' => 3306
    ],
    'app_name' => 'My Application',
    'debug' => true
];
<?php
// 3. Use Config anywhere in your app - no initialization needed!
use ByJG\Config\Config;

$appName = Config::get('app_name'); // "My Application"
$dbHost = Config::get('database')['host']; // "localhost"

Traditional Example

You can also use the traditional approach without auto-initialization:

<?php
// 1. Create a config file (config/config-dev.php)
return [
    'database' => [
        'host' => 'localhost',
        'port' => 3306
    ],
    'app_name' => 'My Application',
    'debug' => true
];

// 2. Set up and load the configuration
use ByJG\Config\Definition;
use ByJG\Config\Environment;

// Define the environments
$dev = new Environment('dev');
$prod = new Environment('prod', [$dev]); // prod inherits from dev

// Create and build the definition
$definition = (new Definition())
    ->addEnvironment($dev)
    ->addEnvironment($prod);

// Build with the current environment (using APP_ENV environment variable)
$container = $definition->build();

// 3. Use the configuration values
$appName = $container->get('app_name'); // "My Application"
$dbHost = $container->get('database')['host']; // "localhost"

Dependency Injection Example

<?php
// 1. Define your classes
namespace MyApp;

class DatabaseService 
{
    private $host;
    private $port;
    
    public function __construct($host, $port) 
    {
        $this->host = $host;
        $this->port = $port;
    }
}

class UserRepository 
{
    private $db;
    
    public function __construct(DatabaseService $db) 
    {
        $this->db = $db;
    }
}

// 2. Register them in your config file
use ByJG\Config\DependencyInjection as DI;
use ByJG\Config\Param;

return [
    'database' => [
        'host' => 'localhost',
        'port' => 3306
    ],
    
    // Register the database service
    \MyApp\DatabaseService::class => DI::bind(\MyApp\DatabaseService::class)
        ->withConstructorArgs([
            Param::get('database')['host'],
            Param::get('database')['port']
        ])
        ->toSingleton(),
    
    // Register the repository with auto-injection
    \MyApp\UserRepository::class => DI::bind(\MyApp\UserRepository::class)
        ->withInjectedConstructor()
        ->toInstance(),

    // For mixed injection (auto-inject classes, manually provide scalars)
    \MyApp\UserService::class => DI::bind(\MyApp\UserService::class)
        ->withInjectedConstructorOverrides([
            'apiKey' => 'my-secret-key'  // Manually provide scalar values
        ])
        ->toInstance()
];

// 3. Use the services from the container
$userRepo = $container->get(\MyApp\UserRepository::class);

Documentation Index

Start with the fundamentals and move toward the advanced features following this order:

Installation

composer require "byjg/config"

Tests

./vendor/bin/phpunit

Dependencies

flowchart TD
    byjg/config --> byjg/cache-engine
Loading

Open source ByJG

byjg/config 适用场景与选型建议

byjg/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.04k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 byjg/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 byjg/config 我们能提供哪些服务?
定制开发 / 二次开发

基于 byjg/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 10
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-18