byjg/config
Composer 安装命令:
composer require byjg/config
包简介
A very basic and minimalist PSR-11 implementation for config management and dependency injection.
README 文档
README
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:
- Set up the configuration files – Define environments, file naming, and directory structure;
- Load the configuration – Build
Definitioninstances and work with the PSR-11 container; - Using the Config Facade – Access container values through the static API;
- Auto-Initialization – Let
Configbootstrap itself fromconfig/ConfigBootstrap.php; - Define dependency injection – Bind services, constructor parameters, and factories;
- Work with special types – Parse
.envvalues through built-in or custom parsers; - Configure your webserver – Expose environment variables in different runtimes;
- Follow good practices – Apply conventions that keep configuration manageable;
- Explore advanced features – Use caching, inheritance constraints, and container helpers;
Installation
composer require "byjg/config"
Tests
./vendor/bin/phpunit
Dependencies
flowchart TD
byjg/config --> byjg/cache-engine
Loading
byjg/config 适用场景与选型建议
byjg/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.04k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 byjg/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 byjg/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 12.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 10
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-18